#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
using namespace std;
const int mxn=1e5+5;
set<int>ch[mxn],g[mxn],gr[mxn];
ll sz[mxn]{0},pr[mxn]{0};
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}queue<pii>qr;
void add(int a,int b){
if(a==b)return;
g[a].insert(b);
gr[b].insert(a);
if(g[b].count(a))qr.push({a,b});
}
ll rs=0;
void uni(int a,int b){
a=get(a),b=get(b);
if(a==b)return;
if(ch[a].size()+gr[a].size()+g[a].size()>ch[b].size()+gr[b].size()+g[b].size())swap(a,b);
rs += ((ll)ch[a].size())*sz[b] + ((ll)ch[b].size()*sz[a]);
pr[a]=b;sz[b]+=sz[a];
for(auto it : ch[a]){
if(ch[b].count(it))rs-=sz[b];
else {
ch[b].insert(it);
}
}g[a].erase(b);gr[a].erase(b);gr[b].erase(a);g[b].erase(a);
for(auto it : g[a]){
add(b,it);
}
for(auto it : gr[a]){
add(it,b);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)sz[i]=1,ch[i].insert(i),pr[i]=i;
while(m--){
int a,b;cin>>a>>b;b=get(b);
if(a==b||ch[b].count(a)){
cout<<rs<<'\n';continue;
}rs+=sz[b];ch[b].insert(a);a=get(a);add(a,b);
while(!qr.empty()){
uni(qr.front().f,qr.front().s);qr.pop();
}cout<<rs<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15696 KB |
Output is correct |
2 |
Correct |
4 ms |
15696 KB |
Output is correct |
3 |
Correct |
4 ms |
15952 KB |
Output is correct |
4 |
Correct |
3 ms |
15784 KB |
Output is correct |
5 |
Correct |
4 ms |
15952 KB |
Output is correct |
6 |
Correct |
3 ms |
15696 KB |
Output is correct |
7 |
Correct |
4 ms |
15952 KB |
Output is correct |
8 |
Correct |
4 ms |
15952 KB |
Output is correct |
9 |
Correct |
4 ms |
15952 KB |
Output is correct |
10 |
Correct |
3 ms |
15908 KB |
Output is correct |
11 |
Correct |
4 ms |
15952 KB |
Output is correct |
12 |
Correct |
3 ms |
15952 KB |
Output is correct |
13 |
Correct |
4 ms |
15952 KB |
Output is correct |
14 |
Correct |
4 ms |
15952 KB |
Output is correct |
15 |
Correct |
4 ms |
15952 KB |
Output is correct |
16 |
Correct |
3 ms |
15952 KB |
Output is correct |
17 |
Correct |
3 ms |
15952 KB |
Output is correct |
18 |
Correct |
3 ms |
15952 KB |
Output is correct |
19 |
Correct |
4 ms |
15952 KB |
Output is correct |
20 |
Correct |
3 ms |
15808 KB |
Output is correct |
21 |
Correct |
4 ms |
15952 KB |
Output is correct |
22 |
Correct |
3 ms |
15952 KB |
Output is correct |
23 |
Correct |
3 ms |
15952 KB |
Output is correct |
24 |
Correct |
3 ms |
15952 KB |
Output is correct |
25 |
Correct |
3 ms |
15952 KB |
Output is correct |
26 |
Correct |
3 ms |
15952 KB |
Output is correct |
27 |
Correct |
3 ms |
15976 KB |
Output is correct |
28 |
Correct |
3 ms |
15952 KB |
Output is correct |
29 |
Correct |
3 ms |
15952 KB |
Output is correct |
30 |
Correct |
3 ms |
15952 KB |
Output is correct |
31 |
Correct |
3 ms |
15952 KB |
Output is correct |
32 |
Correct |
3 ms |
15952 KB |
Output is correct |
33 |
Correct |
4 ms |
15952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15696 KB |
Output is correct |
2 |
Correct |
4 ms |
15696 KB |
Output is correct |
3 |
Correct |
4 ms |
15952 KB |
Output is correct |
4 |
Correct |
3 ms |
15784 KB |
Output is correct |
5 |
Correct |
4 ms |
15952 KB |
Output is correct |
6 |
Correct |
3 ms |
15696 KB |
Output is correct |
7 |
Correct |
4 ms |
15952 KB |
Output is correct |
8 |
Correct |
4 ms |
15952 KB |
Output is correct |
9 |
Correct |
4 ms |
15952 KB |
Output is correct |
10 |
Correct |
3 ms |
15908 KB |
Output is correct |
11 |
Correct |
4 ms |
15952 KB |
Output is correct |
12 |
Correct |
3 ms |
15952 KB |
Output is correct |
13 |
Correct |
4 ms |
15952 KB |
Output is correct |
14 |
Correct |
4 ms |
15952 KB |
Output is correct |
15 |
Correct |
4 ms |
15952 KB |
Output is correct |
16 |
Correct |
3 ms |
15952 KB |
Output is correct |
17 |
Correct |
3 ms |
15952 KB |
Output is correct |
18 |
Correct |
3 ms |
15952 KB |
Output is correct |
19 |
Correct |
4 ms |
15952 KB |
Output is correct |
20 |
Correct |
3 ms |
15808 KB |
Output is correct |
21 |
Correct |
4 ms |
15952 KB |
Output is correct |
22 |
Correct |
3 ms |
15952 KB |
Output is correct |
23 |
Correct |
3 ms |
15952 KB |
Output is correct |
24 |
Correct |
3 ms |
15952 KB |
Output is correct |
25 |
Correct |
3 ms |
15952 KB |
Output is correct |
26 |
Correct |
3 ms |
15952 KB |
Output is correct |
27 |
Correct |
3 ms |
15976 KB |
Output is correct |
28 |
Correct |
3 ms |
15952 KB |
Output is correct |
29 |
Correct |
3 ms |
15952 KB |
Output is correct |
30 |
Correct |
3 ms |
15952 KB |
Output is correct |
31 |
Correct |
3 ms |
15952 KB |
Output is correct |
32 |
Correct |
3 ms |
15952 KB |
Output is correct |
33 |
Correct |
4 ms |
15952 KB |
Output is correct |
34 |
Correct |
5 ms |
15952 KB |
Output is correct |
35 |
Correct |
72 ms |
19660 KB |
Output is correct |
36 |
Correct |
109 ms |
25172 KB |
Output is correct |
37 |
Correct |
97 ms |
22600 KB |
Output is correct |
38 |
Correct |
95 ms |
24652 KB |
Output is correct |
39 |
Correct |
6 ms |
16376 KB |
Output is correct |
40 |
Correct |
8 ms |
16976 KB |
Output is correct |
41 |
Correct |
7 ms |
16984 KB |
Output is correct |
42 |
Correct |
4 ms |
16208 KB |
Output is correct |
43 |
Correct |
5 ms |
16172 KB |
Output is correct |
44 |
Correct |
6 ms |
16208 KB |
Output is correct |
45 |
Correct |
5 ms |
16172 KB |
Output is correct |
46 |
Correct |
5 ms |
16208 KB |
Output is correct |
47 |
Correct |
7 ms |
16632 KB |
Output is correct |
48 |
Correct |
7 ms |
16464 KB |
Output is correct |
49 |
Correct |
15 ms |
17744 KB |
Output is correct |
50 |
Correct |
101 ms |
22604 KB |
Output is correct |
51 |
Correct |
10 ms |
16732 KB |
Output is correct |
52 |
Correct |
89 ms |
23020 KB |
Output is correct |
53 |
Correct |
14 ms |
17460 KB |
Output is correct |
54 |
Correct |
104 ms |
21692 KB |
Output is correct |
55 |
Correct |
9 ms |
16720 KB |
Output is correct |
56 |
Correct |
9 ms |
16720 KB |
Output is correct |
57 |
Correct |
9 ms |
16976 KB |
Output is correct |
58 |
Correct |
9 ms |
16992 KB |
Output is correct |
59 |
Correct |
7 ms |
16720 KB |
Output is correct |
60 |
Correct |
69 ms |
19016 KB |
Output is correct |
61 |
Correct |
8 ms |
16720 KB |
Output is correct |
62 |
Correct |
102 ms |
21816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15696 KB |
Output is correct |
2 |
Correct |
4 ms |
15696 KB |
Output is correct |
3 |
Correct |
4 ms |
15952 KB |
Output is correct |
4 |
Correct |
3 ms |
15784 KB |
Output is correct |
5 |
Correct |
4 ms |
15952 KB |
Output is correct |
6 |
Correct |
3 ms |
15696 KB |
Output is correct |
7 |
Correct |
4 ms |
15952 KB |
Output is correct |
8 |
Correct |
4 ms |
15952 KB |
Output is correct |
9 |
Correct |
4 ms |
15952 KB |
Output is correct |
10 |
Correct |
3 ms |
15908 KB |
Output is correct |
11 |
Correct |
4 ms |
15952 KB |
Output is correct |
12 |
Correct |
3 ms |
15952 KB |
Output is correct |
13 |
Correct |
4 ms |
15952 KB |
Output is correct |
14 |
Correct |
4 ms |
15952 KB |
Output is correct |
15 |
Correct |
4 ms |
15952 KB |
Output is correct |
16 |
Correct |
3 ms |
15952 KB |
Output is correct |
17 |
Correct |
3 ms |
15952 KB |
Output is correct |
18 |
Correct |
3 ms |
15952 KB |
Output is correct |
19 |
Correct |
4 ms |
15952 KB |
Output is correct |
20 |
Correct |
3 ms |
15808 KB |
Output is correct |
21 |
Correct |
4 ms |
15952 KB |
Output is correct |
22 |
Correct |
3 ms |
15952 KB |
Output is correct |
23 |
Correct |
3 ms |
15952 KB |
Output is correct |
24 |
Correct |
3 ms |
15952 KB |
Output is correct |
25 |
Correct |
3 ms |
15952 KB |
Output is correct |
26 |
Correct |
3 ms |
15952 KB |
Output is correct |
27 |
Correct |
3 ms |
15976 KB |
Output is correct |
28 |
Correct |
3 ms |
15952 KB |
Output is correct |
29 |
Correct |
3 ms |
15952 KB |
Output is correct |
30 |
Correct |
3 ms |
15952 KB |
Output is correct |
31 |
Correct |
3 ms |
15952 KB |
Output is correct |
32 |
Correct |
3 ms |
15952 KB |
Output is correct |
33 |
Correct |
4 ms |
15952 KB |
Output is correct |
34 |
Correct |
5 ms |
15952 KB |
Output is correct |
35 |
Correct |
72 ms |
19660 KB |
Output is correct |
36 |
Correct |
109 ms |
25172 KB |
Output is correct |
37 |
Correct |
97 ms |
22600 KB |
Output is correct |
38 |
Correct |
95 ms |
24652 KB |
Output is correct |
39 |
Correct |
6 ms |
16376 KB |
Output is correct |
40 |
Correct |
8 ms |
16976 KB |
Output is correct |
41 |
Correct |
7 ms |
16984 KB |
Output is correct |
42 |
Correct |
4 ms |
16208 KB |
Output is correct |
43 |
Correct |
5 ms |
16172 KB |
Output is correct |
44 |
Correct |
6 ms |
16208 KB |
Output is correct |
45 |
Correct |
5 ms |
16172 KB |
Output is correct |
46 |
Correct |
5 ms |
16208 KB |
Output is correct |
47 |
Correct |
7 ms |
16632 KB |
Output is correct |
48 |
Correct |
7 ms |
16464 KB |
Output is correct |
49 |
Correct |
15 ms |
17744 KB |
Output is correct |
50 |
Correct |
101 ms |
22604 KB |
Output is correct |
51 |
Correct |
10 ms |
16732 KB |
Output is correct |
52 |
Correct |
89 ms |
23020 KB |
Output is correct |
53 |
Correct |
14 ms |
17460 KB |
Output is correct |
54 |
Correct |
104 ms |
21692 KB |
Output is correct |
55 |
Correct |
9 ms |
16720 KB |
Output is correct |
56 |
Correct |
9 ms |
16720 KB |
Output is correct |
57 |
Correct |
9 ms |
16976 KB |
Output is correct |
58 |
Correct |
9 ms |
16992 KB |
Output is correct |
59 |
Correct |
7 ms |
16720 KB |
Output is correct |
60 |
Correct |
69 ms |
19016 KB |
Output is correct |
61 |
Correct |
8 ms |
16720 KB |
Output is correct |
62 |
Correct |
102 ms |
21816 KB |
Output is correct |
63 |
Correct |
382 ms |
65028 KB |
Output is correct |
64 |
Correct |
406 ms |
67912 KB |
Output is correct |
65 |
Correct |
351 ms |
64840 KB |
Output is correct |
66 |
Correct |
159 ms |
34196 KB |
Output is correct |
67 |
Correct |
546 ms |
95168 KB |
Output is correct |
68 |
Correct |
144 ms |
33608 KB |
Output is correct |
69 |
Correct |
273 ms |
35588 KB |
Output is correct |
70 |
Correct |
156 ms |
34376 KB |
Output is correct |
71 |
Correct |
179 ms |
34376 KB |
Output is correct |
72 |
Correct |
380 ms |
54600 KB |
Output is correct |
73 |
Correct |
366 ms |
57468 KB |
Output is correct |
74 |
Correct |
1183 ms |
104520 KB |
Output is correct |
75 |
Correct |
505 ms |
46920 KB |
Output is correct |
76 |
Correct |
682 ms |
70000 KB |
Output is correct |
77 |
Correct |
710 ms |
70216 KB |
Output is correct |
78 |
Correct |
184 ms |
41544 KB |
Output is correct |
79 |
Correct |
321 ms |
46920 KB |
Output is correct |
80 |
Correct |
184 ms |
40008 KB |
Output is correct |
81 |
Correct |
325 ms |
44284 KB |
Output is correct |
82 |
Correct |
709 ms |
66704 KB |
Output is correct |
83 |
Correct |
700 ms |
65608 KB |
Output is correct |
84 |
Correct |
604 ms |
68764 KB |
Output is correct |
85 |
Correct |
613 ms |
69492 KB |
Output is correct |
86 |
Correct |
299 ms |
66372 KB |
Output is correct |
87 |
Correct |
268 ms |
69448 KB |
Output is correct |
88 |
Correct |
386 ms |
56136 KB |
Output is correct |
89 |
Correct |
694 ms |
67400 KB |
Output is correct |