#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
//#define int long long
const int maxn=1e5+9;
set<pii>a[maxn];
set<int>b[maxn];
int f[maxn];
int findset(int u){
if (f[u]<0)return u;
return f[u]=findset(f[u]);
}
long long ans=0;
void add(int u,int v){
int x=findset(u),y=findset(v);
if (x==y)return;
auto it=a[y].lower_bound({x,-1});
if (it!=a[y].end()&&(*it).fi==x){
if (sz(a[x])+sz(b[x])<sz(a[y])+sz(b[y])){
swap(x,y);
swap(u,v);
}
vector<pii>ad;
for (auto p:a[y]){
//p.fi clique p.se thuoc y
b[p.fi].erase(p.se);
ans-=abs(f[p.fi]);
if (p.fi!=x)ad.pb({p.se,p.fi});
}
ans+=1ll*abs(f[y])*(2*(abs(f[x]))+sz(b[x])-sz(b[y]));
for (auto p:b[y]){
int pr=findset(p);
a[pr].erase({y,p});
if (pr!=x)ad.pb({p,x});
}
f[x]+=f[y];
f[y]=x;
for (auto p:ad)add(p.fi,p.se);
}
else if (b[y].find(u)==b[y].end()){
ans+=abs(f[y]);
b[y].insert(u);
a[x].insert({y,u});
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,m;
cin>>n>>m;
for1(i,1,n)f[i]=-1;
for1(i,1,m){
int u,v;
cin>>u>>v;
add(u,v);
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9712 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9716 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9620 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9724 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
4 ms |
9684 KB |
Output is correct |
24 |
Correct |
4 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
4 ms |
9684 KB |
Output is correct |
27 |
Correct |
4 ms |
9684 KB |
Output is correct |
28 |
Correct |
4 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9684 KB |
Output is correct |
32 |
Correct |
4 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9712 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9716 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9620 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9724 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
4 ms |
9684 KB |
Output is correct |
24 |
Correct |
4 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
4 ms |
9684 KB |
Output is correct |
27 |
Correct |
4 ms |
9684 KB |
Output is correct |
28 |
Correct |
4 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9684 KB |
Output is correct |
32 |
Correct |
4 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
6 ms |
9812 KB |
Output is correct |
35 |
Correct |
58 ms |
15232 KB |
Output is correct |
36 |
Correct |
75 ms |
17472 KB |
Output is correct |
37 |
Correct |
75 ms |
17544 KB |
Output is correct |
38 |
Correct |
85 ms |
17296 KB |
Output is correct |
39 |
Correct |
6 ms |
9812 KB |
Output is correct |
40 |
Correct |
6 ms |
9832 KB |
Output is correct |
41 |
Correct |
6 ms |
9812 KB |
Output is correct |
42 |
Correct |
6 ms |
9788 KB |
Output is correct |
43 |
Correct |
6 ms |
9840 KB |
Output is correct |
44 |
Correct |
8 ms |
9812 KB |
Output is correct |
45 |
Correct |
5 ms |
9812 KB |
Output is correct |
46 |
Correct |
7 ms |
9812 KB |
Output is correct |
47 |
Correct |
6 ms |
9832 KB |
Output is correct |
48 |
Correct |
6 ms |
9860 KB |
Output is correct |
49 |
Correct |
10 ms |
10624 KB |
Output is correct |
50 |
Correct |
75 ms |
17600 KB |
Output is correct |
51 |
Correct |
9 ms |
10068 KB |
Output is correct |
52 |
Correct |
82 ms |
16268 KB |
Output is correct |
53 |
Correct |
9 ms |
10452 KB |
Output is correct |
54 |
Correct |
73 ms |
16908 KB |
Output is correct |
55 |
Correct |
8 ms |
10112 KB |
Output is correct |
56 |
Correct |
7 ms |
10124 KB |
Output is correct |
57 |
Correct |
9 ms |
10188 KB |
Output is correct |
58 |
Correct |
7 ms |
10196 KB |
Output is correct |
59 |
Correct |
7 ms |
9772 KB |
Output is correct |
60 |
Correct |
60 ms |
14712 KB |
Output is correct |
61 |
Correct |
6 ms |
9940 KB |
Output is correct |
62 |
Correct |
74 ms |
17176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9712 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9716 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9620 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9724 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
4 ms |
9684 KB |
Output is correct |
24 |
Correct |
4 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
4 ms |
9684 KB |
Output is correct |
27 |
Correct |
4 ms |
9684 KB |
Output is correct |
28 |
Correct |
4 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9684 KB |
Output is correct |
32 |
Correct |
4 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
6 ms |
9812 KB |
Output is correct |
35 |
Correct |
58 ms |
15232 KB |
Output is correct |
36 |
Correct |
75 ms |
17472 KB |
Output is correct |
37 |
Correct |
75 ms |
17544 KB |
Output is correct |
38 |
Correct |
85 ms |
17296 KB |
Output is correct |
39 |
Correct |
6 ms |
9812 KB |
Output is correct |
40 |
Correct |
6 ms |
9832 KB |
Output is correct |
41 |
Correct |
6 ms |
9812 KB |
Output is correct |
42 |
Correct |
6 ms |
9788 KB |
Output is correct |
43 |
Correct |
6 ms |
9840 KB |
Output is correct |
44 |
Correct |
8 ms |
9812 KB |
Output is correct |
45 |
Correct |
5 ms |
9812 KB |
Output is correct |
46 |
Correct |
7 ms |
9812 KB |
Output is correct |
47 |
Correct |
6 ms |
9832 KB |
Output is correct |
48 |
Correct |
6 ms |
9860 KB |
Output is correct |
49 |
Correct |
10 ms |
10624 KB |
Output is correct |
50 |
Correct |
75 ms |
17600 KB |
Output is correct |
51 |
Correct |
9 ms |
10068 KB |
Output is correct |
52 |
Correct |
82 ms |
16268 KB |
Output is correct |
53 |
Correct |
9 ms |
10452 KB |
Output is correct |
54 |
Correct |
73 ms |
16908 KB |
Output is correct |
55 |
Correct |
8 ms |
10112 KB |
Output is correct |
56 |
Correct |
7 ms |
10124 KB |
Output is correct |
57 |
Correct |
9 ms |
10188 KB |
Output is correct |
58 |
Correct |
7 ms |
10196 KB |
Output is correct |
59 |
Correct |
7 ms |
9772 KB |
Output is correct |
60 |
Correct |
60 ms |
14712 KB |
Output is correct |
61 |
Correct |
6 ms |
9940 KB |
Output is correct |
62 |
Correct |
74 ms |
17176 KB |
Output is correct |
63 |
Correct |
290 ms |
43720 KB |
Output is correct |
64 |
Correct |
303 ms |
43720 KB |
Output is correct |
65 |
Correct |
321 ms |
43644 KB |
Output is correct |
66 |
Correct |
69 ms |
19184 KB |
Output is correct |
67 |
Correct |
108 ms |
21388 KB |
Output is correct |
68 |
Correct |
66 ms |
19140 KB |
Output is correct |
69 |
Correct |
161 ms |
20608 KB |
Output is correct |
70 |
Correct |
74 ms |
19104 KB |
Output is correct |
71 |
Correct |
77 ms |
19188 KB |
Output is correct |
72 |
Correct |
153 ms |
21632 KB |
Output is correct |
73 |
Correct |
143 ms |
21644 KB |
Output is correct |
74 |
Correct |
556 ms |
42296 KB |
Output is correct |
75 |
Correct |
244 ms |
26248 KB |
Output is correct |
76 |
Correct |
342 ms |
32672 KB |
Output is correct |
77 |
Correct |
318 ms |
32844 KB |
Output is correct |
78 |
Correct |
97 ms |
21624 KB |
Output is correct |
79 |
Correct |
182 ms |
26360 KB |
Output is correct |
80 |
Correct |
106 ms |
21540 KB |
Output is correct |
81 |
Correct |
186 ms |
24928 KB |
Output is correct |
82 |
Correct |
338 ms |
33416 KB |
Output is correct |
83 |
Correct |
311 ms |
33480 KB |
Output is correct |
84 |
Correct |
272 ms |
33304 KB |
Output is correct |
85 |
Correct |
326 ms |
33300 KB |
Output is correct |
86 |
Correct |
79 ms |
18416 KB |
Output is correct |
87 |
Correct |
84 ms |
20620 KB |
Output is correct |
88 |
Correct |
164 ms |
21896 KB |
Output is correct |
89 |
Correct |
368 ms |
31716 KB |
Output is correct |