Submission #231295

# Submission time Handle Problem Language Result Execution time Memory
231295 2020-05-13T10:25:11 Z WLZ Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1314 ms 72140 KB
#include "bits/stdc++.h"
using namespace std;

int n, m;
long long ans = 0;
vector<int> p, sz;
vector< set<int> > in_v, in_group, out_group, members;
vector<long long> cost;

int root(int x) {
    if (p[x] == x) {
        return x;
    }
    return (p[x] = root(p[x]));
}

void merge(int u, int v) {
    u = root(u); v = root(v);
    if (u == v) {
        return;
    }
    ans -= (long long) members[u].size() * (long long) (members[u].size() + in_v[u].size() - 1);
    ans -= (long long) members[v].size() * (long long) (members[v].size() + in_v[v].size() - 1);
    if (members[u].size() + in_v[u].size() + in_group[u].size() + out_group[v].size() < members[v].size() + in_v[v].size() + in_group[v].size() + out_group[v].size()) {
        swap(u, v);
    }
    for (auto& x : members[v]) {
        members[u].insert(x);
        if (in_v[u].count(x)) {
            in_v[u].erase(x);
        }
    }
    members[v].clear();
    for (auto& x : in_v[v]) {
        if (!members[u].count(x)) {
            in_v[u].insert(x);
        }
    }
    in_v[v].clear();
    ans += (long long) members[u].size() * (long long) (members[u].size() + in_v[u].size() - 1);
    vector<int> to_merge;
    for (auto& x : in_group[v]) {
        if (out_group[u].count(x)) {
            to_merge.push_back(x);
        }
        out_group[x].erase(v);
        out_group[x].insert(u);
        in_group[u].insert(x);
    }
    in_group[v].clear();
    for (auto& x : out_group[v]) {
        if (in_group[u].count(x)) {
            to_merge.push_back(x);
        }
        in_group[x].erase(v);
        in_group[x].insert(u);
        out_group[u].insert(x);
    }
    out_group[v].clear();
    in_group[u].erase(v);
    out_group[u].erase(v);
    p[v] = u;
    for (auto& x : to_merge) {
        merge(u, x);
    }
}

void add_edge(int u, int v) {
    v = root(v);
    if (v == root(u)) {
        return;
    }
    if (!in_v[v].count(u)) {
        ans += (long long) members[v].size();
        in_v[v].insert(u);
    }
    u = root(u);
    in_group[v].insert(u);
    out_group[u].insert(v);
    if (in_group[u].count(v)) {
        merge(u, v);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    p.resize(n + 1);
    iota(p.begin(), p.end(), 0);
    in_v.resize(n + 1);
    in_group.resize(n + 1);
    out_group.resize(n + 1);
    members.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        members[i].insert(i);
    }
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        add_edge(u, v);
        cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 420 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 392 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 420 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 392 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 7 ms 512 KB Output is correct
35 Correct 93 ms 6392 KB Output is correct
36 Correct 122 ms 9336 KB Output is correct
37 Correct 122 ms 9464 KB Output is correct
38 Correct 118 ms 9080 KB Output is correct
39 Correct 8 ms 896 KB Output is correct
40 Correct 9 ms 1024 KB Output is correct
41 Correct 10 ms 1024 KB Output is correct
42 Correct 8 ms 896 KB Output is correct
43 Correct 10 ms 1024 KB Output is correct
44 Correct 10 ms 1024 KB Output is correct
45 Correct 8 ms 896 KB Output is correct
46 Correct 8 ms 896 KB Output is correct
47 Correct 10 ms 1024 KB Output is correct
48 Correct 10 ms 896 KB Output is correct
49 Correct 18 ms 1792 KB Output is correct
50 Correct 128 ms 9568 KB Output is correct
51 Correct 14 ms 1280 KB Output is correct
52 Correct 104 ms 7928 KB Output is correct
53 Correct 17 ms 1664 KB Output is correct
54 Correct 117 ms 8696 KB Output is correct
55 Correct 12 ms 1408 KB Output is correct
56 Correct 12 ms 1408 KB Output is correct
57 Correct 12 ms 1664 KB Output is correct
58 Correct 12 ms 1664 KB Output is correct
59 Correct 9 ms 1024 KB Output is correct
60 Correct 89 ms 5880 KB Output is correct
61 Correct 11 ms 1152 KB Output is correct
62 Correct 123 ms 8956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 420 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 392 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 7 ms 512 KB Output is correct
35 Correct 93 ms 6392 KB Output is correct
36 Correct 122 ms 9336 KB Output is correct
37 Correct 122 ms 9464 KB Output is correct
38 Correct 118 ms 9080 KB Output is correct
39 Correct 8 ms 896 KB Output is correct
40 Correct 9 ms 1024 KB Output is correct
41 Correct 10 ms 1024 KB Output is correct
42 Correct 8 ms 896 KB Output is correct
43 Correct 10 ms 1024 KB Output is correct
44 Correct 10 ms 1024 KB Output is correct
45 Correct 8 ms 896 KB Output is correct
46 Correct 8 ms 896 KB Output is correct
47 Correct 10 ms 1024 KB Output is correct
48 Correct 10 ms 896 KB Output is correct
49 Correct 18 ms 1792 KB Output is correct
50 Correct 128 ms 9568 KB Output is correct
51 Correct 14 ms 1280 KB Output is correct
52 Correct 104 ms 7928 KB Output is correct
53 Correct 17 ms 1664 KB Output is correct
54 Correct 117 ms 8696 KB Output is correct
55 Correct 12 ms 1408 KB Output is correct
56 Correct 12 ms 1408 KB Output is correct
57 Correct 12 ms 1664 KB Output is correct
58 Correct 12 ms 1664 KB Output is correct
59 Correct 9 ms 1024 KB Output is correct
60 Correct 89 ms 5880 KB Output is correct
61 Correct 11 ms 1152 KB Output is correct
62 Correct 123 ms 8956 KB Output is correct
63 Correct 591 ms 71928 KB Output is correct
64 Correct 551 ms 72056 KB Output is correct
65 Correct 576 ms 72140 KB Output is correct
66 Correct 270 ms 28964 KB Output is correct
67 Correct 586 ms 37012 KB Output is correct
68 Correct 255 ms 28792 KB Output is correct
69 Correct 626 ms 35704 KB Output is correct
70 Correct 278 ms 28664 KB Output is correct
71 Correct 314 ms 28664 KB Output is correct
72 Correct 588 ms 36600 KB Output is correct
73 Correct 607 ms 36472 KB Output is correct
74 Correct 1314 ms 49144 KB Output is correct
75 Correct 826 ms 41720 KB Output is correct
76 Correct 1042 ms 48048 KB Output is correct
77 Correct 1043 ms 47992 KB Output is correct
78 Correct 307 ms 36816 KB Output is correct
79 Correct 528 ms 40056 KB Output is correct
80 Correct 318 ms 34424 KB Output is correct
81 Correct 581 ms 37880 KB Output is correct
82 Correct 983 ms 56184 KB Output is correct
83 Correct 983 ms 56172 KB Output is correct
84 Correct 966 ms 65528 KB Output is correct
85 Correct 931 ms 65528 KB Output is correct
86 Correct 423 ms 32792 KB Output is correct
87 Correct 481 ms 34996 KB Output is correct
88 Correct 645 ms 36600 KB Output is correct
89 Correct 1022 ms 47352 KB Output is correct