#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using ll=long long;
const ll mod=1000000007;
const int N=100005;
int n,m;
struct UF{
int p[N];
ll ans=0,sz[N];
set<int> in[N],out[N],inV[N],S[N];
void init(){
for(int i=1;i<=n;i++){
p[i]=0;
sz[i]=1;
S[i].emplace(i);
}
}
int Find(int u){
if(!p[u]) return u;
return p[u]=Find(p[u]);
}
void add(int u,int v){
int Gu=Find(u),Gv=Find(v);
if(Gu==Gv) return;
if(in[Gu].count(Gv)){
ans+=2*sz[Gu]*sz[Gv];
ans-=sz[Gu]*inV[Gu].size();
ans-=sz[Gv]*inV[Gv].size();
//debug(sz[Gu],sz[Gv],inV[Gu],inV[Gv]);
if(in[Gu].size()+out[Gu].size()+inV[Gu].size()+S[Gu].size()<in[Gv].size()+out[Gv].size()+inV[Gv].size()+S[Gv].size()) swap(Gu,Gv);
vector<int> V1,V2;
for(int Gw: in[Gv]){
out[Gw].erase(Gv);
out[Gw].emplace(Gu);
in[Gu].emplace(Gw);
if(out[Gu].count(Gw)) V1.emplace_back(Gw);
}
for(int Gw: out[Gv]){
in[Gw].erase(Gv);
in[Gw].emplace(Gu);
out[Gu].emplace(Gw);
if(in[Gu].count(Gw)) V2.emplace_back(Gw);
}
for(int w: inV[Gv]){
if(!S[Gu].count(w)) inV[Gu].emplace(w);
}
for(int w: S[Gv]){
S[Gu].emplace(w);
inV[Gu].erase(w);
}
sz[Gu]+=sz[Gv];
ans+=sz[Gu]*inV[Gu].size();
//debug(sz[Gu],inV[Gu]);
p[Gv]=Gu;
for(int Gw: V1) add(Gw,Gv);
for(int Gw: V2) add(Gw,Gu);
} else if(!inV[Gv].count(u)){
ans+=sz[Gv];
//ug(sz[Gv]);
in[Gv].emplace(Gu);
out[Gu].emplace(Gv);
inV[Gv].emplace(u);
}
}
}U;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
U.init();
for(int u,v,i=1;i<=m;i++){
cin>>u>>v;
U.add(u,v);
cout<<U.ans<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20060 KB |
Output is correct |
2 |
Correct |
5 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
5 ms |
20172 KB |
Output is correct |
6 |
Correct |
5 ms |
20152 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
6 ms |
20312 KB |
Output is correct |
9 |
Correct |
6 ms |
20060 KB |
Output is correct |
10 |
Correct |
5 ms |
20056 KB |
Output is correct |
11 |
Correct |
6 ms |
20152 KB |
Output is correct |
12 |
Correct |
6 ms |
20060 KB |
Output is correct |
13 |
Correct |
5 ms |
20060 KB |
Output is correct |
14 |
Correct |
5 ms |
20060 KB |
Output is correct |
15 |
Correct |
6 ms |
20056 KB |
Output is correct |
16 |
Correct |
5 ms |
20312 KB |
Output is correct |
17 |
Correct |
5 ms |
20060 KB |
Output is correct |
18 |
Correct |
5 ms |
20060 KB |
Output is correct |
19 |
Correct |
5 ms |
20060 KB |
Output is correct |
20 |
Correct |
5 ms |
20060 KB |
Output is correct |
21 |
Correct |
5 ms |
20196 KB |
Output is correct |
22 |
Correct |
5 ms |
20224 KB |
Output is correct |
23 |
Correct |
5 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
25 |
Correct |
5 ms |
20060 KB |
Output is correct |
26 |
Correct |
6 ms |
20060 KB |
Output is correct |
27 |
Correct |
5 ms |
20060 KB |
Output is correct |
28 |
Correct |
5 ms |
20060 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20148 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
6 ms |
20060 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20060 KB |
Output is correct |
2 |
Correct |
5 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
5 ms |
20172 KB |
Output is correct |
6 |
Correct |
5 ms |
20152 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
6 ms |
20312 KB |
Output is correct |
9 |
Correct |
6 ms |
20060 KB |
Output is correct |
10 |
Correct |
5 ms |
20056 KB |
Output is correct |
11 |
Correct |
6 ms |
20152 KB |
Output is correct |
12 |
Correct |
6 ms |
20060 KB |
Output is correct |
13 |
Correct |
5 ms |
20060 KB |
Output is correct |
14 |
Correct |
5 ms |
20060 KB |
Output is correct |
15 |
Correct |
6 ms |
20056 KB |
Output is correct |
16 |
Correct |
5 ms |
20312 KB |
Output is correct |
17 |
Correct |
5 ms |
20060 KB |
Output is correct |
18 |
Correct |
5 ms |
20060 KB |
Output is correct |
19 |
Correct |
5 ms |
20060 KB |
Output is correct |
20 |
Correct |
5 ms |
20060 KB |
Output is correct |
21 |
Correct |
5 ms |
20196 KB |
Output is correct |
22 |
Correct |
5 ms |
20224 KB |
Output is correct |
23 |
Correct |
5 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
25 |
Correct |
5 ms |
20060 KB |
Output is correct |
26 |
Correct |
6 ms |
20060 KB |
Output is correct |
27 |
Correct |
5 ms |
20060 KB |
Output is correct |
28 |
Correct |
5 ms |
20060 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20148 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
6 ms |
20060 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
34 |
Correct |
9 ms |
20420 KB |
Output is correct |
35 |
Correct |
76 ms |
26088 KB |
Output is correct |
36 |
Correct |
87 ms |
29028 KB |
Output is correct |
37 |
Correct |
87 ms |
29088 KB |
Output is correct |
38 |
Correct |
85 ms |
28752 KB |
Output is correct |
39 |
Correct |
8 ms |
20572 KB |
Output is correct |
40 |
Correct |
10 ms |
20828 KB |
Output is correct |
41 |
Correct |
9 ms |
20828 KB |
Output is correct |
42 |
Correct |
7 ms |
20572 KB |
Output is correct |
43 |
Correct |
8 ms |
20568 KB |
Output is correct |
44 |
Correct |
8 ms |
20552 KB |
Output is correct |
45 |
Correct |
7 ms |
20572 KB |
Output is correct |
46 |
Correct |
7 ms |
20568 KB |
Output is correct |
47 |
Correct |
8 ms |
20828 KB |
Output is correct |
48 |
Correct |
8 ms |
20828 KB |
Output is correct |
49 |
Correct |
14 ms |
21596 KB |
Output is correct |
50 |
Correct |
89 ms |
29268 KB |
Output is correct |
51 |
Correct |
10 ms |
20824 KB |
Output is correct |
52 |
Correct |
76 ms |
27584 KB |
Output is correct |
53 |
Correct |
13 ms |
21596 KB |
Output is correct |
54 |
Correct |
81 ms |
28500 KB |
Output is correct |
55 |
Correct |
9 ms |
20828 KB |
Output is correct |
56 |
Correct |
10 ms |
20828 KB |
Output is correct |
57 |
Correct |
10 ms |
21536 KB |
Output is correct |
58 |
Correct |
10 ms |
21340 KB |
Output is correct |
59 |
Correct |
8 ms |
20828 KB |
Output is correct |
60 |
Correct |
62 ms |
25952 KB |
Output is correct |
61 |
Correct |
9 ms |
20824 KB |
Output is correct |
62 |
Correct |
83 ms |
28588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20060 KB |
Output is correct |
2 |
Correct |
5 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
5 ms |
20172 KB |
Output is correct |
6 |
Correct |
5 ms |
20152 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
6 ms |
20312 KB |
Output is correct |
9 |
Correct |
6 ms |
20060 KB |
Output is correct |
10 |
Correct |
5 ms |
20056 KB |
Output is correct |
11 |
Correct |
6 ms |
20152 KB |
Output is correct |
12 |
Correct |
6 ms |
20060 KB |
Output is correct |
13 |
Correct |
5 ms |
20060 KB |
Output is correct |
14 |
Correct |
5 ms |
20060 KB |
Output is correct |
15 |
Correct |
6 ms |
20056 KB |
Output is correct |
16 |
Correct |
5 ms |
20312 KB |
Output is correct |
17 |
Correct |
5 ms |
20060 KB |
Output is correct |
18 |
Correct |
5 ms |
20060 KB |
Output is correct |
19 |
Correct |
5 ms |
20060 KB |
Output is correct |
20 |
Correct |
5 ms |
20060 KB |
Output is correct |
21 |
Correct |
5 ms |
20196 KB |
Output is correct |
22 |
Correct |
5 ms |
20224 KB |
Output is correct |
23 |
Correct |
5 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
25 |
Correct |
5 ms |
20060 KB |
Output is correct |
26 |
Correct |
6 ms |
20060 KB |
Output is correct |
27 |
Correct |
5 ms |
20060 KB |
Output is correct |
28 |
Correct |
5 ms |
20060 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20148 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
6 ms |
20060 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
34 |
Correct |
9 ms |
20420 KB |
Output is correct |
35 |
Correct |
76 ms |
26088 KB |
Output is correct |
36 |
Correct |
87 ms |
29028 KB |
Output is correct |
37 |
Correct |
87 ms |
29088 KB |
Output is correct |
38 |
Correct |
85 ms |
28752 KB |
Output is correct |
39 |
Correct |
8 ms |
20572 KB |
Output is correct |
40 |
Correct |
10 ms |
20828 KB |
Output is correct |
41 |
Correct |
9 ms |
20828 KB |
Output is correct |
42 |
Correct |
7 ms |
20572 KB |
Output is correct |
43 |
Correct |
8 ms |
20568 KB |
Output is correct |
44 |
Correct |
8 ms |
20552 KB |
Output is correct |
45 |
Correct |
7 ms |
20572 KB |
Output is correct |
46 |
Correct |
7 ms |
20568 KB |
Output is correct |
47 |
Correct |
8 ms |
20828 KB |
Output is correct |
48 |
Correct |
8 ms |
20828 KB |
Output is correct |
49 |
Correct |
14 ms |
21596 KB |
Output is correct |
50 |
Correct |
89 ms |
29268 KB |
Output is correct |
51 |
Correct |
10 ms |
20824 KB |
Output is correct |
52 |
Correct |
76 ms |
27584 KB |
Output is correct |
53 |
Correct |
13 ms |
21596 KB |
Output is correct |
54 |
Correct |
81 ms |
28500 KB |
Output is correct |
55 |
Correct |
9 ms |
20828 KB |
Output is correct |
56 |
Correct |
10 ms |
20828 KB |
Output is correct |
57 |
Correct |
10 ms |
21536 KB |
Output is correct |
58 |
Correct |
10 ms |
21340 KB |
Output is correct |
59 |
Correct |
8 ms |
20828 KB |
Output is correct |
60 |
Correct |
62 ms |
25952 KB |
Output is correct |
61 |
Correct |
9 ms |
20824 KB |
Output is correct |
62 |
Correct |
83 ms |
28588 KB |
Output is correct |
63 |
Correct |
411 ms |
72628 KB |
Output is correct |
64 |
Correct |
373 ms |
72644 KB |
Output is correct |
65 |
Correct |
380 ms |
72788 KB |
Output is correct |
66 |
Correct |
193 ms |
43736 KB |
Output is correct |
67 |
Correct |
325 ms |
69728 KB |
Output is correct |
68 |
Correct |
200 ms |
43600 KB |
Output is correct |
69 |
Correct |
305 ms |
41296 KB |
Output is correct |
70 |
Correct |
217 ms |
43600 KB |
Output is correct |
71 |
Correct |
197 ms |
43600 KB |
Output is correct |
72 |
Correct |
332 ms |
53916 KB |
Output is correct |
73 |
Correct |
362 ms |
55632 KB |
Output is correct |
74 |
Correct |
885 ms |
79724 KB |
Output is correct |
75 |
Correct |
456 ms |
49040 KB |
Output is correct |
76 |
Correct |
671 ms |
63764 KB |
Output is correct |
77 |
Correct |
637 ms |
63896 KB |
Output is correct |
78 |
Correct |
162 ms |
44884 KB |
Output is correct |
79 |
Correct |
331 ms |
51024 KB |
Output is correct |
80 |
Correct |
228 ms |
43580 KB |
Output is correct |
81 |
Correct |
335 ms |
47444 KB |
Output is correct |
82 |
Correct |
581 ms |
62036 KB |
Output is correct |
83 |
Correct |
566 ms |
61800 KB |
Output is correct |
84 |
Correct |
588 ms |
86696 KB |
Output is correct |
85 |
Correct |
569 ms |
86804 KB |
Output is correct |
86 |
Correct |
249 ms |
73812 KB |
Output is correct |
87 |
Correct |
288 ms |
76480 KB |
Output is correct |
88 |
Correct |
342 ms |
54864 KB |
Output is correct |
89 |
Correct |
617 ms |
61412 KB |
Output is correct |