QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#18357#2214. Link Cut Digraphqingyu_orzWA 593ms63336kbC++142.3kb2022-01-18 12:33:232022-05-04 18:10:53

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-04 18:10:53]
  • 评测
  • 测评结果:WA
  • 用时:593ms
  • 内存:63336kb
  • [2022-01-18 12:33:23]
  • 提交

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){
		if(cu>mid)continue;
		int U=u[cu],V=v[cu];
		while(U!=col[U]){
			sl.emplace_back(make_pair(U,col[U]));
			U=col[U];
		}
		sl.emplace_back(make_pair(U,U));
		while(V!=col[V]){
			sl.emplace_back(make_pair(V,col[V]));
			V=col[V];
		}
		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 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: 0
Wrong Answer
time: 593ms
memory: 63336kb