#include<bits/stdc++.h>
#define ld long double
#define pii pair<long long,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
#define pb push_back
#define f first
#define s second
#define vi vector<ll>
#define vvi vector<vi>
#define vpii vector<pii>
#define ll long long
#define siz(x) (ll)x.size()
using namespace std;
const int N=1e5+5;
ll pr[N],sz[N];
ll ans=0;
set<int>g[N],rg[N],ch[N];
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}queue<pii>mer;
void addedge(int a,int b){
g[a].insert(b);rg[b].insert(a);
if(g[b].count(a))mer.push({a,b});
}
int gsz(int x){
return ch[x].size()+g[x].size()+rg[x].size();
}
void uni(int a,int b){
if(a==b)return;
if(gsz(a)<gsz(b))swap(a,b);
ans += siz(ch[a])*sz[b]+siz(ch[b])*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);rg[b].erase(a);g[a].erase(b);rg[a].erase(b);
for(auto it : g[b]){
addedge(a,it);
}
for(auto it : rg[b]){
addedge(it,a);
}
}
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,sz[i]=1,ch[i].insert(i);
while(m--){
int a,b;cin>>a>>b;
b=get(b);
if(a==b||ch[b].count(a)){
cout<<ans<<'\n';
continue;
}ch[b].insert(a);
ans+=sz[b];a=get(a);
addedge(a,b);
while(!mer.empty()){
uni(get(mer.front().f),get(mer.front().s));mer.pop();
}cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15000 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14936 KB |
Output is correct |
9 |
Correct |
5 ms |
14960 KB |
Output is correct |
10 |
Correct |
5 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
5 ms |
14940 KB |
Output is correct |
21 |
Correct |
5 ms |
15008 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14940 KB |
Output is correct |
24 |
Correct |
5 ms |
14936 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14936 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
5 ms |
14940 KB |
Output is correct |
29 |
Correct |
5 ms |
14936 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
6 ms |
14940 KB |
Output is correct |
32 |
Correct |
5 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15000 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14936 KB |
Output is correct |
9 |
Correct |
5 ms |
14960 KB |
Output is correct |
10 |
Correct |
5 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
5 ms |
14940 KB |
Output is correct |
21 |
Correct |
5 ms |
15008 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14940 KB |
Output is correct |
24 |
Correct |
5 ms |
14936 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14936 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
5 ms |
14940 KB |
Output is correct |
29 |
Correct |
5 ms |
14936 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
6 ms |
14940 KB |
Output is correct |
32 |
Correct |
5 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
34 |
Correct |
6 ms |
14940 KB |
Output is correct |
35 |
Correct |
114 ms |
21144 KB |
Output is correct |
36 |
Correct |
131 ms |
24420 KB |
Output is correct |
37 |
Correct |
124 ms |
24452 KB |
Output is correct |
38 |
Correct |
112 ms |
23820 KB |
Output is correct |
39 |
Correct |
7 ms |
15268 KB |
Output is correct |
40 |
Correct |
10 ms |
16220 KB |
Output is correct |
41 |
Correct |
10 ms |
15964 KB |
Output is correct |
42 |
Correct |
7 ms |
15196 KB |
Output is correct |
43 |
Correct |
8 ms |
15196 KB |
Output is correct |
44 |
Correct |
8 ms |
15368 KB |
Output is correct |
45 |
Correct |
7 ms |
15196 KB |
Output is correct |
46 |
Correct |
8 ms |
15380 KB |
Output is correct |
47 |
Correct |
8 ms |
15708 KB |
Output is correct |
48 |
Correct |
9 ms |
15708 KB |
Output is correct |
49 |
Correct |
14 ms |
16732 KB |
Output is correct |
50 |
Correct |
123 ms |
24528 KB |
Output is correct |
51 |
Correct |
14 ms |
15868 KB |
Output is correct |
52 |
Correct |
102 ms |
22356 KB |
Output is correct |
53 |
Correct |
19 ms |
16472 KB |
Output is correct |
54 |
Correct |
108 ms |
23388 KB |
Output is correct |
55 |
Correct |
10 ms |
15964 KB |
Output is correct |
56 |
Correct |
9 ms |
15960 KB |
Output is correct |
57 |
Correct |
11 ms |
15960 KB |
Output is correct |
58 |
Correct |
9 ms |
15964 KB |
Output is correct |
59 |
Correct |
7 ms |
15548 KB |
Output is correct |
60 |
Correct |
82 ms |
20660 KB |
Output is correct |
61 |
Correct |
10 ms |
15708 KB |
Output is correct |
62 |
Correct |
105 ms |
23636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15000 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14936 KB |
Output is correct |
9 |
Correct |
5 ms |
14960 KB |
Output is correct |
10 |
Correct |
5 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
5 ms |
14940 KB |
Output is correct |
21 |
Correct |
5 ms |
15008 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14940 KB |
Output is correct |
24 |
Correct |
5 ms |
14936 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14936 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
5 ms |
14940 KB |
Output is correct |
29 |
Correct |
5 ms |
14936 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
6 ms |
14940 KB |
Output is correct |
32 |
Correct |
5 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
34 |
Correct |
6 ms |
14940 KB |
Output is correct |
35 |
Correct |
114 ms |
21144 KB |
Output is correct |
36 |
Correct |
131 ms |
24420 KB |
Output is correct |
37 |
Correct |
124 ms |
24452 KB |
Output is correct |
38 |
Correct |
112 ms |
23820 KB |
Output is correct |
39 |
Correct |
7 ms |
15268 KB |
Output is correct |
40 |
Correct |
10 ms |
16220 KB |
Output is correct |
41 |
Correct |
10 ms |
15964 KB |
Output is correct |
42 |
Correct |
7 ms |
15196 KB |
Output is correct |
43 |
Correct |
8 ms |
15196 KB |
Output is correct |
44 |
Correct |
8 ms |
15368 KB |
Output is correct |
45 |
Correct |
7 ms |
15196 KB |
Output is correct |
46 |
Correct |
8 ms |
15380 KB |
Output is correct |
47 |
Correct |
8 ms |
15708 KB |
Output is correct |
48 |
Correct |
9 ms |
15708 KB |
Output is correct |
49 |
Correct |
14 ms |
16732 KB |
Output is correct |
50 |
Correct |
123 ms |
24528 KB |
Output is correct |
51 |
Correct |
14 ms |
15868 KB |
Output is correct |
52 |
Correct |
102 ms |
22356 KB |
Output is correct |
53 |
Correct |
19 ms |
16472 KB |
Output is correct |
54 |
Correct |
108 ms |
23388 KB |
Output is correct |
55 |
Correct |
10 ms |
15964 KB |
Output is correct |
56 |
Correct |
9 ms |
15960 KB |
Output is correct |
57 |
Correct |
11 ms |
15960 KB |
Output is correct |
58 |
Correct |
9 ms |
15964 KB |
Output is correct |
59 |
Correct |
7 ms |
15548 KB |
Output is correct |
60 |
Correct |
82 ms |
20660 KB |
Output is correct |
61 |
Correct |
10 ms |
15708 KB |
Output is correct |
62 |
Correct |
105 ms |
23636 KB |
Output is correct |
63 |
Correct |
742 ms |
68684 KB |
Output is correct |
64 |
Correct |
678 ms |
68548 KB |
Output is correct |
65 |
Correct |
706 ms |
68740 KB |
Output is correct |
66 |
Correct |
227 ms |
34604 KB |
Output is correct |
67 |
Correct |
902 ms |
97520 KB |
Output is correct |
68 |
Correct |
220 ms |
34644 KB |
Output is correct |
69 |
Correct |
378 ms |
35560 KB |
Output is correct |
70 |
Correct |
221 ms |
34388 KB |
Output is correct |
71 |
Correct |
197 ms |
34384 KB |
Output is correct |
72 |
Correct |
606 ms |
54964 KB |
Output is correct |
73 |
Correct |
622 ms |
57688 KB |
Output is correct |
74 |
Correct |
2011 ms |
109384 KB |
Output is correct |
75 |
Correct |
641 ms |
47172 KB |
Output is correct |
76 |
Correct |
1299 ms |
71344 KB |
Output is correct |
77 |
Correct |
1198 ms |
71792 KB |
Output is correct |
78 |
Correct |
338 ms |
41736 KB |
Output is correct |
79 |
Correct |
680 ms |
47552 KB |
Output is correct |
80 |
Correct |
294 ms |
40568 KB |
Output is correct |
81 |
Correct |
418 ms |
44588 KB |
Output is correct |
82 |
Correct |
1121 ms |
66800 KB |
Output is correct |
83 |
Correct |
1208 ms |
67120 KB |
Output is correct |
84 |
Correct |
987 ms |
69720 KB |
Output is correct |
85 |
Correct |
959 ms |
70008 KB |
Output is correct |
86 |
Correct |
373 ms |
67344 KB |
Output is correct |
87 |
Correct |
383 ms |
69456 KB |
Output is correct |
88 |
Correct |
618 ms |
56252 KB |
Output is correct |
89 |
Correct |
1163 ms |
69112 KB |
Output is correct |