#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
const int maxn=1e5+9;
set<pii>a[maxn];
//x->u
set<int>b[maxn];
//u->x
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+=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({pr,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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9724 KB |
Output is correct |
2 |
Correct |
4 ms |
9716 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9724 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9700 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9716 KB |
Output is correct |
13 |
Correct |
4 ms |
9720 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9704 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9716 KB |
Output is correct |
21 |
Correct |
5 ms |
9736 KB |
Output is correct |
22 |
Correct |
5 ms |
9720 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
4 ms |
9724 KB |
Output is correct |
25 |
Correct |
5 ms |
9728 KB |
Output is correct |
26 |
Correct |
4 ms |
9684 KB |
Output is correct |
27 |
Correct |
4 ms |
9684 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9716 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9724 KB |
Output is correct |
2 |
Correct |
4 ms |
9716 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9724 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9700 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9716 KB |
Output is correct |
13 |
Correct |
4 ms |
9720 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9704 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9716 KB |
Output is correct |
21 |
Correct |
5 ms |
9736 KB |
Output is correct |
22 |
Correct |
5 ms |
9720 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
4 ms |
9724 KB |
Output is correct |
25 |
Correct |
5 ms |
9728 KB |
Output is correct |
26 |
Correct |
4 ms |
9684 KB |
Output is correct |
27 |
Correct |
4 ms |
9684 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9716 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9724 KB |
Output is correct |
2 |
Correct |
4 ms |
9716 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9724 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9700 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9716 KB |
Output is correct |
13 |
Correct |
4 ms |
9720 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9704 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9716 KB |
Output is correct |
21 |
Correct |
5 ms |
9736 KB |
Output is correct |
22 |
Correct |
5 ms |
9720 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
4 ms |
9724 KB |
Output is correct |
25 |
Correct |
5 ms |
9728 KB |
Output is correct |
26 |
Correct |
4 ms |
9684 KB |
Output is correct |
27 |
Correct |
4 ms |
9684 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9716 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |