답안 #434430

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

template <class T> using Vec = std::vector<T>;
template <class T, class U> using Map = __gnu_pbds::gp_hash_table<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 0 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 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 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 332 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 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 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 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 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 332 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 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 4 ms 588 KB Output is correct
35 Correct 129 ms 7416 KB Output is correct
36 Correct 177 ms 15196 KB Output is correct
37 Correct 175 ms 15536 KB Output is correct
38 Correct 170 ms 14180 KB Output is correct
39 Correct 5 ms 2764 KB Output is correct
40 Correct 6 ms 2900 KB Output is correct
41 Correct 6 ms 2892 KB Output is correct
42 Correct 5 ms 2764 KB Output is correct
43 Correct 9 ms 3196 KB Output is correct
44 Correct 9 ms 3020 KB Output is correct
45 Correct 5 ms 2764 KB Output is correct
46 Correct 5 ms 2764 KB Output is correct
47 Correct 6 ms 3020 KB Output is correct
48 Correct 7 ms 3032 KB Output is correct
49 Correct 20 ms 5068 KB Output is correct
50 Correct 176 ms 15932 KB Output is correct
51 Correct 14 ms 3236 KB Output is correct
52 Correct 142 ms 10356 KB Output is correct
53 Correct 20 ms 4824 KB Output is correct
54 Correct 162 ms 12968 KB Output is correct
55 Correct 21 ms 3788 KB Output is correct
56 Correct 16 ms 3916 KB Output is correct
57 Correct 11 ms 3404 KB Output is correct
58 Correct 12 ms 3532 KB Output is correct
59 Correct 5 ms 2764 KB Output is correct
60 Correct 130 ms 5092 KB Output is correct
61 Correct 8 ms 3148 KB Output is correct
62 Correct 165 ms 13380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 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 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 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 332 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 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 4 ms 588 KB Output is correct
35 Correct 129 ms 7416 KB Output is correct
36 Correct 177 ms 15196 KB Output is correct
37 Correct 175 ms 15536 KB Output is correct
38 Correct 170 ms 14180 KB Output is correct
39 Correct 5 ms 2764 KB Output is correct
40 Correct 6 ms 2900 KB Output is correct
41 Correct 6 ms 2892 KB Output is correct
42 Correct 5 ms 2764 KB Output is correct
43 Correct 9 ms 3196 KB Output is correct
44 Correct 9 ms 3020 KB Output is correct
45 Correct 5 ms 2764 KB Output is correct
46 Correct 5 ms 2764 KB Output is correct
47 Correct 6 ms 3020 KB Output is correct
48 Correct 7 ms 3032 KB Output is correct
49 Correct 20 ms 5068 KB Output is correct
50 Correct 176 ms 15932 KB Output is correct
51 Correct 14 ms 3236 KB Output is correct
52 Correct 142 ms 10356 KB Output is correct
53 Correct 20 ms 4824 KB Output is correct
54 Correct 162 ms 12968 KB Output is correct
55 Correct 21 ms 3788 KB Output is correct
56 Correct 16 ms 3916 KB Output is correct
57 Correct 11 ms 3404 KB Output is correct
58 Correct 12 ms 3532 KB Output is correct
59 Correct 5 ms 2764 KB Output is correct
60 Correct 130 ms 5092 KB Output is correct
61 Correct 8 ms 3148 KB Output is correct
62 Correct 165 ms 13380 KB Output is correct
63 Correct 826 ms 202244 KB Output is correct
64 Correct 830 ms 202308 KB Output is correct
65 Correct 837 ms 202404 KB Output is correct
66 Correct 288 ms 126788 KB Output is correct
67 Correct 470 ms 130848 KB Output is correct
68 Correct 283 ms 126756 KB Output is correct
69 Execution timed out 5062 ms 139196 KB Time limit exceeded
70 Halted 0 ms 0 KB -