#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> Out[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);
Out[a].insert(B);
ans += sz[B]*sz(In[B]);
}
skip:
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23384 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
22968 KB |
Output is correct |
7 |
Correct |
5 ms |
23244 KB |
Output is correct |
8 |
Correct |
6 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23384 KB |
Output is correct |
10 |
Correct |
5 ms |
23128 KB |
Output is correct |
11 |
Correct |
5 ms |
23200 KB |
Output is correct |
12 |
Correct |
6 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
23132 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
5 ms |
23132 KB |
Output is correct |
19 |
Correct |
5 ms |
23132 KB |
Output is correct |
20 |
Correct |
5 ms |
23132 KB |
Output is correct |
21 |
Correct |
5 ms |
23128 KB |
Output is correct |
22 |
Correct |
6 ms |
23132 KB |
Output is correct |
23 |
Correct |
5 ms |
23132 KB |
Output is correct |
24 |
Correct |
5 ms |
23128 KB |
Output is correct |
25 |
Correct |
6 ms |
23128 KB |
Output is correct |
26 |
Correct |
5 ms |
23132 KB |
Output is correct |
27 |
Correct |
5 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23128 KB |
Output is correct |
29 |
Correct |
5 ms |
23132 KB |
Output is correct |
30 |
Correct |
5 ms |
23132 KB |
Output is correct |
31 |
Correct |
5 ms |
23132 KB |
Output is correct |
32 |
Correct |
5 ms |
23132 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23384 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
22968 KB |
Output is correct |
7 |
Correct |
5 ms |
23244 KB |
Output is correct |
8 |
Correct |
6 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23384 KB |
Output is correct |
10 |
Correct |
5 ms |
23128 KB |
Output is correct |
11 |
Correct |
5 ms |
23200 KB |
Output is correct |
12 |
Correct |
6 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
23132 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
5 ms |
23132 KB |
Output is correct |
19 |
Correct |
5 ms |
23132 KB |
Output is correct |
20 |
Correct |
5 ms |
23132 KB |
Output is correct |
21 |
Correct |
5 ms |
23128 KB |
Output is correct |
22 |
Correct |
6 ms |
23132 KB |
Output is correct |
23 |
Correct |
5 ms |
23132 KB |
Output is correct |
24 |
Correct |
5 ms |
23128 KB |
Output is correct |
25 |
Correct |
6 ms |
23128 KB |
Output is correct |
26 |
Correct |
5 ms |
23132 KB |
Output is correct |
27 |
Correct |
5 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23128 KB |
Output is correct |
29 |
Correct |
5 ms |
23132 KB |
Output is correct |
30 |
Correct |
5 ms |
23132 KB |
Output is correct |
31 |
Correct |
5 ms |
23132 KB |
Output is correct |
32 |
Correct |
5 ms |
23132 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
34 |
Correct |
7 ms |
23388 KB |
Output is correct |
35 |
Correct |
60 ms |
28864 KB |
Output is correct |
36 |
Correct |
83 ms |
31816 KB |
Output is correct |
37 |
Correct |
81 ms |
31952 KB |
Output is correct |
38 |
Correct |
97 ms |
31512 KB |
Output is correct |
39 |
Correct |
8 ms |
23384 KB |
Output is correct |
40 |
Correct |
11 ms |
23644 KB |
Output is correct |
41 |
Correct |
9 ms |
23644 KB |
Output is correct |
42 |
Correct |
6 ms |
23388 KB |
Output is correct |
43 |
Correct |
7 ms |
23632 KB |
Output is correct |
44 |
Correct |
9 ms |
23388 KB |
Output is correct |
45 |
Correct |
7 ms |
23388 KB |
Output is correct |
46 |
Correct |
9 ms |
23388 KB |
Output is correct |
47 |
Correct |
11 ms |
23644 KB |
Output is correct |
48 |
Correct |
10 ms |
23644 KB |
Output is correct |
49 |
Correct |
17 ms |
24412 KB |
Output is correct |
50 |
Correct |
85 ms |
32052 KB |
Output is correct |
51 |
Correct |
12 ms |
23900 KB |
Output is correct |
52 |
Correct |
75 ms |
29780 KB |
Output is correct |
53 |
Correct |
13 ms |
24412 KB |
Output is correct |
54 |
Correct |
82 ms |
30956 KB |
Output is correct |
55 |
Correct |
9 ms |
23896 KB |
Output is correct |
56 |
Correct |
9 ms |
23900 KB |
Output is correct |
57 |
Correct |
9 ms |
24412 KB |
Output is correct |
58 |
Correct |
9 ms |
24412 KB |
Output is correct |
59 |
Correct |
10 ms |
23896 KB |
Output is correct |
60 |
Correct |
66 ms |
27668 KB |
Output is correct |
61 |
Correct |
9 ms |
23640 KB |
Output is correct |
62 |
Correct |
84 ms |
31316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23384 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
22968 KB |
Output is correct |
7 |
Correct |
5 ms |
23244 KB |
Output is correct |
8 |
Correct |
6 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23384 KB |
Output is correct |
10 |
Correct |
5 ms |
23128 KB |
Output is correct |
11 |
Correct |
5 ms |
23200 KB |
Output is correct |
12 |
Correct |
6 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
23132 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
5 ms |
23132 KB |
Output is correct |
19 |
Correct |
5 ms |
23132 KB |
Output is correct |
20 |
Correct |
5 ms |
23132 KB |
Output is correct |
21 |
Correct |
5 ms |
23128 KB |
Output is correct |
22 |
Correct |
6 ms |
23132 KB |
Output is correct |
23 |
Correct |
5 ms |
23132 KB |
Output is correct |
24 |
Correct |
5 ms |
23128 KB |
Output is correct |
25 |
Correct |
6 ms |
23128 KB |
Output is correct |
26 |
Correct |
5 ms |
23132 KB |
Output is correct |
27 |
Correct |
5 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23128 KB |
Output is correct |
29 |
Correct |
5 ms |
23132 KB |
Output is correct |
30 |
Correct |
5 ms |
23132 KB |
Output is correct |
31 |
Correct |
5 ms |
23132 KB |
Output is correct |
32 |
Correct |
5 ms |
23132 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
34 |
Correct |
7 ms |
23388 KB |
Output is correct |
35 |
Correct |
60 ms |
28864 KB |
Output is correct |
36 |
Correct |
83 ms |
31816 KB |
Output is correct |
37 |
Correct |
81 ms |
31952 KB |
Output is correct |
38 |
Correct |
97 ms |
31512 KB |
Output is correct |
39 |
Correct |
8 ms |
23384 KB |
Output is correct |
40 |
Correct |
11 ms |
23644 KB |
Output is correct |
41 |
Correct |
9 ms |
23644 KB |
Output is correct |
42 |
Correct |
6 ms |
23388 KB |
Output is correct |
43 |
Correct |
7 ms |
23632 KB |
Output is correct |
44 |
Correct |
9 ms |
23388 KB |
Output is correct |
45 |
Correct |
7 ms |
23388 KB |
Output is correct |
46 |
Correct |
9 ms |
23388 KB |
Output is correct |
47 |
Correct |
11 ms |
23644 KB |
Output is correct |
48 |
Correct |
10 ms |
23644 KB |
Output is correct |
49 |
Correct |
17 ms |
24412 KB |
Output is correct |
50 |
Correct |
85 ms |
32052 KB |
Output is correct |
51 |
Correct |
12 ms |
23900 KB |
Output is correct |
52 |
Correct |
75 ms |
29780 KB |
Output is correct |
53 |
Correct |
13 ms |
24412 KB |
Output is correct |
54 |
Correct |
82 ms |
30956 KB |
Output is correct |
55 |
Correct |
9 ms |
23896 KB |
Output is correct |
56 |
Correct |
9 ms |
23900 KB |
Output is correct |
57 |
Correct |
9 ms |
24412 KB |
Output is correct |
58 |
Correct |
9 ms |
24412 KB |
Output is correct |
59 |
Correct |
10 ms |
23896 KB |
Output is correct |
60 |
Correct |
66 ms |
27668 KB |
Output is correct |
61 |
Correct |
9 ms |
23640 KB |
Output is correct |
62 |
Correct |
84 ms |
31316 KB |
Output is correct |
63 |
Correct |
475 ms |
87464 KB |
Output is correct |
64 |
Correct |
505 ms |
87468 KB |
Output is correct |
65 |
Correct |
499 ms |
87464 KB |
Output is correct |
66 |
Correct |
162 ms |
40580 KB |
Output is correct |
67 |
Correct |
868 ms |
58436 KB |
Output is correct |
68 |
Correct |
171 ms |
40900 KB |
Output is correct |
69 |
Correct |
369 ms |
44900 KB |
Output is correct |
70 |
Correct |
186 ms |
40500 KB |
Output is correct |
71 |
Correct |
217 ms |
40576 KB |
Output is correct |
72 |
Correct |
551 ms |
51584 KB |
Output is correct |
73 |
Correct |
606 ms |
52552 KB |
Output is correct |
74 |
Correct |
1146 ms |
70148 KB |
Output is correct |
75 |
Correct |
449 ms |
51144 KB |
Output is correct |
76 |
Correct |
827 ms |
61100 KB |
Output is correct |
77 |
Correct |
768 ms |
61384 KB |
Output is correct |
78 |
Correct |
229 ms |
47836 KB |
Output is correct |
79 |
Correct |
307 ms |
51652 KB |
Output is correct |
80 |
Correct |
221 ms |
47940 KB |
Output is correct |
81 |
Correct |
397 ms |
51136 KB |
Output is correct |
82 |
Correct |
674 ms |
68296 KB |
Output is correct |
83 |
Correct |
639 ms |
68228 KB |
Output is correct |
84 |
Correct |
622 ms |
86904 KB |
Output is correct |
85 |
Correct |
606 ms |
86724 KB |
Output is correct |
86 |
Correct |
845 ms |
62528 KB |
Output is correct |
87 |
Correct |
837 ms |
64200 KB |
Output is correct |
88 |
Correct |
550 ms |
51796 KB |
Output is correct |
89 |
Correct |
691 ms |
58860 KB |
Output is correct |