#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
const I N=100000;
const I M=300000;
pair<I,I>fols[M];
set<pair<I,I>>viss;
vector<I>mems[N];
set<I>incs[N];
I pars[N];
Lli res=0;
I fnd(I i){
if(pars[i]<0)return i;
return pars[i]=fnd(pars[i]);
}
void uni(I a,I b){
if((a=fnd(a))==(b=fnd(b)))return;
if(pars[a]>pars[b])swap(a,b);
res-=(Lli)-pars[a]*(-pars[a]-1);
res-=(Lli)-pars[b]*(-pars[b]-1);
res-=(Lli)-pars[a]*incs[a].size();
res-=(Lli)-pars[b]*incs[b].size();
for(auto c:mems[b]){
if(incs[a].count(c))incs[a].erase(c);
mems[a].push_back(c);
}
for(auto c:incs[b]){
if(fnd(c)!=a)incs[a].insert(c);
}
pars[a]+=pars[b],pars[b]=a;
res+=(Lli)-pars[a]*(-pars[a]-1);
res+=(Lli)-pars[a]*incs[a].size();
}
I main(){
cin.tie(0)->sync_with_stdio(0);
I n,m;cin>>n>>m;
for(I i=0;i<m;i++){
I a,b;cin>>a>>b,a--,b--;
fols[i]={a,b};
}
fill_n(pars,n,-1);
for(I i=0;i<n;i++)mems[i].push_back(i);
for(I i=0;i<m;i++){
auto[a,b]=fols[i];
if(viss.count({b,a})){
uni(a,b);
}else{
viss.insert({a,b});
b=fnd(b);
if(!incs[b].count(a)){
incs[b].insert(a);
res+=-pars[b];
}
}
printf("%lli\n",res);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |