#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
using B=bool;
const I N=100000;
set<pair<I,I>>viss;
I pars[N];
set<I>incs[N],outs[N],nods[N];
Lli res=0;
I fnd(I i){
return pars[i]<0?i:pars[i]=fnd(pars[i]);
}
I siz(I i){
return-pars[fnd(i)];
}
B con(I a,I b){
return fnd(a)==fnd(b);
}
void uni(I a,I b){
if((a=fnd(a))==(b=fnd(b)))return;
if(pars[a]>pars[b])swap(a,b);
Lli siz1=siz(a),siz2=siz(b);
res-=siz1*(incs[a].size()+siz1-1);
res-=siz2*(incs[b].size()+siz2-1);
for(auto c:incs[b]){
outs[c].erase(b);
if(fnd(c)!=a){
incs[a].insert(c);
outs[c].insert(a);
}
}
for(auto c:nods[b]){
nods[a].insert(c);
if(outs[c].count(a)){
outs[c].erase(a);
incs[a].erase(c);
}
}
pars[a]+=pars[b],pars[b]=a;
Lli siz3=siz(a);
res+=siz3*(incs[a].size()+siz3-1);
}
I main(){
cin.tie(0)->sync_with_stdio(0);
I n,m;cin>>n>>m;
fill_n(pars,n,-1);
for(I i=0;i<n;i++)nods[i].insert(i);
for(I i=0;i<m;i++){
I a,b;cin>>a>>b,a--,b--;
if(!con(a,b)&&!outs[a].count(fnd(b))){
incs[fnd(b)].insert(a);
outs[a].insert(fnd(b));
res+=siz(b);
}
viss.insert({a,b});
if(viss.count({b,a}))uni(a,b);
printf("%lli\n",res);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
14408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
14408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
14408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |