#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
ll cnt = 0;
vector<int> par;
vector<vector<int> > group;
vector<set<int> > group_edges;
vector<set<int> > group_followers;
vector<set<int> > person_following;
void add_edge(int a, int gb);
void add_group_edge (int ga, int gb) {
ga = par[ga];
gb = par[gb];
if(ga == gb) return;
if(group_edges[ga].count(gb)) return;
group_edges[ga].insert(gb);
if(group_edges[gb].count(ga)){
if(group[ga].size() < group[gb].size()) swap(ga, gb);
vector<pair<int,int> > add_edges;
while(!group_followers[gb].empty()){
int a = *group_followers[gb].begin();
cnt -= (int)group[gb].size();
group_followers[gb].erase(a);
person_following[a].erase(gb);
add_edges.push_back({a, gb});
}
for(int b : group[gb]){
while(!person_following[b].empty()){
int gc = *person_following[b].begin();
cnt -= (int)group[gc].size();
person_following[b].erase(gc);
group_followers[gc].erase(b);
add_edges.push_back({b, gc});
}
}
cnt -= 1ll * ((int)group[ga].size()) * ((int)group[ga].size() - 1);
cnt -= 1ll * ((int)group[gb].size()) * ((int)group[gb].size() - 1);
for(int x : group[gb]){
par[x] = ga;
group[ga].push_back(x);
}
cnt += 1ll * ((int)group[ga].size()) * ((int)group[ga].size() - 1);
cnt += 1ll * ((int)group_followers[ga].size()) * ((int)group[gb].size());
for(pair<int,int> x : add_edges){
add_edge(x.first, x.second);
}
}
}
void add_edge(int a, int b){
int ga = par[a];
int gb = par[b];
if(ga == gb) return;
if(person_following[a].count(gb)) return;
cnt += (int)group[gb].size();
person_following[a].insert(gb);
group_followers[gb].insert(a);
add_group_edge(ga, gb);
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
par.resize(n), group.resize(n), group_edges.resize(n);
group_followers.resize(n), person_following.resize(n);
for(int i = 0; i < n; i++){
par[i] = i;
group[i].push_back(i);
}
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
a--; b--;
add_edge(a, b);
cout << cnt << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
3 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 |
6 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 |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 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 |
6 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 |
384 KB |
Output is correct |
25 |
Correct |
6 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 |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
3 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 |
6 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 |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 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 |
6 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 |
384 KB |
Output is correct |
25 |
Correct |
6 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 |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
7 ms |
512 KB |
Output is correct |
35 |
Correct |
90 ms |
4216 KB |
Output is correct |
36 |
Correct |
130 ms |
7016 KB |
Output is correct |
37 |
Correct |
119 ms |
7164 KB |
Output is correct |
38 |
Correct |
122 ms |
6776 KB |
Output is correct |
39 |
Correct |
8 ms |
896 KB |
Output is correct |
40 |
Correct |
9 ms |
1152 KB |
Output is correct |
41 |
Correct |
8 ms |
1152 KB |
Output is correct |
42 |
Correct |
8 ms |
1024 KB |
Output is correct |
43 |
Correct |
9 ms |
1024 KB |
Output is correct |
44 |
Correct |
9 ms |
1024 KB |
Output is correct |
45 |
Correct |
8 ms |
1024 KB |
Output is correct |
46 |
Correct |
8 ms |
896 KB |
Output is correct |
47 |
Correct |
11 ms |
1024 KB |
Output is correct |
48 |
Correct |
11 ms |
1024 KB |
Output is correct |
49 |
Correct |
16 ms |
1792 KB |
Output is correct |
50 |
Correct |
117 ms |
7288 KB |
Output is correct |
51 |
Correct |
16 ms |
1280 KB |
Output is correct |
52 |
Correct |
102 ms |
5368 KB |
Output is correct |
53 |
Correct |
19 ms |
1664 KB |
Output is correct |
54 |
Correct |
117 ms |
6264 KB |
Output is correct |
55 |
Correct |
11 ms |
1408 KB |
Output is correct |
56 |
Correct |
15 ms |
1408 KB |
Output is correct |
57 |
Correct |
10 ms |
1408 KB |
Output is correct |
58 |
Correct |
12 ms |
1408 KB |
Output is correct |
59 |
Correct |
9 ms |
1024 KB |
Output is correct |
60 |
Correct |
91 ms |
3448 KB |
Output is correct |
61 |
Correct |
10 ms |
1152 KB |
Output is correct |
62 |
Correct |
118 ms |
6520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
3 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 |
6 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 |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 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 |
6 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 |
384 KB |
Output is correct |
25 |
Correct |
6 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 |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
7 ms |
512 KB |
Output is correct |
35 |
Correct |
90 ms |
4216 KB |
Output is correct |
36 |
Correct |
130 ms |
7016 KB |
Output is correct |
37 |
Correct |
119 ms |
7164 KB |
Output is correct |
38 |
Correct |
122 ms |
6776 KB |
Output is correct |
39 |
Correct |
8 ms |
896 KB |
Output is correct |
40 |
Correct |
9 ms |
1152 KB |
Output is correct |
41 |
Correct |
8 ms |
1152 KB |
Output is correct |
42 |
Correct |
8 ms |
1024 KB |
Output is correct |
43 |
Correct |
9 ms |
1024 KB |
Output is correct |
44 |
Correct |
9 ms |
1024 KB |
Output is correct |
45 |
Correct |
8 ms |
1024 KB |
Output is correct |
46 |
Correct |
8 ms |
896 KB |
Output is correct |
47 |
Correct |
11 ms |
1024 KB |
Output is correct |
48 |
Correct |
11 ms |
1024 KB |
Output is correct |
49 |
Correct |
16 ms |
1792 KB |
Output is correct |
50 |
Correct |
117 ms |
7288 KB |
Output is correct |
51 |
Correct |
16 ms |
1280 KB |
Output is correct |
52 |
Correct |
102 ms |
5368 KB |
Output is correct |
53 |
Correct |
19 ms |
1664 KB |
Output is correct |
54 |
Correct |
117 ms |
6264 KB |
Output is correct |
55 |
Correct |
11 ms |
1408 KB |
Output is correct |
56 |
Correct |
15 ms |
1408 KB |
Output is correct |
57 |
Correct |
10 ms |
1408 KB |
Output is correct |
58 |
Correct |
12 ms |
1408 KB |
Output is correct |
59 |
Correct |
9 ms |
1024 KB |
Output is correct |
60 |
Correct |
91 ms |
3448 KB |
Output is correct |
61 |
Correct |
10 ms |
1152 KB |
Output is correct |
62 |
Correct |
118 ms |
6520 KB |
Output is correct |
63 |
Correct |
580 ms |
64608 KB |
Output is correct |
64 |
Correct |
576 ms |
64888 KB |
Output is correct |
65 |
Correct |
582 ms |
64540 KB |
Output is correct |
66 |
Correct |
307 ms |
32240 KB |
Output is correct |
67 |
Correct |
520 ms |
38324 KB |
Output is correct |
68 |
Correct |
280 ms |
32240 KB |
Output is correct |
69 |
Correct |
512 ms |
33392 KB |
Output is correct |
70 |
Correct |
299 ms |
32500 KB |
Output is correct |
71 |
Correct |
275 ms |
32240 KB |
Output is correct |
72 |
Correct |
583 ms |
36948 KB |
Output is correct |
73 |
Correct |
558 ms |
36980 KB |
Output is correct |
74 |
Correct |
1197 ms |
60936 KB |
Output is correct |
75 |
Correct |
768 ms |
38512 KB |
Output is correct |
76 |
Correct |
876 ms |
48572 KB |
Output is correct |
77 |
Correct |
894 ms |
48448 KB |
Output is correct |
78 |
Correct |
285 ms |
35060 KB |
Output is correct |
79 |
Correct |
513 ms |
38256 KB |
Output is correct |
80 |
Correct |
288 ms |
34932 KB |
Output is correct |
81 |
Correct |
453 ms |
36976 KB |
Output is correct |
82 |
Correct |
811 ms |
51128 KB |
Output is correct |
83 |
Correct |
820 ms |
51112 KB |
Output is correct |
84 |
Correct |
794 ms |
51012 KB |
Output is correct |
85 |
Correct |
813 ms |
51188 KB |
Output is correct |
86 |
Correct |
297 ms |
36080 KB |
Output is correct |
87 |
Correct |
351 ms |
37100 KB |
Output is correct |
88 |
Correct |
555 ms |
36856 KB |
Output is correct |
89 |
Correct |
871 ms |
46324 KB |
Output is correct |