#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const int MAXN = 100'009;
int cnt[MAXN];
int p[MAXN];
int sz[MAXN];
int sum[MAXN];
vector<int> V[MAXN];
set<int> In[MAXN];
set<int> in[MAXN];
set<int> out[MAXN];
int ans;
int Find(int x) {
return x==p[x]?x:p[x]=Find(p[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if(a==b) return;
ans -= sz[a]*sz(In[a]);
ans -= sz[b]*sz(In[b]);
ans -= sz[a]*(sz[a]-1);
ans -= sz[b]*(sz[b]-1);
vector<int> union_queue;
if(sz(out[a])+sz(in[a])<sz(out[b])+sz(in[b])) swap(a, b);
for(int x:out[b]) {
if(x==a) continue;
if(in[a].count(x)) {
union_queue.pb(x);
}
out[a].insert(x);
in[x].erase(b);
in[x].insert(a);
}
for(int x:In[b]) {
if(Find(x)!=a) {
In[a].insert(x);
}
}
In[b].clear();
for(int x:V[b]) {
In[a].erase(x);
V[a].pb(x);
}
V[b].clear();
out[b].clear();
for(int x:in[b]) {
if(x==a) continue;
if(out[a].count(x)) {
union_queue.pb(x);
}
in[a].insert(x);
out[x].erase(b);
out[x].insert(a);
}
in[b].clear();
p[b] = a;
sz[a] += sz[b];
ans += sz[a]*(sz[a]-1);
ans += sz[a]*sz(In[a]);
for(int x:union_queue) {
Union(a, x);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
iota(p, p+n+1, 0);
fill(sz, sz+n+1, 1);
for(int i=1;i<=n;i++) {
V[i].pb(i);
}
for(int i=0;i<m;i++) {
int a, b;
cin >> a >> b;
int A = Find(a);
int B = Find(b);
if(A==B) goto skip;
out[A].insert(B);
if(in[A].count(B)) {
Union(A, B);
} else {
ans -= sz[B]*sz(In[B]);
in[B].insert(A);
In[B].insert(a);
ans += sz[B]*sz(In[B]);
}
skip:
cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19056 KB |
Output is correct |
5 |
Correct |
4 ms |
19096 KB |
Output is correct |
6 |
Correct |
4 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
19036 KB |
Output is correct |
8 |
Correct |
5 ms |
19036 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
19036 KB |
Output is correct |
11 |
Correct |
5 ms |
19036 KB |
Output is correct |
12 |
Correct |
5 ms |
19036 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
4 ms |
19036 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
4 ms |
19036 KB |
Output is correct |
18 |
Correct |
4 ms |
19036 KB |
Output is correct |
19 |
Correct |
4 ms |
19032 KB |
Output is correct |
20 |
Correct |
5 ms |
19036 KB |
Output is correct |
21 |
Correct |
4 ms |
19036 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
4 ms |
19116 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
4 ms |
19036 KB |
Output is correct |
27 |
Correct |
4 ms |
19036 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
4 ms |
19032 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
18884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19056 KB |
Output is correct |
5 |
Correct |
4 ms |
19096 KB |
Output is correct |
6 |
Correct |
4 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
19036 KB |
Output is correct |
8 |
Correct |
5 ms |
19036 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
19036 KB |
Output is correct |
11 |
Correct |
5 ms |
19036 KB |
Output is correct |
12 |
Correct |
5 ms |
19036 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
4 ms |
19036 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
4 ms |
19036 KB |
Output is correct |
18 |
Correct |
4 ms |
19036 KB |
Output is correct |
19 |
Correct |
4 ms |
19032 KB |
Output is correct |
20 |
Correct |
5 ms |
19036 KB |
Output is correct |
21 |
Correct |
4 ms |
19036 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
4 ms |
19116 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
4 ms |
19036 KB |
Output is correct |
27 |
Correct |
4 ms |
19036 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
4 ms |
19032 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
18884 KB |
Output is correct |
34 |
Correct |
6 ms |
19032 KB |
Output is correct |
35 |
Correct |
55 ms |
22608 KB |
Output is correct |
36 |
Correct |
73 ms |
25172 KB |
Output is correct |
37 |
Correct |
73 ms |
25168 KB |
Output is correct |
38 |
Correct |
74 ms |
24916 KB |
Output is correct |
39 |
Correct |
6 ms |
19288 KB |
Output is correct |
40 |
Correct |
8 ms |
19548 KB |
Output is correct |
41 |
Correct |
10 ms |
19556 KB |
Output is correct |
42 |
Correct |
5 ms |
19292 KB |
Output is correct |
43 |
Correct |
6 ms |
19288 KB |
Output is correct |
44 |
Correct |
7 ms |
19292 KB |
Output is correct |
45 |
Correct |
5 ms |
19292 KB |
Output is correct |
46 |
Correct |
5 ms |
19292 KB |
Output is correct |
47 |
Correct |
7 ms |
19300 KB |
Output is correct |
48 |
Correct |
7 ms |
19292 KB |
Output is correct |
49 |
Correct |
11 ms |
19940 KB |
Output is correct |
50 |
Correct |
76 ms |
25148 KB |
Output is correct |
51 |
Correct |
8 ms |
19552 KB |
Output is correct |
52 |
Correct |
72 ms |
23880 KB |
Output is correct |
53 |
Correct |
11 ms |
20260 KB |
Output is correct |
54 |
Correct |
70 ms |
24428 KB |
Output is correct |
55 |
Correct |
7 ms |
19548 KB |
Output is correct |
56 |
Correct |
8 ms |
19780 KB |
Output is correct |
57 |
Correct |
8 ms |
20152 KB |
Output is correct |
58 |
Correct |
9 ms |
20060 KB |
Output is correct |
59 |
Correct |
9 ms |
19548 KB |
Output is correct |
60 |
Correct |
63 ms |
22008 KB |
Output is correct |
61 |
Correct |
8 ms |
19292 KB |
Output is correct |
62 |
Correct |
88 ms |
24548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19056 KB |
Output is correct |
5 |
Correct |
4 ms |
19096 KB |
Output is correct |
6 |
Correct |
4 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
19036 KB |
Output is correct |
8 |
Correct |
5 ms |
19036 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
19036 KB |
Output is correct |
11 |
Correct |
5 ms |
19036 KB |
Output is correct |
12 |
Correct |
5 ms |
19036 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
4 ms |
19036 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
4 ms |
19036 KB |
Output is correct |
18 |
Correct |
4 ms |
19036 KB |
Output is correct |
19 |
Correct |
4 ms |
19032 KB |
Output is correct |
20 |
Correct |
5 ms |
19036 KB |
Output is correct |
21 |
Correct |
4 ms |
19036 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
4 ms |
19116 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
4 ms |
19036 KB |
Output is correct |
27 |
Correct |
4 ms |
19036 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
4 ms |
19032 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
18884 KB |
Output is correct |
34 |
Correct |
6 ms |
19032 KB |
Output is correct |
35 |
Correct |
55 ms |
22608 KB |
Output is correct |
36 |
Correct |
73 ms |
25172 KB |
Output is correct |
37 |
Correct |
73 ms |
25168 KB |
Output is correct |
38 |
Correct |
74 ms |
24916 KB |
Output is correct |
39 |
Correct |
6 ms |
19288 KB |
Output is correct |
40 |
Correct |
8 ms |
19548 KB |
Output is correct |
41 |
Correct |
10 ms |
19556 KB |
Output is correct |
42 |
Correct |
5 ms |
19292 KB |
Output is correct |
43 |
Correct |
6 ms |
19288 KB |
Output is correct |
44 |
Correct |
7 ms |
19292 KB |
Output is correct |
45 |
Correct |
5 ms |
19292 KB |
Output is correct |
46 |
Correct |
5 ms |
19292 KB |
Output is correct |
47 |
Correct |
7 ms |
19300 KB |
Output is correct |
48 |
Correct |
7 ms |
19292 KB |
Output is correct |
49 |
Correct |
11 ms |
19940 KB |
Output is correct |
50 |
Correct |
76 ms |
25148 KB |
Output is correct |
51 |
Correct |
8 ms |
19552 KB |
Output is correct |
52 |
Correct |
72 ms |
23880 KB |
Output is correct |
53 |
Correct |
11 ms |
20260 KB |
Output is correct |
54 |
Correct |
70 ms |
24428 KB |
Output is correct |
55 |
Correct |
7 ms |
19548 KB |
Output is correct |
56 |
Correct |
8 ms |
19780 KB |
Output is correct |
57 |
Correct |
8 ms |
20152 KB |
Output is correct |
58 |
Correct |
9 ms |
20060 KB |
Output is correct |
59 |
Correct |
9 ms |
19548 KB |
Output is correct |
60 |
Correct |
63 ms |
22008 KB |
Output is correct |
61 |
Correct |
8 ms |
19292 KB |
Output is correct |
62 |
Correct |
88 ms |
24548 KB |
Output is correct |
63 |
Correct |
382 ms |
66632 KB |
Output is correct |
64 |
Correct |
383 ms |
66388 KB |
Output is correct |
65 |
Correct |
397 ms |
66668 KB |
Output is correct |
66 |
Correct |
125 ms |
29888 KB |
Output is correct |
67 |
Correct |
671 ms |
47748 KB |
Output is correct |
68 |
Correct |
126 ms |
29888 KB |
Output is correct |
69 |
Correct |
245 ms |
34820 KB |
Output is correct |
70 |
Correct |
121 ms |
29900 KB |
Output is correct |
71 |
Correct |
122 ms |
29848 KB |
Output is correct |
72 |
Correct |
471 ms |
41268 KB |
Output is correct |
73 |
Correct |
500 ms |
41928 KB |
Output is correct |
74 |
Correct |
896 ms |
54596 KB |
Output is correct |
75 |
Correct |
350 ms |
38848 KB |
Output is correct |
76 |
Correct |
615 ms |
46960 KB |
Output is correct |
77 |
Correct |
611 ms |
47300 KB |
Output is correct |
78 |
Correct |
167 ms |
35784 KB |
Output is correct |
79 |
Correct |
249 ms |
38116 KB |
Output is correct |
80 |
Correct |
160 ms |
35976 KB |
Output is correct |
81 |
Correct |
255 ms |
37960 KB |
Output is correct |
82 |
Correct |
520 ms |
52864 KB |
Output is correct |
83 |
Correct |
521 ms |
52900 KB |
Output is correct |
84 |
Correct |
500 ms |
71432 KB |
Output is correct |
85 |
Correct |
522 ms |
71452 KB |
Output is correct |
86 |
Correct |
689 ms |
51976 KB |
Output is correct |
87 |
Correct |
743 ms |
53172 KB |
Output is correct |
88 |
Correct |
463 ms |
41188 KB |
Output is correct |
89 |
Correct |
566 ms |
45100 KB |
Output is correct |