#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<ll,ll>
#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>g[mxn],gr[mxn],ch[mxn];
int pr[mxn];
ll sz[mxn]{0};
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}queue<pii>qr;ll ans=0;
void ins(int a,int b){
g[a].insert(b);gr[b].insert(a);
if(g[b].count(a))qr.push({a,b});
}
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()+g[b].size()+gr[b].size())swap(a,b);
ans+=(ll)ch[a].size()*sz[b]+(ll)ch[b].size()*sz[a];
pr[b]=a;sz[a]+=sz[b];
for(auto it : ch[b]){
if(ch[a].count(it))ans-=sz[a];
else ch[a].insert(it);
}g[b].erase(a);gr[b].erase(a);g[a].erase(b);gr [a].erase(b);
for(auto it : gr[b])ins(it,a);
for(auto it : g[b])ins(a,it);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)pr[i]=i,ch[i].insert(i),sz[i]=1;
for(int i=1;i<=m;i++){
int a,b;cin>>a>>b;b=get(b);
if(a==b||ch[b].count(a)){cout<<ans<<'\n';continue;}
ch[b].insert(a);a=get(a);ins(a,b);ans+=sz[b];
while(!qr.empty())uni(qr.front().f,qr.front().s),qr.pop();
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14560 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14596 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14472 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
5 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
4 ms |
14428 KB |
Output is correct |
14 |
Correct |
5 ms |
14520 KB |
Output is correct |
15 |
Correct |
5 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14540 KB |
Output is correct |
17 |
Correct |
5 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14424 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
5 ms |
14428 KB |
Output is correct |
21 |
Correct |
6 ms |
14428 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
5 ms |
14428 KB |
Output is correct |
24 |
Correct |
5 ms |
14428 KB |
Output is correct |
25 |
Correct |
5 ms |
14428 KB |
Output is correct |
26 |
Correct |
5 ms |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14428 KB |
Output is correct |
28 |
Correct |
5 ms |
14428 KB |
Output is correct |
29 |
Correct |
5 ms |
14384 KB |
Output is correct |
30 |
Correct |
5 ms |
14428 KB |
Output is correct |
31 |
Correct |
5 ms |
14428 KB |
Output is correct |
32 |
Correct |
6 ms |
14560 KB |
Output is correct |
33 |
Correct |
5 ms |
14572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14560 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14596 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14472 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
5 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
4 ms |
14428 KB |
Output is correct |
14 |
Correct |
5 ms |
14520 KB |
Output is correct |
15 |
Correct |
5 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14540 KB |
Output is correct |
17 |
Correct |
5 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14424 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
5 ms |
14428 KB |
Output is correct |
21 |
Correct |
6 ms |
14428 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
5 ms |
14428 KB |
Output is correct |
24 |
Correct |
5 ms |
14428 KB |
Output is correct |
25 |
Correct |
5 ms |
14428 KB |
Output is correct |
26 |
Correct |
5 ms |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14428 KB |
Output is correct |
28 |
Correct |
5 ms |
14428 KB |
Output is correct |
29 |
Correct |
5 ms |
14384 KB |
Output is correct |
30 |
Correct |
5 ms |
14428 KB |
Output is correct |
31 |
Correct |
5 ms |
14428 KB |
Output is correct |
32 |
Correct |
6 ms |
14560 KB |
Output is correct |
33 |
Correct |
5 ms |
14572 KB |
Output is correct |
34 |
Correct |
7 ms |
14684 KB |
Output is correct |
35 |
Correct |
61 ms |
20632 KB |
Output is correct |
36 |
Correct |
85 ms |
23892 KB |
Output is correct |
37 |
Correct |
83 ms |
23892 KB |
Output is correct |
38 |
Correct |
84 ms |
23492 KB |
Output is correct |
39 |
Correct |
6 ms |
14684 KB |
Output is correct |
40 |
Correct |
11 ms |
15708 KB |
Output is correct |
41 |
Correct |
9 ms |
15732 KB |
Output is correct |
42 |
Correct |
6 ms |
14680 KB |
Output is correct |
43 |
Correct |
7 ms |
14888 KB |
Output is correct |
44 |
Correct |
6 ms |
14940 KB |
Output is correct |
45 |
Correct |
7 ms |
14940 KB |
Output is correct |
46 |
Correct |
6 ms |
14684 KB |
Output is correct |
47 |
Correct |
8 ms |
15256 KB |
Output is correct |
48 |
Correct |
8 ms |
15192 KB |
Output is correct |
49 |
Correct |
13 ms |
16216 KB |
Output is correct |
50 |
Correct |
83 ms |
24144 KB |
Output is correct |
51 |
Correct |
10 ms |
15452 KB |
Output is correct |
52 |
Correct |
73 ms |
21952 KB |
Output is correct |
53 |
Correct |
15 ms |
15964 KB |
Output is correct |
54 |
Correct |
77 ms |
23052 KB |
Output is correct |
55 |
Correct |
10 ms |
15452 KB |
Output is correct |
56 |
Correct |
10 ms |
15452 KB |
Output is correct |
57 |
Correct |
9 ms |
15448 KB |
Output is correct |
58 |
Correct |
9 ms |
15452 KB |
Output is correct |
59 |
Correct |
7 ms |
15196 KB |
Output is correct |
60 |
Correct |
60 ms |
20052 KB |
Output is correct |
61 |
Correct |
9 ms |
15196 KB |
Output is correct |
62 |
Correct |
83 ms |
23216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14560 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14596 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14472 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
5 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
4 ms |
14428 KB |
Output is correct |
14 |
Correct |
5 ms |
14520 KB |
Output is correct |
15 |
Correct |
5 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14540 KB |
Output is correct |
17 |
Correct |
5 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14424 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
5 ms |
14428 KB |
Output is correct |
21 |
Correct |
6 ms |
14428 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
5 ms |
14428 KB |
Output is correct |
24 |
Correct |
5 ms |
14428 KB |
Output is correct |
25 |
Correct |
5 ms |
14428 KB |
Output is correct |
26 |
Correct |
5 ms |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14428 KB |
Output is correct |
28 |
Correct |
5 ms |
14428 KB |
Output is correct |
29 |
Correct |
5 ms |
14384 KB |
Output is correct |
30 |
Correct |
5 ms |
14428 KB |
Output is correct |
31 |
Correct |
5 ms |
14428 KB |
Output is correct |
32 |
Correct |
6 ms |
14560 KB |
Output is correct |
33 |
Correct |
5 ms |
14572 KB |
Output is correct |
34 |
Correct |
7 ms |
14684 KB |
Output is correct |
35 |
Correct |
61 ms |
20632 KB |
Output is correct |
36 |
Correct |
85 ms |
23892 KB |
Output is correct |
37 |
Correct |
83 ms |
23892 KB |
Output is correct |
38 |
Correct |
84 ms |
23492 KB |
Output is correct |
39 |
Correct |
6 ms |
14684 KB |
Output is correct |
40 |
Correct |
11 ms |
15708 KB |
Output is correct |
41 |
Correct |
9 ms |
15732 KB |
Output is correct |
42 |
Correct |
6 ms |
14680 KB |
Output is correct |
43 |
Correct |
7 ms |
14888 KB |
Output is correct |
44 |
Correct |
6 ms |
14940 KB |
Output is correct |
45 |
Correct |
7 ms |
14940 KB |
Output is correct |
46 |
Correct |
6 ms |
14684 KB |
Output is correct |
47 |
Correct |
8 ms |
15256 KB |
Output is correct |
48 |
Correct |
8 ms |
15192 KB |
Output is correct |
49 |
Correct |
13 ms |
16216 KB |
Output is correct |
50 |
Correct |
83 ms |
24144 KB |
Output is correct |
51 |
Correct |
10 ms |
15452 KB |
Output is correct |
52 |
Correct |
73 ms |
21952 KB |
Output is correct |
53 |
Correct |
15 ms |
15964 KB |
Output is correct |
54 |
Correct |
77 ms |
23052 KB |
Output is correct |
55 |
Correct |
10 ms |
15452 KB |
Output is correct |
56 |
Correct |
10 ms |
15452 KB |
Output is correct |
57 |
Correct |
9 ms |
15448 KB |
Output is correct |
58 |
Correct |
9 ms |
15452 KB |
Output is correct |
59 |
Correct |
7 ms |
15196 KB |
Output is correct |
60 |
Correct |
60 ms |
20052 KB |
Output is correct |
61 |
Correct |
9 ms |
15196 KB |
Output is correct |
62 |
Correct |
83 ms |
23216 KB |
Output is correct |
63 |
Correct |
318 ms |
68156 KB |
Output is correct |
64 |
Correct |
329 ms |
68136 KB |
Output is correct |
65 |
Correct |
313 ms |
67920 KB |
Output is correct |
66 |
Correct |
127 ms |
34132 KB |
Output is correct |
67 |
Correct |
458 ms |
97104 KB |
Output is correct |
68 |
Correct |
123 ms |
34132 KB |
Output is correct |
69 |
Correct |
232 ms |
35220 KB |
Output is correct |
70 |
Correct |
156 ms |
34128 KB |
Output is correct |
71 |
Correct |
134 ms |
34132 KB |
Output is correct |
72 |
Correct |
304 ms |
54608 KB |
Output is correct |
73 |
Correct |
321 ms |
57424 KB |
Output is correct |
74 |
Correct |
962 ms |
109140 KB |
Output is correct |
75 |
Correct |
354 ms |
46420 KB |
Output is correct |
76 |
Correct |
578 ms |
70968 KB |
Output is correct |
77 |
Correct |
612 ms |
71204 KB |
Output is correct |
78 |
Correct |
140 ms |
41300 KB |
Output is correct |
79 |
Correct |
278 ms |
46932 KB |
Output is correct |
80 |
Correct |
129 ms |
40156 KB |
Output is correct |
81 |
Correct |
234 ms |
44144 KB |
Output is correct |
82 |
Correct |
585 ms |
66260 KB |
Output is correct |
83 |
Correct |
566 ms |
66604 KB |
Output is correct |
84 |
Correct |
471 ms |
69200 KB |
Output is correct |
85 |
Correct |
490 ms |
69352 KB |
Output is correct |
86 |
Correct |
206 ms |
66928 KB |
Output is correct |
87 |
Correct |
222 ms |
69024 KB |
Output is correct |
88 |
Correct |
345 ms |
55892 KB |
Output is correct |
89 |
Correct |
611 ms |
68492 KB |
Output is correct |