QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#20269#2214. Link Cut Digraph19ty02#WA 408ms78384kbC++172.1kb2022-02-15 10:35:372022-05-03 09:26:08

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:26:08]
  • 评测
  • 测评结果:WA
  • 用时:408ms
  • 内存:78384kb
  • [2022-02-15 10:35:37]
  • 提交

answer

#include<cstdio>
#include<vector>
#include<iostream>
#define LL long long
using namespace std;
const int N=3e5+10,M=3e5+10;
int n,m;
struct edge{
	int id,u,v;
}e[M];
vector<int> ae[M];

int h[N],tot;
struct tar{
	int to,nxt;
}tr[M];
void add(int u,int v){
	tr[++tot]=(tar){v,h[u]};
	h[u]=tot;
}
int sta[N],tp,scc[N],dfn[N],low[N],_dfn,col;
void tarjan(int u){
	dfn[u]=low[u]=++_dfn;
	sta[++tp]=u;
	for(int i=h[u];i;i=tr[i].nxt){
		int v=tr[i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}else{
			if(!scc[v])low[u]=min(low[u],dfn[v]);
		}
	}
	if(dfn[u]==low[u]){
		col++;
		while(sta[tp]!=u)scc[sta[tp--]]=col;
		scc[sta[tp--]]=col;
	}
}
void solve(int l,int r,vector<int> x){
	if(l==r){
		for(int i=0;i<x.size();i++){
			ae[l].push_back(x[i]);
		}
		return;
	}
	int mid=(l+r)>>1;
	
	vector<int> d;
	for(int i=0;i<x.size();i++){
		int j=x[i];
		if(e[j].id>mid)break;
		add(e[j].u,e[j].v);
		d.push_back(e[j].u);
		d.push_back(e[j].v);
	}
	for(int i=0;i<d.size();i++){
		int j=d[i];
		if(!scc[j])tarjan(j);
	}
	vector<int> x1,x2;
	for(int i=0;i<x.size();i++){
		int j=x[i];
		if(e[j].id<=mid&&scc[e[j].u]==scc[e[j].v])x1.push_back(j);
		else x2.push_back(j);
	}
	for(int i=0;i<d.size();i++){
		int j=d[i];
		h[j]=dfn[j]=low[j]=scc[j]=0;
	}
	tot=tp=_dfn=col=0;
	d.clear();
	
	solve(l,mid,x1);
	solve(mid+1,r,x2);
}

int fa[N],sz[N];
LL ans;
int Find(int x){
	return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Merge(int u,int v){
	int fu=Find(u),fv=Find(v);
	if(fu!=fv){
		ans+=1ll*sz[fu]*sz[fv];
		fa[fv]=fu;sz[fu]+=sz[fv];
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		e[i].id=i;
		scanf("%d%d",&e[i].u,&e[i].v);
	}
	vector<int> x;
	for(int i=1;i<=m;i++)x.push_back(i);
	solve(1,m+1,x);
	
	for(int i=1;i<=n;i++)fa[i]=i,sz[i]=1;
	for(int i=1;i<=m;i++){
		for(int j=0;j<ae[i].size();j++){
			int k=ae[i][j];
			Merge(e[k].u,e[k].v);
//			printf("edge: u:%d v=%d on time %d\n",e[k].u,e[k].v,i);
		}
		printf("%lld\n",ans);
	}
}
/*
4 5
1 2
2 3
2 4
3 1
3 4
*/

详细

Test #1:

score: 0
Wrong Answer
time: 408ms
memory: 78384kb