#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const int nmax = 1000001;
set <int> in[nmax], out[nmax];
set <int> indg[nmax];
ll sz[nmax];
ll curans;
int p[nmax];
vector <vector <int> > cmp(nmax);
int find_set(int a){
return p[a] == a ? a : p[a] = find_set(p[a]);
}
void union_sets(int a, int b){
if(a == b)
return;
if(sz[a] < sz[b])
swap(a, b);
curans -= (ll)indg[b].size() * sz[b];
curans -= (ll)indg[a].size() * sz[a];
curans -= sz[b] * sz[b] - sz[b];
curans -= sz[a] * sz[a] - sz[a];
// cout << curans << ' ';
vector <int> vec;
out[b].erase(a);
out[a].erase(b);
in[a].erase(b);
in[b].erase(a);
for(int to : out[b]){
in[to].erase(b);
if(find_set(to) == a) continue;
if(in[a].find(to) != in[a].end())
vec.pb(to);
out[a].insert(to);
in[to].insert(a);
}
for(int to : in[b]){
out[to].erase(b);
if(find_set(to) == a) continue;
if(out[a].find(to) != out[a].end())
vec.pb(to);
in[a].insert(to);
out[to].insert(a);
}
for(int to : indg[b]){
if(find_set(to) != a) indg[a].insert(to);
}
for(int to : cmp[b]){
cmp[a].pb(to);
indg[a].erase(to);
}
p[b] = a;
sz[a] += sz[b];
curans += sz[a] * (ll)indg[a].size();
//cout << sz[a] << ' ';
// cout << curans << ' ';
curans += sz[a] * sz[a] - sz[a];
for(int to : vec){
union_sets(find_set(a), find_set(to));
}
}
void Merge(int a, int b){
b = find_set(b);
int x= find_set(a);
//cout << x << ' ' << b << "\n";
if(x == b)
return;
if(in[x].find(b) != in[x].end()){
// cout << 1;
union_sets(x, b);
}
else{
curans -= sz[b] * (ll)indg[b].size();
indg[b].insert(a);
curans += sz[b] * (ll)indg[b].size();
in[b].insert(x);
out[x].insert(b);
}
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 1; i <= n; i++){
p[i] = i;
sz[i] = 1;
cmp[i].pb(i);
}
int m; cin >> m;
for(int i= 1; i <= m; i++){
int u, v; cin >> u >> v;
Merge(u, v);
cout << curans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
164676 KB |
Output is correct |
2 |
Correct |
81 ms |
164684 KB |
Output is correct |
3 |
Correct |
73 ms |
164700 KB |
Output is correct |
4 |
Correct |
74 ms |
164676 KB |
Output is correct |
5 |
Correct |
79 ms |
164640 KB |
Output is correct |
6 |
Correct |
76 ms |
164752 KB |
Output is correct |
7 |
Correct |
74 ms |
164688 KB |
Output is correct |
8 |
Correct |
75 ms |
164720 KB |
Output is correct |
9 |
Correct |
81 ms |
164680 KB |
Output is correct |
10 |
Correct |
75 ms |
164624 KB |
Output is correct |
11 |
Correct |
76 ms |
164640 KB |
Output is correct |
12 |
Correct |
72 ms |
164704 KB |
Output is correct |
13 |
Correct |
73 ms |
164816 KB |
Output is correct |
14 |
Correct |
76 ms |
164688 KB |
Output is correct |
15 |
Correct |
73 ms |
164676 KB |
Output is correct |
16 |
Correct |
75 ms |
164684 KB |
Output is correct |
17 |
Correct |
72 ms |
164676 KB |
Output is correct |
18 |
Correct |
70 ms |
164664 KB |
Output is correct |
19 |
Correct |
78 ms |
164608 KB |
Output is correct |
20 |
Correct |
81 ms |
164684 KB |
Output is correct |
21 |
Correct |
72 ms |
164728 KB |
Output is correct |
22 |
Correct |
74 ms |
164628 KB |
Output is correct |
23 |
Correct |
73 ms |
164712 KB |
Output is correct |
24 |
Correct |
79 ms |
164700 KB |
Output is correct |
25 |
Correct |
75 ms |
164668 KB |
Output is correct |
26 |
Correct |
75 ms |
164688 KB |
Output is correct |
27 |
Correct |
73 ms |
164676 KB |
Output is correct |
28 |
Correct |
73 ms |
164624 KB |
Output is correct |
29 |
Correct |
73 ms |
164732 KB |
Output is correct |
30 |
Correct |
80 ms |
164684 KB |
Output is correct |
31 |
Correct |
84 ms |
164684 KB |
Output is correct |
32 |
Correct |
72 ms |
164700 KB |
Output is correct |
33 |
Correct |
73 ms |
164708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
164676 KB |
Output is correct |
2 |
Correct |
81 ms |
164684 KB |
Output is correct |
3 |
Correct |
73 ms |
164700 KB |
Output is correct |
4 |
Correct |
74 ms |
164676 KB |
Output is correct |
5 |
Correct |
79 ms |
164640 KB |
Output is correct |
6 |
Correct |
76 ms |
164752 KB |
Output is correct |
7 |
Correct |
74 ms |
164688 KB |
Output is correct |
8 |
Correct |
75 ms |
164720 KB |
Output is correct |
9 |
Correct |
81 ms |
164680 KB |
Output is correct |
10 |
Correct |
75 ms |
164624 KB |
Output is correct |
11 |
Correct |
76 ms |
164640 KB |
Output is correct |
12 |
Correct |
72 ms |
164704 KB |
Output is correct |
13 |
Correct |
73 ms |
164816 KB |
Output is correct |
14 |
Correct |
76 ms |
164688 KB |
Output is correct |
15 |
Correct |
73 ms |
164676 KB |
Output is correct |
16 |
Correct |
75 ms |
164684 KB |
Output is correct |
17 |
Correct |
72 ms |
164676 KB |
Output is correct |
18 |
Correct |
70 ms |
164664 KB |
Output is correct |
19 |
Correct |
78 ms |
164608 KB |
Output is correct |
20 |
Correct |
81 ms |
164684 KB |
Output is correct |
21 |
Correct |
72 ms |
164728 KB |
Output is correct |
22 |
Correct |
74 ms |
164628 KB |
Output is correct |
23 |
Correct |
73 ms |
164712 KB |
Output is correct |
24 |
Correct |
79 ms |
164700 KB |
Output is correct |
25 |
Correct |
75 ms |
164668 KB |
Output is correct |
26 |
Correct |
75 ms |
164688 KB |
Output is correct |
27 |
Correct |
73 ms |
164676 KB |
Output is correct |
28 |
Correct |
73 ms |
164624 KB |
Output is correct |
29 |
Correct |
73 ms |
164732 KB |
Output is correct |
30 |
Correct |
80 ms |
164684 KB |
Output is correct |
31 |
Correct |
84 ms |
164684 KB |
Output is correct |
32 |
Correct |
72 ms |
164700 KB |
Output is correct |
33 |
Correct |
73 ms |
164708 KB |
Output is correct |
34 |
Correct |
78 ms |
164812 KB |
Output is correct |
35 |
Correct |
155 ms |
169068 KB |
Output is correct |
36 |
Correct |
162 ms |
171744 KB |
Output is correct |
37 |
Correct |
182 ms |
171800 KB |
Output is correct |
38 |
Correct |
163 ms |
171372 KB |
Output is correct |
39 |
Correct |
81 ms |
164900 KB |
Output is correct |
40 |
Correct |
83 ms |
165024 KB |
Output is correct |
41 |
Correct |
75 ms |
165048 KB |
Output is correct |
42 |
Correct |
74 ms |
164848 KB |
Output is correct |
43 |
Correct |
80 ms |
165028 KB |
Output is correct |
44 |
Correct |
79 ms |
165012 KB |
Output is correct |
45 |
Correct |
76 ms |
164924 KB |
Output is correct |
46 |
Correct |
79 ms |
164868 KB |
Output is correct |
47 |
Correct |
80 ms |
164996 KB |
Output is correct |
48 |
Correct |
91 ms |
164976 KB |
Output is correct |
49 |
Correct |
82 ms |
165920 KB |
Output is correct |
50 |
Correct |
174 ms |
171880 KB |
Output is correct |
51 |
Correct |
83 ms |
165276 KB |
Output is correct |
52 |
Correct |
152 ms |
170088 KB |
Output is correct |
53 |
Correct |
86 ms |
165876 KB |
Output is correct |
54 |
Correct |
162 ms |
170880 KB |
Output is correct |
55 |
Correct |
78 ms |
165360 KB |
Output is correct |
56 |
Correct |
93 ms |
165440 KB |
Output is correct |
57 |
Correct |
80 ms |
165668 KB |
Output is correct |
58 |
Correct |
78 ms |
165784 KB |
Output is correct |
59 |
Correct |
81 ms |
164848 KB |
Output is correct |
60 |
Correct |
143 ms |
168172 KB |
Output is correct |
61 |
Correct |
80 ms |
165120 KB |
Output is correct |
62 |
Correct |
171 ms |
171340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
164676 KB |
Output is correct |
2 |
Correct |
81 ms |
164684 KB |
Output is correct |
3 |
Correct |
73 ms |
164700 KB |
Output is correct |
4 |
Correct |
74 ms |
164676 KB |
Output is correct |
5 |
Correct |
79 ms |
164640 KB |
Output is correct |
6 |
Correct |
76 ms |
164752 KB |
Output is correct |
7 |
Correct |
74 ms |
164688 KB |
Output is correct |
8 |
Correct |
75 ms |
164720 KB |
Output is correct |
9 |
Correct |
81 ms |
164680 KB |
Output is correct |
10 |
Correct |
75 ms |
164624 KB |
Output is correct |
11 |
Correct |
76 ms |
164640 KB |
Output is correct |
12 |
Correct |
72 ms |
164704 KB |
Output is correct |
13 |
Correct |
73 ms |
164816 KB |
Output is correct |
14 |
Correct |
76 ms |
164688 KB |
Output is correct |
15 |
Correct |
73 ms |
164676 KB |
Output is correct |
16 |
Correct |
75 ms |
164684 KB |
Output is correct |
17 |
Correct |
72 ms |
164676 KB |
Output is correct |
18 |
Correct |
70 ms |
164664 KB |
Output is correct |
19 |
Correct |
78 ms |
164608 KB |
Output is correct |
20 |
Correct |
81 ms |
164684 KB |
Output is correct |
21 |
Correct |
72 ms |
164728 KB |
Output is correct |
22 |
Correct |
74 ms |
164628 KB |
Output is correct |
23 |
Correct |
73 ms |
164712 KB |
Output is correct |
24 |
Correct |
79 ms |
164700 KB |
Output is correct |
25 |
Correct |
75 ms |
164668 KB |
Output is correct |
26 |
Correct |
75 ms |
164688 KB |
Output is correct |
27 |
Correct |
73 ms |
164676 KB |
Output is correct |
28 |
Correct |
73 ms |
164624 KB |
Output is correct |
29 |
Correct |
73 ms |
164732 KB |
Output is correct |
30 |
Correct |
80 ms |
164684 KB |
Output is correct |
31 |
Correct |
84 ms |
164684 KB |
Output is correct |
32 |
Correct |
72 ms |
164700 KB |
Output is correct |
33 |
Correct |
73 ms |
164708 KB |
Output is correct |
34 |
Correct |
78 ms |
164812 KB |
Output is correct |
35 |
Correct |
155 ms |
169068 KB |
Output is correct |
36 |
Correct |
162 ms |
171744 KB |
Output is correct |
37 |
Correct |
182 ms |
171800 KB |
Output is correct |
38 |
Correct |
163 ms |
171372 KB |
Output is correct |
39 |
Correct |
81 ms |
164900 KB |
Output is correct |
40 |
Correct |
83 ms |
165024 KB |
Output is correct |
41 |
Correct |
75 ms |
165048 KB |
Output is correct |
42 |
Correct |
74 ms |
164848 KB |
Output is correct |
43 |
Correct |
80 ms |
165028 KB |
Output is correct |
44 |
Correct |
79 ms |
165012 KB |
Output is correct |
45 |
Correct |
76 ms |
164924 KB |
Output is correct |
46 |
Correct |
79 ms |
164868 KB |
Output is correct |
47 |
Correct |
80 ms |
164996 KB |
Output is correct |
48 |
Correct |
91 ms |
164976 KB |
Output is correct |
49 |
Correct |
82 ms |
165920 KB |
Output is correct |
50 |
Correct |
174 ms |
171880 KB |
Output is correct |
51 |
Correct |
83 ms |
165276 KB |
Output is correct |
52 |
Correct |
152 ms |
170088 KB |
Output is correct |
53 |
Correct |
86 ms |
165876 KB |
Output is correct |
54 |
Correct |
162 ms |
170880 KB |
Output is correct |
55 |
Correct |
78 ms |
165360 KB |
Output is correct |
56 |
Correct |
93 ms |
165440 KB |
Output is correct |
57 |
Correct |
80 ms |
165668 KB |
Output is correct |
58 |
Correct |
78 ms |
165784 KB |
Output is correct |
59 |
Correct |
81 ms |
164848 KB |
Output is correct |
60 |
Correct |
143 ms |
168172 KB |
Output is correct |
61 |
Correct |
80 ms |
165120 KB |
Output is correct |
62 |
Correct |
171 ms |
171340 KB |
Output is correct |
63 |
Correct |
574 ms |
214256 KB |
Output is correct |
64 |
Correct |
497 ms |
214308 KB |
Output is correct |
65 |
Correct |
494 ms |
214220 KB |
Output is correct |
66 |
Correct |
202 ms |
177360 KB |
Output is correct |
67 |
Correct |
316 ms |
183560 KB |
Output is correct |
68 |
Correct |
219 ms |
177240 KB |
Output is correct |
69 |
Correct |
346 ms |
180052 KB |
Output is correct |
70 |
Correct |
245 ms |
177284 KB |
Output is correct |
71 |
Correct |
187 ms |
177376 KB |
Output is correct |
72 |
Correct |
365 ms |
182244 KB |
Output is correct |
73 |
Correct |
422 ms |
182376 KB |
Output is correct |
74 |
Correct |
869 ms |
209344 KB |
Output is correct |
75 |
Correct |
504 ms |
185976 KB |
Output is correct |
76 |
Correct |
705 ms |
197456 KB |
Output is correct |
77 |
Correct |
691 ms |
197684 KB |
Output is correct |
78 |
Correct |
293 ms |
182292 KB |
Output is correct |
79 |
Correct |
364 ms |
184380 KB |
Output is correct |
80 |
Correct |
253 ms |
181044 KB |
Output is correct |
81 |
Correct |
347 ms |
182748 KB |
Output is correct |
82 |
Correct |
761 ms |
201284 KB |
Output is correct |
83 |
Correct |
743 ms |
201288 KB |
Output is correct |
84 |
Correct |
695 ms |
215104 KB |
Output is correct |
85 |
Correct |
681 ms |
215096 KB |
Output is correct |
86 |
Correct |
234 ms |
177100 KB |
Output is correct |
87 |
Correct |
225 ms |
178088 KB |
Output is correct |
88 |
Correct |
385 ms |
182556 KB |
Output is correct |
89 |
Correct |
696 ms |
194936 KB |
Output is correct |