Submission #2271614


Source Code Expand

#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;

struct UnionFind {
        int n;
        vector<int> parent;
        vector<int> rank;
        vector<int> num;
        int find(int x) {
                if (parent[x] == x) return  x;
                return parent[x] = find(parent[x]);
        }
        UnionFind(int n_) {
                n = n_;
                parent.resize(n);
                for (int i = 0; i < n; i ++) parent[i] = i;
                rank.assign(n, 0);
                num.assign(n, 1);
        }
        void unite(int x, int y) {
                if ((x = find(x)) != (y = find(y))) {
                        if (rank[x] < rank[y]) {
                                parent[x] = y;
                                num[y] += num[x];
                        } else {
                                parent[y] = x;
                                if (rank[x] == rank[y]) rank[x] ++;
                                num[x] += num[y];
                        }
                        n --;
                }
        }
        bool same(int x, int y) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};

int main() {
        int n, m;
        scanf("%d%d", &n, &m);
        vector<pair<int, pair<int, int>>> es;
        for (int i = 0; i < m; i ++) {
                int a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                a --, b --;
                es.push_back({c, {a, b}});
        }
        sort(es.begin(), es.end());
        vector<pair<int, pair<int, int>>> use;
        UnionFind uf(n);
        long long tot = 0;
        for (int i = 0; i < (int) es.size(); i ++) {
                int a, b;
                tie(a, b) = es[i].second;
                if (uf.same(a, b)) continue;
                uf.unite(a, b);
                use.push_back(es[i]);
                tot += es[i].first;
        }
        assert((int) use.size() == n - 1);
        vector<vector<pair<int, int>>> g(n);
        for (int i = 0; i < n - 1; i ++) {
                int c = use[i].first;
                int a = use[i].second.first;
                int b = use[i].second.second;
                g[a].push_back({b, c});
                g[b].push_back({a, c});
        }
        int q;
        scanf("%d", &q);
        assert(q <= 3000);
        while (q --) {
                int s, t;
                scanf("%d%d", &s, &t);
                s --, t --;
                int maxval = 0;
                function<void (int, int, int)> dfs = [&](int u, int prev, int ma) {
                        if (u == t) {
                                maxval = ma;
                                return;
                        }
                        for (auto e : g[u]) if (e.first != prev) {
                                dfs(e.first, u, max(ma, e.second));
                        }
                };
                dfs(s, -1, 0);
                printf("%lld\n", tot - maxval);
        }
        return 0;
}

Submission Info

Submission Time
Task A - Graph
User KokiYmgch
Language C++14 (GCC 5.4.1)
Score 500
Code Size 3247 Byte
Status RE
Exec Time 396 ms
Memory 8304 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:49:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &n, &m);
                              ^
./Main.cpp:53:44: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d%d%d", &a, &b, &c);
                                            ^
./Main.cpp:79:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &q);
                        ^
./Main.cpp:83:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d%d", &s, &t);
                                      ^

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 200 / 200 300 / 300 0 / 200
Status
AC × 2
AC × 12
AC × 21
AC × 23
RE × 8
Set Name Test Cases
Sample sample_1.txt, sample_2.txt
subtask1 sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
subtask2 sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt
All sample_1.txt, sample_2.txt, sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_3_1.txt, subtask_3_2.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt
Case Name Status Exec Time Memory
sample_1.txt AC 1 ms 256 KB
sample_2.txt AC 1 ms 256 KB
subtask_1_1.txt AC 1 ms 256 KB
subtask_1_10.txt AC 68 ms 3444 KB
subtask_1_11.txt AC 1 ms 256 KB
subtask_1_2.txt AC 15 ms 1148 KB
subtask_1_3.txt AC 135 ms 7152 KB
subtask_1_4.txt AC 3 ms 640 KB
subtask_1_5.txt AC 3 ms 640 KB
subtask_1_6.txt AC 34 ms 1912 KB
subtask_1_7.txt AC 134 ms 6640 KB
subtask_1_8.txt AC 3 ms 640 KB
subtask_1_9.txt AC 5 ms 704 KB
subtask_2_1.txt AC 386 ms 6512 KB
subtask_2_2.txt AC 387 ms 7024 KB
subtask_2_3.txt AC 388 ms 6640 KB
subtask_2_4.txt AC 395 ms 7920 KB
subtask_2_5.txt AC 250 ms 640 KB
subtask_2_6.txt AC 263 ms 892 KB
subtask_2_7.txt AC 296 ms 1912 KB
subtask_2_8.txt AC 396 ms 7024 KB
subtask_3_1.txt RE 231 ms 6768 KB
subtask_3_2.txt RE 228 ms 8304 KB
subtask_3_3.txt RE 97 ms 640 KB
subtask_3_4.txt RE 101 ms 704 KB
subtask_3_5.txt RE 162 ms 3444 KB
subtask_3_6.txt RE 196 ms 7408 KB
subtask_3_7.txt RE 228 ms 7536 KB
subtask_3_8.txt RE 228 ms 7280 KB