QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#447380#8055. Balanceliqingyang#WA 67ms10808kbC++143.6kb2024-06-18 12:03:162024-06-18 12:03:17

Judging History

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

  • [2024-06-18 12:03:17]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:10808kb
  • [2024-06-18 12:03:16]
  • 提交

answer

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int T,n,m,x[200010],y[200010],a[100010],ra[100010];
int d[100010],fa[100010],f[100010];
int num,A[100010],B[100010];
int Num,id[100010],ID[100010],Size[100010];
int F[100010],G[100010],ans[100010];
vector<int> e[100010],vf[100010],vg[100010];
int find(int x)
{
	return f[x]==x?x
	:f[x]=find(f[x]);
}
void dfs(int now)
{
	for(int i=0;i<e[now].size();i++)
	{
		int t=e[now][i];
		if(d[t])
		{
			if(d[t]>d[now])
			{
				int x=find(t);
				while(d[x]>d[now])
				{
					x=f[x]=find(fa[x]);
				}
			}
			continue;
		}
		d[t]=d[now]+1;
		fa[t]=now;
		dfs(t);
	}
}
inline int add(int now,int *A,vector<int> *v)
{
	int p=lower_bound(A+1,A+num+1,Size[now])-A;
	if(p>num||A[p]!=Size[now])
	{
		return 0;
	}
	if(p==1||(!v[p-1].empty()&&v[p-1].back()>id[now]))
	{
		v[p].emplace_back(id[now]);
		return p;
	}
	return 0;
}
void dfs(int now,int fa)
{
	ID[id[now]=++Num]=now;
	for(int i=0;i<e[now].size();i++)
	{
		int t=e[now][i];
		if(t==fa)
		{
			continue;
		}
		dfs(t,now);
		Size[now]+=Size[t];
	}
	F[now]=add(now,A,vf);
	G[now]=add(now,B,vg);
}
inline bool judge(int l,int r,const vector<int> &v)
{
	int p=lower_bound(v.begin(),v.end(),l)-v.begin();
	return p<v.size()&&v[p]<=r;
}
inline void print(int p,int k,int *A,vector<int> *v,int *a)
{
	while(--k)
	{
		int P=ID[v[k][lower_bound(v[k].begin(),
		v[k].end(),id[p])-v[k].begin()]];
		for(int i=id[p];i<id[P];ans[i++]=a[A[k+1]]);
		for(int i=id[P]+Size[P];i<id[p]+Size[p];ans[i++]=a[A[k+1]]);
		p=P;
	}
	for(int i=id[p];i<id[p]+Size[p];ans[i++]=a[A[1]]);
}
inline bool judge(int now,int t,int *A,int *B,int *F,
vector<int> *vf,vector<int> *vg,int *a,int *ra)
{
	if(!F[t])
	{
		return 0;
	}
	int T=num-F[t]-1;
	if(!T||judge(id[now],id[t]-1,vg[T])
	||judge(id[t]+Size[t],id[now]+Size[now]-1,vg[T]))
	{
		cout<<"Yes"<<'\n';
		print(t,F[t],A,vf,a);
		if(T&&judge(1,id[t]-1,vg[T]))
		{
			print(ID[vg[T][lower_bound(vg[T].begin(),vg[T].end(),
			id[now])-vg[T].begin()]],T,B,vg,ra);
		}
		else if(T)
		{
			print(ID[vg[T][lower_bound(vg[T].begin(),vg[T].end(),
			id[t]+Size[t])-vg[T].begin()]],T,B,vg,ra);
		}
		for(int i=1;i<=n;i++)
		{
			int p=id[find(i)];
			cout<<(ans[p]?ans[p]:a[A[F[t]+1]])<<" ";
		}
		cout<<'\n';
		return 1;
	}
	return 0;
}
bool solve(int now,int fa)
{
	for(int i=0;i<e[now].size();i++)
	{
		int t=e[now][i];
		if(t==fa)
		{
			continue;
		}
		if(solve(t,now))
		{
			return 1;
		}
		if(judge(now,t,A,B,F,vf,vg,a,ra)
		||judge(now,t,B,A,G,vg,vf,ra,a))
		{
			return 1;
		}
	}
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			e[i].clear();
			d[i]=0,f[i]=i;
			Size[i]=0;
			vf[i].clear();
			vg[i].clear();
			ans[i]=0;
		}
		for(int i=1;i<=m;i++)
		{
			cin>>x[i]>>y[i];
			e[x[i]].emplace_back(y[i]);
			e[y[i]].emplace_back(x[i]);
		}
		d[1]=1,dfs(1);
		for(int i=1;i<=n;i++)
		{
			e[i].clear();
		}
		for(int i=1;i<=m;i++)
		{
			int X=find(x[i]),Y=find(y[i]);
			if(X==Y)
			{
				continue;
			}
			e[X].emplace_back(Y);
			e[Y].emplace_back(X);
		}
		for(int i=1;i<=n;i++)
		{
			Size[find(i)]++;
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{
			ra[i]=a[i];
		}
		reverse(ra+1,ra+n+1);
		num=0;
		for(int i=1;i<=n;i++)
		{
			if(i==n||a[i+1]>a[i])
			{
				A[++num]=i;
			}
		}
		num=0;
		for(int i=n;i;i--)
		{
			if(i==1||a[i-1]<a[i])
			{
				B[++num]=n-i+1;
			}
		}
		Num=0,dfs(1,1);
		if(!solve(1,1))
		{
			cout<<"No"<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10752kb

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
Wrong Answer
time: 67ms
memory: 10808kb

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:

wrong answer ans finds an answer, but out doesn't (test case 3)