QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#18361#2214. Link Cut DigraphJohnAlfnov#AC ✓1117ms91056kbC++142.5kb2022-01-18 13:26:012022-05-04 18:11:47

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:11:47]
  • 评测
  • 测评结果:AC
  • 用时:1117ms
  • 内存:91056kb
  • [2022-01-18 13:26:01]
  • 提交

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: 922ms
memory: 78768kb

Test #2:

score: 0
Accepted
time: 1017ms
memory: 79208kb

Test #3:

score: 0
Accepted
time: 936ms
memory: 78472kb

Test #4:

score: 0
Accepted
time: 893ms
memory: 91056kb

Test #5:

score: 0
Accepted
time: 926ms
memory: 77856kb

Test #6:

score: 0
Accepted
time: 1113ms
memory: 77864kb

Test #7:

score: 0
Accepted
time: 1048ms
memory: 78048kb

Test #8:

score: 0
Accepted
time: 999ms
memory: 77888kb

Test #9:

score: 0
Accepted
time: 993ms
memory: 89324kb

Test #10:

score: 0
Accepted
time: 1117ms
memory: 79172kb

Test #11:

score: 0
Accepted
time: 1085ms
memory: 79492kb

Test #12:

score: 0
Accepted
time: 1063ms
memory: 79552kb

Test #13:

score: 0
Accepted
time: 881ms
memory: 89472kb

Test #14:

score: 0
Accepted
time: 822ms
memory: 89484kb

Test #15:

score: 0
Accepted
time: 646ms
memory: 59996kb

Test #16:

score: 0
Accepted
time: 915ms
memory: 59720kb

Test #17:

score: 0
Accepted
time: 1044ms
memory: 58228kb

Test #18:

score: 0
Accepted
time: 906ms
memory: 60060kb

Test #19:

score: 0
Accepted
time: 1037ms
memory: 79016kb

Test #20:

score: 0
Accepted
time: 936ms
memory: 82396kb

Test #21:

score: 0
Accepted
time: 939ms
memory: 82096kb

Test #22:

score: 0
Accepted
time: 959ms
memory: 82360kb

Test #23:

score: 0
Accepted
time: 935ms
memory: 83996kb

Test #24:

score: 0
Accepted
time: 880ms
memory: 81688kb

Test #25:

score: 0
Accepted
time: 957ms
memory: 82220kb

Test #26:

score: 0
Accepted
time: 943ms
memory: 80444kb

Test #27:

score: 0
Accepted
time: 981ms
memory: 77660kb