QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#671918#8055. BalanceeastcloudML 4ms77396kbC++143.5kb2024-10-24 14:59:202024-10-24 14:59:20

Judging History

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

  • [2024-10-24 14:59:20]
  • 评测
  • 测评结果:ML
  • 用时:4ms
  • 内存:77396kb
  • [2024-10-24 14:59:20]
  • 提交

answer

#include<bits/stdc++.h>

#define vi vector<int>
#define fi first
#define se second
#define mp make_pair
#define pi pair<int,int>
#define ary array<int>
#define all(x) begin(x),end(x)
#define pb emplace_back

#define N 700005

using namespace std;

int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
	while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}
void write(int x){
	if(x<0)x=-x,putchar('-');
	if(x/10)write(x/10);
	putchar(x%10+'0');
}

int dfn[N],low[N],tot,stk[N],tp,cnt,E=0,bel[N];
vector<pi> e[N],g[N];
vi C[N];
void tarjan(int x,int fa){
	dfn[x]=low[x]=++tot;stk[++tp]=x;
	auto form=[&](int p){
		cnt++;
		do C[cnt].pb(stk[tp]),bel[stk[tp]]=cnt;
		while(stk[tp--]!=p);
	};
	for(auto _:e[x]){
		int v=_.fi,id=_.se;
		if(id==fa)continue;
		if(!dfn[v]){
			tarjan(v,id);low[x]=min(low[x],low[v]);
			if(low[v]>low[x])form(v);
		}
		else low[x]=min(low[x],dfn[v]);
	}
	if(x==1)form(x);
}

int n,m,sz[N],ed;
pi w[N],pre[N],upl[N];

void fresh(pi &W,pi &P,pi upd,int v){
	if(upd.fi>W.fi)P.fi=v,W.fi=upd.fi;
	if(upd.se>W.se)P.se=v,W.se=upd.se;
}

int flag=0,now=0;
int cut[N],res[N];
int buc[N],a[N],b[N];
int U[N],D[N];

void recol(int x,int col){
	res[x]=col;
	for(auto _:g[x])if(!res[_.fi] && !cut[_.se])recol(_.fi,col);
}
void trace(int x,int opt){
	if(!x)return;
	int las=(opt?pre[x].se:pre[x].fi);
	if(!las)return;
	int eson=0;
	for(auto _:g[x])if(_.fi==las){eson=_.se;break;}
	if(!opt){
		if(U[las])cut[eson]=1;
		trace(las,opt);
		if(U[las])recol(las,b[++now]);
	}
	else{
		if(D[las])cut[eson]=1,recol(x,b[++now]);
		trace(las,opt);
	}
}
void sol(int A,int B){
	trace(A,0);trace(B,1);
}
void dfs(int x,int fa){
	sz[x]=C[x].size();
	for(auto _:g[x]){
		int v=_.fi,id=_.se;
		if(v==fa)continue;
		dfs(v,x);if(flag)return;
		sz[x]+=sz[v];
		
		if(w[x].fi!=0 && w[x].fi+upl[v].se>=ed-1)return pre[x].se=v,flag=1,sol(x,x);
		if(w[x].se!=0 && w[x].se+upl[v].fi>=ed-1)return pre[x].fi=v,flag=1,sol(x,x);
		
		fresh(w[x],pre[x],upl[v],v);
		
		if(w[x].fi>=ed-1)return flag=1,sol(x,0);
		if(w[x].se>=ed-1)return flag=1,sol(0,x);
	}
	upl[x]=w[x];
	if(buc[sz[x]])upl[x].fi++,U[x]=1;
	if(buc[n-sz[x]])upl[x].se++,D[x]=1;
}



void solve(){
	n=read(),m=read();
	for(int i=1;i<=E;i++)cut[i]=0;
	for(int i=1;i<=cnt;i++){
		g[i].clear(),C[i].clear();e[i].clear();
		w[i].fi=w[i].se=pre[i].fi=pre[i].se=res[i]=U[i]=D[i]=0;
		dfn[i]=low[i]=buc[i]=a[i]=b[i]=bel[i]=0;
	}
	tot=cnt=tp=E=flag=now=ed=0;cnt=n;
	for(int i=1;i<=m;i++){
		int u=read(),v=read();
		e[u].pb(v,i);e[v].pb(u,i);
	}
	tarjan(1,-1);
	for(int i=1;i<=n;i++){
		for(auto _:e[i]){
			int v=_.fi;
			if(bel[v]!=bel[i] && bel[v]<bel[i]){
				g[bel[v]].pb(bel[i],++E);
				g[bel[i]].pb(bel[v],E);
			}
		}
	}
	for(int i=1;i<=n;i++)a[i]=read(),buc[a[i]]++;
	ed=0;
	for(int i=1;i<=n;i++){
		if(buc[i])a[++ed]=buc[i],b[ed]=i;
		buc[i]=0;
	}
	if(ed==1 && cnt!=1)return printf("No\n"),void();
	else if(ed==1){
		printf("Yes\n");
		for(int i=1;i<=n;i++)write(b[i]),putchar(' ');putchar('\n');
		return;
	}
	for(int i=1;i<=ed;i++)a[i]+=a[i-1],buc[a[i]]++;
	
	dfs(cnt,0);
	if(!flag)printf("No\n");
	else{
		printf("Yes\n");
		for(int i=1;i<=n;i++)write((res[bel[i]]?res[bel[i]]:b[ed])),putchar(' ');
		putchar('\n');
	}
}

int main(){
	#ifdef EAST_CLOUD
	freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	
	int T=read();while(T--)solve();
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 77396kb

input:

5
5 4
1 2
2 3
3 4
4 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 2 2 3
5 6
1 2
1 2
2 3
3 4
4 5
3 5
1 2 1 2 1
2 2
1 2
1 2
1 2

output:

Yes
5 4 3 2 1 
No
Yes
2 1 3 2 2 
Yes
2 2 1 1 1 
No

result:

ok ok (5 test cases)

Test #2:

score: -100
Memory Limit Exceeded

input:

100000
4 3
4 2
1 3
2 1
2 1 3 2
5 7
2 5
5 3
2 3
5 1
4 3
2 4
4 3
1 3 1 1 2
3 2
2 1
2 3
1 1 1
4 7
3 4
1 4
2 3
4 3
4 2
3 1
4 3
2 3 3 2
4 6
2 4
3 1
1 2
1 2
2 3
4 2
1 1 3 3
4 7
3 2
4 2
1 2
3 4
3 2
2 4
3 4
2 1 1 1
5 5
3 2
1 5
4 5
4 3
2 1
1 2 2 3 2
3 5
2 3
1 3
3 1
3 1
2 1
3 2 3
2 3
2 1
2 1
1 2
1 1
2 3
2 1
1...

output:

Yes
2 2 1 3 
No
No
No
No
Yes
2 1 1 1 
No
No
No
No
No
No
No
No
Yes
1 3 1 1 1 
No
Yes
2 1 
No
Yes
2 1 
No
No
No
No
No
No
No
No
No
Yes
1 1 1 2 
No
No
No
No
No
No
No
Yes
1 1 1 1 2 
Yes
1 2 1 1 
No
No
No
No
Yes
3 1 3 3 2 
Yes
1 2 1 1 
No
No
Yes
2 2 1 
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Ye...

result: