QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#720210#8048. Roman Masterqzez#RE 0ms0kbC++143.1kb2024-11-07 11:13:272024-11-07 11:13:32

Judging History

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

  • [2024-11-07 11:13:32]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-07 11:13:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;using pii=pair<int,int>;
const int N=5e5+5,INF=1e9+7;
int n;
vector<int> S[N],G[N];
int scc[N],m;
namespace Tarjan{
	int dfn[N],low[N],dh,st[N],sh;
	void clr(){
		for(int i=0;i<=n+1;i++) dfn[i]=low[i]=st[i]=0;dh=sh=m=0;
	}
	void tarjan(int x,int La){
		dfn[x]=low[x]=++dh;st[++sh]=x;
		int flag=0;
		for(int i:S[x])if(i^La||flag){
			if(!dfn[i]) tarjan(i,x),low[x]=min(low[x],low[i]);
			else low[x]=min(low[x],dfn[i]);
		}else flag=1;
		if(low[x]>=dfn[x]){
			++m;
			while(st[sh+1]^x) scc[st[sh]]=m,sh--;
		}
	}
}
int A[N],B[N];
int siz[N],sum[N],fa[N],bg[N],bh,en[N],d[N];
void make(int x,int La){
	sum[x]=siz[x];fa[x]=La;bg[x]=++bh;d[x]=d[La]+1;
	for(int i:G[x]) if(i^La) make(i,x),sum[x]+=sum[i];
	en[x]=bh;
}
int col[N];
void push(int x,int La,int y,int w){
	if(x==y) return;
	col[x]=w;
	for(int i:G[x]) if(i^La) push(i,x,y,w);
}
int check(int x,int La,int *A,int l,int r){
	if(l>r) return 1;
	int pt=r;
	for(int i=l;i<r;i++) if(A[i]^A[i+1]) pt=i;
	if(pt==r) return push(x,La,0,A[l]),1;
	queue<pii> Q;Q.emplace(x,La);
	while(!Q.empty()){
		auto [a,b]=Q.front();Q.pop();
		int ss=(b==fa[a]?sum[a]:n-sum[b]);
		if(ss==(r-pt)&&check(a,b,A,pt+1,r)){
			push(x,La,a,A[l]);
			return 1;
		}
		for(int i:G[a])if(i^b){
			int ss=(a==fa[i]?sum[i]:n-sum[a]);
			if(ss>=r-pt) Q.emplace(i,a);
		}
	}
	return 0;

}
void print(){
	puts("Yes");
	for(int i=1;i<=n;i++) printf("%d%c",col[scc[i]]," \n"[i==n]);
}
void Solve(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) S[i].clear(),G[i].clear();
	while(m--){
		int x,y;scanf("%d%d",&x,&y);
		S[x].push_back(y);S[y].push_back(x);
	}
	Tarjan::clr();
	Tarjan::tarjan(1,0);
	for(int i=1;i<=n;i++) for(int j:S[i]) if(scc[i]^scc[j]){
		G[scc[i]].push_back(scc[j]),G[scc[j]].push_back(scc[i]);
	}
	for(int i=1;i<=n;i++) scanf("%d",&A[i]);
	sort(A+1,A+n+1);
	copy(A+1,A+n+1,B+1);reverse(B+1,B+n+1);
	for(int i=1;i<=m;i++) siz[i]=0;
	for(int i=1;i<=n;i++) siz[scc[i]]++;
	bh=0;
	make(1,0);
	int p1=n;
	for(int i=n-1;i*2>=n;i--) if(A[i]^A[i+1]) p1=i;
	for(int i=1;i<=n;i++) if(sum[i]==p1&&check(i,fa[i],B,n-p1+1,n)&&check(fa[i],i,A,p1+1,n)){
		return print();
	}
	int p2=n;
	for(int i=n-1;i*2>=n;i--) if(B[i]^B[i+1]) p2=i;
	for(int i=1;i<=n;i++) if(sum[i]==p2&&check(i,fa[i],A,n-p2+1,n)&&check(fa[i],i,B,p2+1,n)){
		return print();
	}
	p1=p2=0;
	for(int i=1;i*2<=n;i++) if(A[i]^A[i+1]) p1=i;
	for(int i=1;i*2<=n;i++) if(B[i]^B[i+1]) p2=i;
	vector<int> s1,s2;
	for(int i=1;i<=n;i++) if(sum[i]==p1&&check(i,fa[i],B,n-p1+1,n)) s1.push_back(i);
	for(int i=1;i<=n;i++) if(sum[i]==p2&&check(i,fa[i],A,n-p2+1,n)) s2.push_back(i);
	for(int i:s1) for(int j:s2){
		int x=i,y=j;
		if(d[x]>d[y]) swap(x,y);
		if(bg[x]<=bg[y]&&bg[y]<=en[x]) continue;
		fill(col+1,col+m+1,A[p1+1]);
		check(i,fa[i],B,n-p1+1,n);
		check(j,fa[j],A,n-p2+1,n);
		return print();
	}
	puts("No");
}
int main(){
	freopen("1.in","r",stdin);freopen("1.out","w",stdout);
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

3
II
IVI
VIIIIIV

output:


result: