QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#453799#8055. Balanceucup-team3282TL 0ms14464kbC++143.2kb2024-06-24 11:58:412024-06-24 11:58:41

Judging History

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

  • [2024-06-24 11:58:41]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:14464kb
  • [2024-06-24 11:58:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;

int T;
int n,m;
vector <pair<int,int> > g[maxn];
int a[maxn],tmp[maxn],c;

int dfn[maxn],low[maxn],dfnc=0;
vector <pair<int,int> > g1[maxn];
int val[maxn],ec=0;
stack <int> s;
vector <int> dcc[maxn];
int dccc=0,bel[maxn];

void tarjan(int u,int fid){
	dfn[u]=low[u]=++dfnc;
	s.push(u);
	for(auto t:g[u]){
		int v=t.first,id=t.second;
		if(!dfn[v]){
			tarjan(v,id);
			low[u]=min(low[v],low[u]);
		}
		else if(id!=(fid^1)){
			low[u]=min(dfn[v],low[u]);
		}
	}
	if(low[u]==dfn[u]){
		dccc++;
		int x;
		do{
			x=s.top();
			s.pop();
			dcc[dccc].push_back(x);
			bel[x]=dccc;
		}while(x!=u);
	}
}

int fa[maxn],sz[maxn];
void dfs1(int u){
	sz[u]=dcc[u].size();
	for(auto &t:g1[u]){
		int v=t.first;
		if(v==fa[u])
			continue;
		fa[v]=u;
		dfs1(v);
		sz[u]+=sz[v];
		if(a[n-sz[v]]!=a[n-sz[v]+1]){
			val[t.second]=1;
		}
		if(a[sz[v]]!=a[sz[v]+1]){
			val[t.second^1]=1;
		}
	}
}

int dp[maxn][2],p[maxn][2];
int d=0;
int x,y;
void dfs2(int u){
	dp[u][0]=dp[u][1]=0;
	p[u][0]=p[u][1]=u;
	for(auto t:g1[u]){
		int v=t.first,id=t.second;
		if(v==fa[u])
			continue;
		dfs2(v);
		if(dp[u][1]+val[id]+dp[v][0]>=d){
			d=dp[u][1]+val[id]+dp[v][0];
			x=p[u][1];y=p[v][0];
		}
		if(dp[v][1]+val[id^1]+dp[u][0]>=d){
			d=dp[v][1]+val[id^1]+dp[u][0];
			x=p[v][1];y=p[u][0];
		}
		if(val[id]+dp[v][0]>=dp[u][0]){
			dp[u][0]=val[id]+dp[v][0];
			p[u][0]=p[v][0];
		}
		if(dp[v][1]+val[id^1]>=dp[u][1]){
			dp[u][1]=dp[v][1]+val[id^1];
			p[u][1]=p[v][1];
		}
	}
}

bool f[maxn];
void dfs3(int u){
	for(auto t:g1[u]){
		int v=t.first;
		if(v==fa[u])
			continue;
		fa[v]=u;
		dfs3(v);
	}
}
int cc=0;
int ans[maxn];
void dfs4(int u){
	for(int x:dcc[u])
		ans[x]=a[++cc];
	for(auto t:g1[u]){
		int v=t.first,w=val[t.second];
		if(v==fa[u])
			continue;
		if(f[v]&&w)
			continue;
		dfs4(v);
	}
	for(auto t:g1[u]){
		int v=t.first,w=val[t.second];
		if(v==fa[u])
			continue;
		if(f[v]&&w)
			dfs4(v);
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			int u,v;
			cin>>u>>v;
			g[u].push_back({v,i*2-2});
			g[v].push_back({u,i*2-1});
		}
		for(int i=1;i<=n;i++)
			cin>>a[i];
		
		tarjan(1,-1);
		
		for(int u=1;u<=n;u++){
			for(auto t:g[u]){
				int v=t.first;
				if(bel[u]<bel[v]){
					g1[bel[u]].push_back({bel[v],ec++});
					g1[bel[v]].push_back({bel[u],ec++});
				}
			}
		}
		
		sort(a+1,a+n+1);
		dfs1(1);
		for(int i=1;i<=n;i++)
			tmp[i]=a[i];
		c=unique(tmp+1,tmp+n+1)-(tmp+1);
		
		d=0;
		dfs2(1);
		if(d>=c-1){
			cout<<"Yes"<<endl;
			for(int i=1;i<=dccc;i++)
				fa[i]=0;
			dfs3(x);
			for(int p=y;p!=x;p=fa[p])
				f[p]=1;
			dfs4(x);
			for(int i=1;i<=n;i++)
				cout<<ans[i]<<' ';
			cout<<endl;
		}
		else{
			cout<<"No"<<endl;
		}
		
		for(int i=1;i<=n;i++)
			g[i].clear();
		for(int i=1;i<=n;i++)
			dfn[i]=low[i]=0;
		s=stack<int>();
		for(int i=1;i<=dccc;i++){
			dcc[i].clear();
			g1[i].clear();
			fa[i]=sz[i]=f[i]=0;
		}
		for(int i=0;i<ec;i++)
			val[i]=0;
		dccc=dfnc=ec=cc=0;
	}
	
	return 0;
} 

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 14464kb

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
1 2 3 4 5 
No
Yes
2 3 2 2 1 
Yes
2 2 1 1 1 
No

result:

ok ok (5 test cases)

Test #2:

score: -100
Time 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 3 1 
No
Yes
1 1 1 
No
No
Yes
2 1 1 1 
No
No
Yes

result: