#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 |
4 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
3 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10540 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10632 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
3 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10540 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10632 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
34 |
Correct |
4 ms |
10584 KB |
Output is correct |
35 |
Correct |
56 ms |
15096 KB |
Output is correct |
36 |
Correct |
71 ms |
17364 KB |
Output is correct |
37 |
Correct |
72 ms |
17488 KB |
Output is correct |
38 |
Correct |
69 ms |
17276 KB |
Output is correct |
39 |
Correct |
4 ms |
10688 KB |
Output is correct |
40 |
Correct |
5 ms |
10744 KB |
Output is correct |
41 |
Correct |
5 ms |
10644 KB |
Output is correct |
42 |
Correct |
4 ms |
10584 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10772 KB |
Output is correct |
45 |
Correct |
4 ms |
10588 KB |
Output is correct |
46 |
Correct |
4 ms |
10588 KB |
Output is correct |
47 |
Correct |
4 ms |
10776 KB |
Output is correct |
48 |
Correct |
4 ms |
10772 KB |
Output is correct |
49 |
Correct |
9 ms |
11356 KB |
Output is correct |
50 |
Correct |
73 ms |
17440 KB |
Output is correct |
51 |
Correct |
6 ms |
10840 KB |
Output is correct |
52 |
Correct |
62 ms |
16276 KB |
Output is correct |
53 |
Correct |
8 ms |
11352 KB |
Output is correct |
54 |
Correct |
68 ms |
16748 KB |
Output is correct |
55 |
Correct |
5 ms |
10840 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
11100 KB |
Output is correct |
58 |
Correct |
8 ms |
11160 KB |
Output is correct |
59 |
Correct |
4 ms |
10588 KB |
Output is correct |
60 |
Correct |
55 ms |
14468 KB |
Output is correct |
61 |
Correct |
5 ms |
10588 KB |
Output is correct |
62 |
Correct |
70 ms |
17124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
3 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10540 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10632 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
34 |
Correct |
4 ms |
10584 KB |
Output is correct |
35 |
Correct |
56 ms |
15096 KB |
Output is correct |
36 |
Correct |
71 ms |
17364 KB |
Output is correct |
37 |
Correct |
72 ms |
17488 KB |
Output is correct |
38 |
Correct |
69 ms |
17276 KB |
Output is correct |
39 |
Correct |
4 ms |
10688 KB |
Output is correct |
40 |
Correct |
5 ms |
10744 KB |
Output is correct |
41 |
Correct |
5 ms |
10644 KB |
Output is correct |
42 |
Correct |
4 ms |
10584 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10772 KB |
Output is correct |
45 |
Correct |
4 ms |
10588 KB |
Output is correct |
46 |
Correct |
4 ms |
10588 KB |
Output is correct |
47 |
Correct |
4 ms |
10776 KB |
Output is correct |
48 |
Correct |
4 ms |
10772 KB |
Output is correct |
49 |
Correct |
9 ms |
11356 KB |
Output is correct |
50 |
Correct |
73 ms |
17440 KB |
Output is correct |
51 |
Correct |
6 ms |
10840 KB |
Output is correct |
52 |
Correct |
62 ms |
16276 KB |
Output is correct |
53 |
Correct |
8 ms |
11352 KB |
Output is correct |
54 |
Correct |
68 ms |
16748 KB |
Output is correct |
55 |
Correct |
5 ms |
10840 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
11100 KB |
Output is correct |
58 |
Correct |
8 ms |
11160 KB |
Output is correct |
59 |
Correct |
4 ms |
10588 KB |
Output is correct |
60 |
Correct |
55 ms |
14468 KB |
Output is correct |
61 |
Correct |
5 ms |
10588 KB |
Output is correct |
62 |
Correct |
70 ms |
17124 KB |
Output is correct |
63 |
Correct |
253 ms |
43088 KB |
Output is correct |
64 |
Correct |
240 ms |
42732 KB |
Output is correct |
65 |
Correct |
244 ms |
42704 KB |
Output is correct |
66 |
Correct |
64 ms |
13904 KB |
Output is correct |
67 |
Correct |
102 ms |
17748 KB |
Output is correct |
68 |
Correct |
60 ms |
14104 KB |
Output is correct |
69 |
Correct |
149 ms |
18940 KB |
Output is correct |
70 |
Correct |
58 ms |
13904 KB |
Output is correct |
71 |
Correct |
64 ms |
13840 KB |
Output is correct |
72 |
Correct |
130 ms |
18284 KB |
Output is correct |
73 |
Correct |
124 ms |
17868 KB |
Output is correct |
74 |
Correct |
375 ms |
28372 KB |
Output is correct |
75 |
Correct |
228 ms |
22864 KB |
Output is correct |
76 |
Correct |
296 ms |
25936 KB |
Output is correct |
77 |
Correct |
290 ms |
26196 KB |
Output is correct |
78 |
Correct |
95 ms |
18076 KB |
Output is correct |
79 |
Correct |
174 ms |
20648 KB |
Output is correct |
80 |
Correct |
99 ms |
18140 KB |
Output is correct |
81 |
Correct |
158 ms |
20272 KB |
Output is correct |
82 |
Correct |
311 ms |
31932 KB |
Output is correct |
83 |
Correct |
288 ms |
31828 KB |
Output is correct |
84 |
Correct |
280 ms |
32384 KB |
Output is correct |
85 |
Correct |
265 ms |
32588 KB |
Output is correct |
86 |
Correct |
57 ms |
13016 KB |
Output is correct |
87 |
Correct |
97 ms |
15088 KB |
Output is correct |
88 |
Correct |
122 ms |
18000 KB |
Output is correct |
89 |
Correct |
278 ms |
23172 KB |
Output is correct |