Submission #434427

# Submission time Handle Problem Language Result Execution time Memory
434427 2021-06-21T09:38:16 Z KoD Making Friends on Joitter is Fun (JOI20_joitter2) C++17
17 / 100
5000 ms 100444 KB
#include <bits/stdc++.h>

template <class T> using Vec = std::vector<T>;

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<std::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 process = [&] {
        while (!merge.empty()) {
            auto [x, y] = merge.front();
            merge.pop();
            x = dsu.find(x);
            y = dsu.find(y);
            if (x == y) {
                continue;
            }
            answer -= (long long) dsu.size(x) * out[y][x].size();
            answer -= (long long) dsu.size(y) * out[x][y].size();
            sum[x] -= in[x][y].size();
            in[x].erase(y);
            sum[x] -= out[x][y].size();
            out[x].erase(y);
            sum[y] -= in[y][x].size();
            in[y].erase(x);
            sum[y] -= out[y][x].size();
            out[y].erase(x);
            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);
                set.clear();
            }
            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);
                set.clear();
            }
            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:7:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 |     std::scanf("%d", &x);
      |     ~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory 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 2 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 204 KB Output is correct
27 Correct 1 ms 288 KB Output is correct
28 Correct 1 ms 204 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 3 ms 332 KB Output is correct
# Verdict Execution time Memory 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 2 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 204 KB Output is correct
27 Correct 1 ms 288 KB Output is correct
28 Correct 1 ms 204 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 3 ms 332 KB Output is correct
34 Correct 4 ms 460 KB Output is correct
35 Correct 134 ms 7476 KB Output is correct
36 Correct 200 ms 12260 KB Output is correct
37 Correct 263 ms 12432 KB Output is correct
38 Correct 187 ms 11704 KB Output is correct
39 Correct 3 ms 460 KB Output is correct
40 Correct 5 ms 844 KB Output is correct
41 Correct 4 ms 844 KB Output is correct
42 Correct 3 ms 460 KB Output is correct
43 Correct 12 ms 844 KB Output is correct
44 Correct 12 ms 824 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 4 ms 844 KB Output is correct
48 Correct 5 ms 864 KB Output is correct
49 Correct 19 ms 2292 KB Output is correct
50 Correct 205 ms 12708 KB Output is correct
51 Correct 20 ms 1296 KB Output is correct
52 Correct 151 ms 9348 KB Output is correct
53 Correct 21 ms 2176 KB Output is correct
54 Correct 177 ms 10852 KB Output is correct
55 Correct 19 ms 1712 KB Output is correct
56 Correct 18 ms 1716 KB Output is correct
57 Correct 14 ms 1612 KB Output is correct
58 Correct 14 ms 1692 KB Output is correct
59 Correct 3 ms 532 KB Output is correct
60 Correct 123 ms 5460 KB Output is correct
61 Correct 7 ms 972 KB Output is correct
62 Correct 186 ms 11384 KB Output is correct
# Verdict Execution time Memory 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 2 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 204 KB Output is correct
27 Correct 1 ms 288 KB Output is correct
28 Correct 1 ms 204 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 3 ms 332 KB Output is correct
34 Correct 4 ms 460 KB Output is correct
35 Correct 134 ms 7476 KB Output is correct
36 Correct 200 ms 12260 KB Output is correct
37 Correct 263 ms 12432 KB Output is correct
38 Correct 187 ms 11704 KB Output is correct
39 Correct 3 ms 460 KB Output is correct
40 Correct 5 ms 844 KB Output is correct
41 Correct 4 ms 844 KB Output is correct
42 Correct 3 ms 460 KB Output is correct
43 Correct 12 ms 844 KB Output is correct
44 Correct 12 ms 824 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 4 ms 844 KB Output is correct
48 Correct 5 ms 864 KB Output is correct
49 Correct 19 ms 2292 KB Output is correct
50 Correct 205 ms 12708 KB Output is correct
51 Correct 20 ms 1296 KB Output is correct
52 Correct 151 ms 9348 KB Output is correct
53 Correct 21 ms 2176 KB Output is correct
54 Correct 177 ms 10852 KB Output is correct
55 Correct 19 ms 1712 KB Output is correct
56 Correct 18 ms 1716 KB Output is correct
57 Correct 14 ms 1612 KB Output is correct
58 Correct 14 ms 1692 KB Output is correct
59 Correct 3 ms 532 KB Output is correct
60 Correct 123 ms 5460 KB Output is correct
61 Correct 7 ms 972 KB Output is correct
62 Correct 186 ms 11384 KB Output is correct
63 Correct 548 ms 100392 KB Output is correct
64 Correct 544 ms 100420 KB Output is correct
65 Correct 533 ms 100444 KB Output is correct
66 Correct 160 ms 14916 KB Output is correct
67 Correct 323 ms 28940 KB Output is correct
68 Correct 188 ms 14872 KB Output is correct
69 Execution timed out 5042 ms 23396 KB Time limit exceeded
70 Halted 0 ms 0 KB -