QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18356 | #2214. Link Cut Digraph | qingyu_orz | WA | 504ms | 46828kb | C++14 | 2.0kb | 2022-01-18 12:23:31 | 2022-05-04 18:10:49 |
Judging History
answer
#include<bits/stdc++.h>
#define sh(x) (1ll*x*(x-1)/2)
#define clr(x) (dfn[x]=low[x]=vist[x]=0)
using namespace std;
int u[250005],v[250005];
vector<int>xz[250005];
vector<int>g[250005];
int col[100005],co2[100005];
int dfn[100005],low[100005];
int t1,t2,st[100005],vist[100005];
int findcolour(int x){
return x==col[x]?x:col[x]=findcolour(col[x]);
}
void tarjan(int x){
dfn[x]=low[x]=++t1;
st[++t2]=x,vist[x]=1;
for(auto cu:g[x]){
if(!dfn[cu]){
tarjan(cu);
low[x]=min(low[x],low[cu]);
}else if(vist[cu]){
low[x]=min(low[x],dfn[cu]);
}
}
if(dfn[x]!=low[x])return;
int pp;
do{
pp=st[t2--];
vist[pp]=0,co2[pp]=x;
}while(pp!=x);
}
void solve(int l,int r,auto au){
if(l==r){
xz[l]=au;
return;
}
int mid=(l+r)>>1;
vector<int>ls;
for(auto cu:au){
if(cu>mid)continue;
int U=findcolour(u[cu]),V=findcolour(v[cu]);
g[U].emplace_back(V);
ls.emplace_back(U);
ls.emplace_back(V);
clr(U),clr(V);
}
for(auto cu:ls)if(!dfn[cu]){
t1=t2=0;
tarjan(cu);
}
for(auto cu:ls)g[cu].clear();
vector<int>a1,a2;
for(auto cu:au){
int x=co2[findcolour(u[cu])];
int y=co2[findcolour(v[cu])];
if(x==y)a1.emplace_back(cu);
else a2.emplace_back(cu);
}
for(auto cu:ls)col[cu]=co2[col[cu]];
for(auto cu:ls)co2[cu]=cu;
solve(mid+1,r,a2);
for(auto cu:ls)col[cu]=cu;
solve(l,mid,a1);
}
int fa[100005],cnt[100005];
int findfather(int x){
return x==fa[x]?x:fa[x]=findfather(fa[x]);
}
long long ans;
void merg(int x,int y){
int fx=findfather(x),fy=findfather(y);
if(fx==fy)return;
ans-=sh(cnt[fx])+sh(cnt[fy]);
fa[fx]=fy,cnt[fy]+=cnt[fx];
ans+=sh(cnt[fy]);
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=m;++i){
scanf("%d%d",&u[i],&v[i]);
}
for(int i=1;i<=n;++i)col[i]=i;
for(int i=1;i<=n;++i)co2[i]=i;
vector<int>g;
for(int i=1;i<=m;++i)g.emplace_back(i);
solve(1,m+1,g);
for(int i=1;i<=n;++i)fa[i]=i,cnt[i]=1;
ans=0;
for(int i=1;i<=m;++i){
for(auto cu:xz[i]){
int L=u[cu],R=v[cu];
merg(L,R);
}
printf("%lld\n",ans);
}
return 0;
}
Details
Test #1:
score: 0
Wrong Answer
time: 504ms
memory: 46828kb