QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#20211#2214. Link Cut Digraphhouzhiyuanakioi#AC ✓1044ms90816kbC++2.5kb2022-02-15 00:02:222022-05-03 09:16:55

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-03 09:16:55]
  • 评测
  • 测评结果:AC
  • 用时:1044ms
  • 内存:90816kb
  • [2022-02-15 00:02:22]
  • 提交

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: 937ms
memory: 78616kb

Test #2:

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

Test #3:

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

Test #4:

score: 0
Accepted
time: 891ms
memory: 90816kb

Test #5:

score: 0
Accepted
time: 974ms
memory: 77760kb

Test #6:

score: 0
Accepted
time: 1007ms
memory: 77708kb

Test #7:

score: 0
Accepted
time: 913ms
memory: 77004kb

Test #8:

score: 0
Accepted
time: 948ms
memory: 77832kb

Test #9:

score: 0
Accepted
time: 768ms
memory: 89248kb

Test #10:

score: 0
Accepted
time: 945ms
memory: 78212kb

Test #11:

score: 0
Accepted
time: 895ms
memory: 77916kb

Test #12:

score: 0
Accepted
time: 1001ms
memory: 78496kb

Test #13:

score: 0
Accepted
time: 780ms
memory: 90084kb

Test #14:

score: 0
Accepted
time: 801ms
memory: 89436kb

Test #15:

score: 0
Accepted
time: 596ms
memory: 59632kb

Test #16:

score: 0
Accepted
time: 921ms
memory: 58784kb

Test #17:

score: 0
Accepted
time: 901ms
memory: 58336kb

Test #18:

score: 0
Accepted
time: 886ms
memory: 59724kb

Test #19:

score: 0
Accepted
time: 940ms
memory: 78340kb

Test #20:

score: 0
Accepted
time: 911ms
memory: 82428kb

Test #21:

score: 0
Accepted
time: 873ms
memory: 81852kb

Test #22:

score: 0
Accepted
time: 889ms
memory: 81072kb

Test #23:

score: 0
Accepted
time: 921ms
memory: 82868kb

Test #24:

score: 0
Accepted
time: 900ms
memory: 80700kb

Test #25:

score: 0
Accepted
time: 867ms
memory: 79900kb

Test #26:

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

Test #27:

score: 0
Accepted
time: 883ms
memory: 76752kb