QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#19939 | #2214. Link Cut Digraph | JohnAlfnov# | AC ✓ | 1062ms | 89512kb | C++20 | 2.5kb | 2022-02-14 08:30:09 | 2022-05-08 01:38:46 |
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;
vector<pair<int,int>>sl;
for(auto cu:au){
int U=u[cu],V=v[cu];
vector<int>s1,s2;
while(U!=col[U]){
s1.emplace_back(U);
U=col[U];
}
s1.emplace_back(U);
while(V!=col[V]){
s2.emplace_back(V);
V=col[V];
}
s2.emplace_back(V);
for(auto xx:s1)sl.emplace_back(make_pair(xx,col[xx]));
for(auto yy:s2)sl.emplace_back(make_pair(yy,col[yy]));
while(s1.size()){
int xx=s1.back();
col[xx]=U;
s1.pop_back();
}
while(s2.size()){
int yy=s2.back();
col[yy]=V;
s2.pop_back();
}
if(cu>mid)continue;
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[cu];
for(auto cu:ls)co2[cu]=cu;
solve(mid+1,r,a2);
for(auto pi:sl)col[pi.first]=pi.second;
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: 100
Accepted
time: 933ms
memory: 78556kb
Test #2:
score: 0
Accepted
time: 990ms
memory: 77732kb
Test #3:
score: 0
Accepted
time: 1014ms
memory: 77924kb
Test #4:
score: 0
Accepted
time: 947ms
memory: 88936kb
Test #5:
score: 0
Accepted
time: 1062ms
memory: 77600kb
Test #6:
score: 0
Accepted
time: 1042ms
memory: 77508kb
Test #7:
score: 0
Accepted
time: 994ms
memory: 76988kb
Test #8:
score: 0
Accepted
time: 1046ms
memory: 77520kb
Test #9:
score: 0
Accepted
time: 836ms
memory: 88968kb
Test #10:
score: 0
Accepted
time: 983ms
memory: 78436kb
Test #11:
score: 0
Accepted
time: 1056ms
memory: 77324kb
Test #12:
score: 0
Accepted
time: 999ms
memory: 78512kb
Test #13:
score: 0
Accepted
time: 839ms
memory: 89512kb
Test #14:
score: 0
Accepted
time: 832ms
memory: 88364kb
Test #15:
score: 0
Accepted
time: 636ms
memory: 59532kb
Test #16:
score: 0
Accepted
time: 940ms
memory: 58720kb
Test #17:
score: 0
Accepted
time: 916ms
memory: 57868kb
Test #18:
score: 0
Accepted
time: 929ms
memory: 59764kb
Test #19:
score: 0
Accepted
time: 1003ms
memory: 78384kb
Test #20:
score: 0
Accepted
time: 945ms
memory: 80892kb
Test #21:
score: 0
Accepted
time: 946ms
memory: 81068kb
Test #22:
score: 0
Accepted
time: 996ms
memory: 80808kb
Test #23:
score: 0
Accepted
time: 929ms
memory: 81160kb
Test #24:
score: 0
Accepted
time: 1037ms
memory: 80756kb
Test #25:
score: 0
Accepted
time: 926ms
memory: 79348kb
Test #26:
score: 0
Accepted
time: 932ms
memory: 78484kb
Test #27:
score: 0
Accepted
time: 892ms
memory: 75900kb