QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#18366#2214. Link Cut DigraphWu_Ren#AC ✓418ms38752kbC++111.8kb2022-01-18 15:02:282022-05-04 18:13:22

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:13:22]
  • 评测
  • 测评结果:AC
  • 用时:418ms
  • 内存:38752kb
  • [2022-01-18 15:02:28]
  • 提交

answer

#include<bits/stdc++.h>
int mod;
using namespace std;
int n,m,K,o=0,x,fa[100010],sz[100010];
int dfn[100010],low[100010],st[100010],t;
long long nw,ans[250010];
bool in[100010];
struct op{
	int u,v,t;
};
struct lk{
	int u,v;
}del[100010];
vector<int>nd,E[100010];
int find(int a){
	return fa[a]==a?a:find(fa[a]);
}
void upd(int u,int c){
	nw+=1ll*c*sz[u]*sz[u];
}
void mrg(int u,int v){
	u=find(u),v=find(v);
	if(u==v) return;
	if(sz[u]<sz[v]) swap(u,v);
	del[++o]={u,v};
	upd(u,-1),upd(v,-1),sz[u]+=sz[v],fa[v]=u,upd(u,1);
}
void brk(){
	int u=del[o].u,v=del[o].v;o--;
	upd(u,-1);
	sz[u]-=sz[v],fa[v]=v;
	upd(v,1),upd(u,1);
}
void dfs(int u){
	dfn[u]=low[u]=++x,st[++t]=u,in[u]=1;
	for(int v:E[u]){
		if(!dfn[v]) dfs(v),low[u]=min(low[u],low[v]);
		else if(in[v]) low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u]){
		int v=0;
		do{
			in[st[t]]=0;
			if(v) mrg(v,st[t]);
			v=st[t];
		}while(st[t--]!=u);
	} 
}
void sol(int l,int r,vector<op> &P){
	int mid=(l+r)>>1;
	
	nd.clear();
	for(op i:P) if(i.t<=mid) nd.push_back(find(i.u)),nd.push_back(find(i.v));
	for(int i:nd) dfn[i]=low[i]=0,E[i].clear();
	x=t=0;
	for(op i:P) if(i.t<=mid){
		int u=find(i.u),v=find(i.v);
		E[u].push_back(v);
	}
	
	int ot=o;
	for(int i:nd) if(!dfn[i]) dfs(i);
	
	if(l==r){
		ans[l]=(nw-n)/2;
		while(o>ot) brk();
		return;
	}
	
	vector<op>LP,RP;
	for(op i:P){
		if(find(i.u)==find(i.v)){
			if(i.t<=mid) LP.push_back(i);
		}
		else RP.push_back(i);
	}
	P.resize(0);
	sol(mid+1,r,RP);
	while(o>ot) brk();
	sol(l,mid,LP);
}
int main(){
	scanf("%d%d",&n,&m),nw=n;
	for(int i=1;i<=n;i++) fa[i]=i,sz[i]=1;
	vector<op>P;
	for(int i=1,u,v;i<=m;i++) scanf("%d%d",&u,&v),P.push_back({u,v,i});
	sol(1,m,P);
	for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
}

详细

Test #1:

score: 100
Accepted
time: 396ms
memory: 36500kb

Test #2:

score: 0
Accepted
time: 375ms
memory: 36776kb

Test #3:

score: 0
Accepted
time: 382ms
memory: 37052kb

Test #4:

score: 0
Accepted
time: 338ms
memory: 37688kb

Test #5:

score: 0
Accepted
time: 390ms
memory: 36832kb

Test #6:

score: 0
Accepted
time: 390ms
memory: 36964kb

Test #7:

score: 0
Accepted
time: 378ms
memory: 36476kb

Test #8:

score: 0
Accepted
time: 398ms
memory: 36700kb

Test #9:

score: 0
Accepted
time: 323ms
memory: 38592kb

Test #10:

score: 0
Accepted
time: 418ms
memory: 37104kb

Test #11:

score: 0
Accepted
time: 382ms
memory: 36724kb

Test #12:

score: 0
Accepted
time: 417ms
memory: 36768kb

Test #13:

score: 0
Accepted
time: 319ms
memory: 38696kb

Test #14:

score: 0
Accepted
time: 262ms
memory: 38752kb

Test #15:

score: 0
Accepted
time: 122ms
memory: 20612kb

Test #16:

score: 0
Accepted
time: 335ms
memory: 28348kb

Test #17:

score: 0
Accepted
time: 348ms
memory: 29212kb

Test #18:

score: 0
Accepted
time: 353ms
memory: 29244kb

Test #19:

score: 0
Accepted
time: 397ms
memory: 36840kb

Test #20:

score: 0
Accepted
time: 362ms
memory: 36744kb

Test #21:

score: 0
Accepted
time: 367ms
memory: 36684kb

Test #22:

score: 0
Accepted
time: 362ms
memory: 36700kb

Test #23:

score: 0
Accepted
time: 364ms
memory: 37780kb

Test #24:

score: 0
Accepted
time: 378ms
memory: 36696kb

Test #25:

score: 0
Accepted
time: 369ms
memory: 36564kb

Test #26:

score: 0
Accepted
time: 362ms
memory: 36328kb

Test #27:

score: 0
Accepted
time: 377ms
memory: 35816kb