QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#18360 | #2214. Link Cut Digraph | qingyu_orz | AC ✓ | 1102ms | 89848kb | C++14 | 2.5kb | 2022-01-18 13:25:29 | 2022-05-04 18:11:07 |
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1022ms
memory: 79524kb
Test #2:
score: 0
Accepted
time: 1007ms
memory: 79692kb
Test #3:
score: 0
Accepted
time: 989ms
memory: 78240kb
Test #4:
score: 0
Accepted
time: 957ms
memory: 89772kb
Test #5:
score: 0
Accepted
time: 1069ms
memory: 78480kb
Test #6:
score: 0
Accepted
time: 1041ms
memory: 79848kb
Test #7:
score: 0
Accepted
time: 976ms
memory: 77924kb
Test #8:
score: 0
Accepted
time: 1083ms
memory: 78828kb
Test #9:
score: 0
Accepted
time: 845ms
memory: 89848kb
Test #10:
score: 0
Accepted
time: 1047ms
memory: 78684kb
Test #11:
score: 0
Accepted
time: 1094ms
memory: 77896kb
Test #12:
score: 0
Accepted
time: 1102ms
memory: 78652kb
Test #13:
score: 0
Accepted
time: 900ms
memory: 89540kb
Test #14:
score: 0
Accepted
time: 977ms
memory: 89584kb
Test #15:
score: 0
Accepted
time: 679ms
memory: 60452kb
Test #16:
score: 0
Accepted
time: 964ms
memory: 59328kb
Test #17:
score: 0
Accepted
time: 892ms
memory: 58716kb
Test #18:
score: 0
Accepted
time: 886ms
memory: 59636kb
Test #19:
score: 0
Accepted
time: 940ms
memory: 78596kb
Test #20:
score: 0
Accepted
time: 958ms
memory: 81656kb
Test #21:
score: 0
Accepted
time: 911ms
memory: 82284kb
Test #22:
score: 0
Accepted
time: 1045ms
memory: 81140kb
Test #23:
score: 0
Accepted
time: 942ms
memory: 84452kb
Test #24:
score: 0
Accepted
time: 896ms
memory: 81976kb
Test #25:
score: 0
Accepted
time: 849ms
memory: 81836kb
Test #26:
score: 0
Accepted
time: 908ms
memory: 79520kb
Test #27:
score: 0
Accepted
time: 963ms
memory: 78096kb