답안 #434431

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434431 2021-06-21T09:46:26 Z KoD 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
17 / 100
5000 ms 110016 KB
#include <bits/extc++.h>

template <class T> using Vec = std::vector<T>;
template <class T, class U> using Map = std::unordered_map<T, U>;

int scan() {
    int x;
    std::scanf("%d", &x);
    return x;
}

void print(const long long x) {
    std::printf("%lld\n", x);
}

struct DSU {
    Vec<int> par;
    DSU(const int n) : par(n, -1) {}
    int find(const int x) {
        return par[x] < 0 ? x : par[x] = find(par[x]);
    }
    int size(const int x) {
        return -par[find(x)];
    }
    void absorb(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) {
            par[x] += par[y];
            par[y] = x;
        }
    }
};

int main() {
    const int N = scan();
    int M = scan();
    Vec<Map<int, std::set<int>>> in(N), out(N);
    Vec<int> sum(N);
    DSU dsu(N);
    long long answer = 0;
    std::queue<std::pair<int, int>> merge;
    const auto remove = [&](const int x, const int y) {
        const int s = out[y][x].size();
        const int t = out[x][y].size();
        answer -= (long long) dsu.size(x) * s;
        answer -= (long long) dsu.size(y) * t;
        sum[x] -= s + t;
        sum[y] -= s + t;
        in[x].erase(y);
        out[x].erase(y);
        in[y].erase(x);
        out[y].erase(x);
    };
    const auto process = [&] {
        while (!merge.empty()) {
            auto [x, y] = merge.front();
            merge.pop();
            x = dsu.find(x);
            y = dsu.find(y);
            if (x == y) {
                continue;
            }
            remove(x, y);
            if (sum[y] > sum[x]) {
                std::swap(x, y);
            }
            answer += (long long) dsu.size(x) * dsu.size(y) * 2;
            for (auto &[z, set] : in[y]) {
                assert(!set.empty());
                answer -= (long long) set.size() * dsu.size(y);
                if (out[x].find(z) != out[x].end()) {
                    merge.emplace(x, z);
                }
                for (const auto u : set) {
                    if (in[x][z].insert(u).second) {
                        sum[x] += 1;
                        sum[z] += 1;
                        answer += dsu.size(x);
                        out[z][x].insert(u);
                    }
                }
                sum[z] -= out[z][y].size();
                out[z].erase(y);
            }
            for (const auto &[z, set] : in[x]) {
                answer += (long long) dsu.size(y) * set.size();
            } 
            for (auto &[z, set] : out[y]) {
                assert(!set.empty());
                if (out[z].find(x) != out[z].end()) {
                    merge.emplace(x, z);
                }
                for (const auto u : set) {
                    sum[x] += 1;
                    sum[z] += 1;
                    out[x][z].insert(u);
                    in[z][x].insert(u);
                }
                sum[z] -= in[z][y].size();
                in[z].erase(y);
            }
            in[y].clear();
            out[y].clear();
            sum[y] = 0;
            dsu.absorb(x, y);
        }
    };
    while (M--) {
        const int a = scan() - 1;
        const int b = scan() - 1;
        const int u = dsu.find(a);
        const int v = dsu.find(b);
        if (u != v) {
            if (in[u].find(v) != in[u].end()) {
                merge.emplace(u, v);
                process();
            } else {
                if (out[u][v].insert(a).second) {
                    sum[u] += 1;
                    sum[v] += 1;
                    in[v][u].insert(a);
                    answer += dsu.size(v);
                }
            }
        }
        print(answer);
    }
    return 0;
}

Compilation message

joitter2.cpp: In function 'int scan()':
joitter2.cpp:8:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |     std::scanf("%d", &x);
      |     ~~~~~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 4 ms 460 KB Output is correct
35 Correct 121 ms 5060 KB Output is correct
36 Correct 177 ms 9484 KB Output is correct
37 Correct 196 ms 9828 KB Output is correct
38 Correct 168 ms 9028 KB Output is correct
39 Correct 4 ms 972 KB Output is correct
40 Correct 5 ms 1100 KB Output is correct
41 Correct 6 ms 1100 KB Output is correct
42 Correct 5 ms 972 KB Output is correct
43 Correct 10 ms 972 KB Output is correct
44 Correct 9 ms 972 KB Output is correct
45 Correct 4 ms 972 KB Output is correct
46 Correct 4 ms 972 KB Output is correct
47 Correct 5 ms 1100 KB Output is correct
48 Correct 5 ms 1100 KB Output is correct
49 Correct 15 ms 2252 KB Output is correct
50 Correct 202 ms 9796 KB Output is correct
51 Correct 14 ms 1484 KB Output is correct
52 Correct 143 ms 6744 KB Output is correct
53 Correct 16 ms 2124 KB Output is correct
54 Correct 159 ms 8088 KB Output is correct
55 Correct 14 ms 1996 KB Output is correct
56 Correct 14 ms 1996 KB Output is correct
57 Correct 11 ms 1996 KB Output is correct
58 Correct 11 ms 1996 KB Output is correct
59 Correct 4 ms 844 KB Output is correct
60 Correct 116 ms 3172 KB Output is correct
61 Correct 8 ms 1228 KB Output is correct
62 Correct 160 ms 8636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 4 ms 460 KB Output is correct
35 Correct 121 ms 5060 KB Output is correct
36 Correct 177 ms 9484 KB Output is correct
37 Correct 196 ms 9828 KB Output is correct
38 Correct 168 ms 9028 KB Output is correct
39 Correct 4 ms 972 KB Output is correct
40 Correct 5 ms 1100 KB Output is correct
41 Correct 6 ms 1100 KB Output is correct
42 Correct 5 ms 972 KB Output is correct
43 Correct 10 ms 972 KB Output is correct
44 Correct 9 ms 972 KB Output is correct
45 Correct 4 ms 972 KB Output is correct
46 Correct 4 ms 972 KB Output is correct
47 Correct 5 ms 1100 KB Output is correct
48 Correct 5 ms 1100 KB Output is correct
49 Correct 15 ms 2252 KB Output is correct
50 Correct 202 ms 9796 KB Output is correct
51 Correct 14 ms 1484 KB Output is correct
52 Correct 143 ms 6744 KB Output is correct
53 Correct 16 ms 2124 KB Output is correct
54 Correct 159 ms 8088 KB Output is correct
55 Correct 14 ms 1996 KB Output is correct
56 Correct 14 ms 1996 KB Output is correct
57 Correct 11 ms 1996 KB Output is correct
58 Correct 11 ms 1996 KB Output is correct
59 Correct 4 ms 844 KB Output is correct
60 Correct 116 ms 3172 KB Output is correct
61 Correct 8 ms 1228 KB Output is correct
62 Correct 160 ms 8636 KB Output is correct
63 Correct 620 ms 109920 KB Output is correct
64 Correct 649 ms 110016 KB Output is correct
65 Correct 618 ms 109936 KB Output is correct
66 Correct 228 ms 35952 KB Output is correct
67 Correct 444 ms 42816 KB Output is correct
68 Correct 217 ms 36036 KB Output is correct
69 Execution timed out 5019 ms 30176 KB Time limit exceeded
70 Halted 0 ms 0 KB -