#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse4")
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
int n,m;
struct dsu{
int par[maxn],sz[maxn];
set<pair<int,int>>kho[maxn];
set<int>vorod[maxn];
long long res;
dsu(){
res=0;
for(int i=1;i<maxn;i++){
par[i]=i;
sz[i]=1;
}
}
int root(int u){
if(u==par[u]){
return u;
}
return par[u]=root(par[u]);
}
void con(int u,int v){
u=root(u),v=root(v);
if(sz[u]>sz[v]){
swap(u,v);
}
vector<pair<int,int>>allq;
for(auto x:kho[u]){
res-=sz[x.first];
vorod[x.first].erase(x.second);
allq.push_back(make_pair(x.second,x.first));
}
res+=sz[u]*(2*sz[v]+vorod[v].size());
res-=sz[u]*vorod[u].size();
for(auto x:vorod[u]){
int rootx=root(x);
kho[rootx].erase(make_pair(u,x));
allq.push_back(make_pair(x,u));
}
vorod[u].clear();
kho[u].clear();
par[u]=v;
sz[v]+=sz[u];
for(auto x:allq){
add(x.first,x.second);
}
}
void add(int u,int v){
int rootv=root(v),rootu=root(u);
if(rootu==rootv||vorod[rootv].count(u)!=0){
return ;
}
auto x=kho[rootv].lower_bound(make_pair(rootu,-1));
if(x!=kho[rootv].end()&&(*x).first==rootu){
return con(u,v);
}
res+=sz[rootv];
kho[rootu].insert(make_pair(rootv,u));
vorod[rootv].insert(u);
}
}ds;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
ds.add(u,v);
cout<<ds.res<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10616 KB |
Output is correct |
8 |
Correct |
5 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10584 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10436 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10544 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10776 KB |
Output is correct |
26 |
Correct |
3 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10432 KB |
Output is correct |
31 |
Correct |
3 ms |
10584 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10616 KB |
Output is correct |
8 |
Correct |
5 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10584 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10436 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10544 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10776 KB |
Output is correct |
26 |
Correct |
3 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10432 KB |
Output is correct |
31 |
Correct |
3 ms |
10584 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10584 KB |
Output is correct |
34 |
Correct |
4 ms |
10588 KB |
Output is correct |
35 |
Correct |
54 ms |
13756 KB |
Output is correct |
36 |
Correct |
77 ms |
15768 KB |
Output is correct |
37 |
Correct |
70 ms |
15808 KB |
Output is correct |
38 |
Correct |
67 ms |
15440 KB |
Output is correct |
39 |
Correct |
3 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10588 KB |
Output is correct |
41 |
Correct |
4 ms |
10524 KB |
Output is correct |
42 |
Correct |
4 ms |
10588 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10692 KB |
Output is correct |
45 |
Correct |
4 ms |
10588 KB |
Output is correct |
46 |
Correct |
3 ms |
10584 KB |
Output is correct |
47 |
Correct |
4 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10588 KB |
Output is correct |
49 |
Correct |
8 ms |
11100 KB |
Output is correct |
50 |
Correct |
71 ms |
15772 KB |
Output is correct |
51 |
Correct |
6 ms |
10844 KB |
Output is correct |
52 |
Correct |
65 ms |
14468 KB |
Output is correct |
53 |
Correct |
8 ms |
11100 KB |
Output is correct |
54 |
Correct |
65 ms |
15056 KB |
Output is correct |
55 |
Correct |
5 ms |
10844 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
10844 KB |
Output is correct |
58 |
Correct |
5 ms |
10844 KB |
Output is correct |
59 |
Correct |
3 ms |
10588 KB |
Output is correct |
60 |
Correct |
54 ms |
12884 KB |
Output is correct |
61 |
Correct |
5 ms |
10588 KB |
Output is correct |
62 |
Correct |
67 ms |
15308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10616 KB |
Output is correct |
8 |
Correct |
5 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10584 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10436 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10544 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10776 KB |
Output is correct |
26 |
Correct |
3 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10432 KB |
Output is correct |
31 |
Correct |
3 ms |
10584 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10584 KB |
Output is correct |
34 |
Correct |
4 ms |
10588 KB |
Output is correct |
35 |
Correct |
54 ms |
13756 KB |
Output is correct |
36 |
Correct |
77 ms |
15768 KB |
Output is correct |
37 |
Correct |
70 ms |
15808 KB |
Output is correct |
38 |
Correct |
67 ms |
15440 KB |
Output is correct |
39 |
Correct |
3 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10588 KB |
Output is correct |
41 |
Correct |
4 ms |
10524 KB |
Output is correct |
42 |
Correct |
4 ms |
10588 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10692 KB |
Output is correct |
45 |
Correct |
4 ms |
10588 KB |
Output is correct |
46 |
Correct |
3 ms |
10584 KB |
Output is correct |
47 |
Correct |
4 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10588 KB |
Output is correct |
49 |
Correct |
8 ms |
11100 KB |
Output is correct |
50 |
Correct |
71 ms |
15772 KB |
Output is correct |
51 |
Correct |
6 ms |
10844 KB |
Output is correct |
52 |
Correct |
65 ms |
14468 KB |
Output is correct |
53 |
Correct |
8 ms |
11100 KB |
Output is correct |
54 |
Correct |
65 ms |
15056 KB |
Output is correct |
55 |
Correct |
5 ms |
10844 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
10844 KB |
Output is correct |
58 |
Correct |
5 ms |
10844 KB |
Output is correct |
59 |
Correct |
3 ms |
10588 KB |
Output is correct |
60 |
Correct |
54 ms |
12884 KB |
Output is correct |
61 |
Correct |
5 ms |
10588 KB |
Output is correct |
62 |
Correct |
67 ms |
15308 KB |
Output is correct |
63 |
Correct |
257 ms |
40984 KB |
Output is correct |
64 |
Correct |
235 ms |
40796 KB |
Output is correct |
65 |
Correct |
238 ms |
40640 KB |
Output is correct |
66 |
Correct |
53 ms |
12580 KB |
Output is correct |
67 |
Correct |
90 ms |
16632 KB |
Output is correct |
68 |
Correct |
59 ms |
12660 KB |
Output is correct |
69 |
Correct |
137 ms |
17488 KB |
Output is correct |
70 |
Correct |
61 ms |
12500 KB |
Output is correct |
71 |
Correct |
57 ms |
12632 KB |
Output is correct |
72 |
Correct |
110 ms |
16656 KB |
Output is correct |
73 |
Correct |
111 ms |
16940 KB |
Output is correct |
74 |
Correct |
363 ms |
25684 KB |
Output is correct |
75 |
Correct |
210 ms |
21068 KB |
Output is correct |
76 |
Correct |
263 ms |
23780 KB |
Output is correct |
77 |
Correct |
286 ms |
24028 KB |
Output is correct |
78 |
Correct |
86 ms |
16696 KB |
Output is correct |
79 |
Correct |
162 ms |
18576 KB |
Output is correct |
80 |
Correct |
98 ms |
16780 KB |
Output is correct |
81 |
Correct |
150 ms |
18324 KB |
Output is correct |
82 |
Correct |
289 ms |
30712 KB |
Output is correct |
83 |
Correct |
318 ms |
30480 KB |
Output is correct |
84 |
Correct |
302 ms |
30892 KB |
Output is correct |
85 |
Correct |
315 ms |
31316 KB |
Output is correct |
86 |
Correct |
64 ms |
11884 KB |
Output is correct |
87 |
Correct |
87 ms |
13012 KB |
Output is correct |
88 |
Correct |
157 ms |
16748 KB |
Output is correct |
89 |
Correct |
328 ms |
25416 KB |
Output is correct |