QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356211#8055. BalancezhouhuanyiWA 83ms66972kbC++144.8kb2024-03-17 16:45:222024-03-17 16:45:22

Judging History

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

  • [2024-03-17 16:45:22]
  • 评测
  • 测评结果:WA
  • 用时:83ms
  • 内存:66972kb
  • [2024-03-17 16:45:22]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 400000
using namespace std;
const int inf=(int)(1e9);
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
struct reads
{
	int num,data;
};
int T,n,m,cater,sx,sy,dst,fa[N+1],ans[N+1],cl[N+1],sz[N+1],belong[N+1],rt[N+1],dfn[N+1],low[N+1],leng,a[N+1],tong[N+1],length,num[N+1],num2[N+1],dp[N+1],dp2[N+1],rk[N+1],rk2[N+1];
bool used[N+1],cut[N+1];
vector<reads>E[N+1];
vector<int>ES[N+1];
vector<int>v[N+1];
vector<int>st[N+1];
int find(int x)
{
	if (rt[x]==x) return x;
	return rt[x]=find(rt[x]);
}
void unionn(int x,int y)
{
	rt[x]=y;
	return;
}
void add(int x,int y,int z)
{
	E[x].push_back((reads){y,z}),E[y].push_back((reads){x,z});
	return;
}
void add_edge(int x,int y)
{
	ES[x].push_back(y),ES[y].push_back(x);
	return;
}
void tarjan(int x,int pv)
{
	dfn[x]=low[x]=++leng;
	for (int i=0;i<E[x].size();++i)
	{
		if (!dfn[E[x][i].num])
		{
			tarjan(E[x][i].num,E[x][i].data),low[x]=min(low[x],low[E[x][i].num]);
			if (low[E[x][i].num]>dfn[x]) cut[E[x][i].data]=1;
		}
		else if (E[x][i].data!=pv) low[x]=min(low[x],dfn[E[x][i].num]);
	}
	return;
}
void dfs(int x)
{
	used[x]=1,sz[x]=v[x].size();
	for (int i=0;i<ES[x].size();++i)
		if (!used[ES[x][i]])
		{
			if (x==1) cl[ES[x][i]]=ES[x][i];
			else cl[ES[x][i]]=cl[x];
			fa[ES[x][i]]=x,dfs(ES[x][i]),sz[x]+=sz[ES[x][i]],dp[x]=max(dp[x],dp[ES[x][i]]),dp2[x]=min(dp2[x],dp2[ES[x][i]]);
		}
	if (num[sz[x]]&&dp[x]+1>=num[sz[x]]) dp[x]=max(dp[x],num[sz[x]]),rk[x]=num[sz[x]];
	if (num2[sz[x]]&&dp2[x]-1<=num2[sz[x]]) dp2[x]=min(dp2[x],num2[sz[x]]),rk2[x]=num2[sz[x]];
	return;
}
void dfs2(int x,int d)
{
	for (int i=0;i<v[x].size();++i) ans[v[x][i]]=a[tong[d]];
	for (int i=0;i<ES[x].size();++i)
		if (fa[ES[x][i]]==x)
			dfs2(ES[x][i],d);
	return;
}
void dfs3(int x,int d)
{
	bool op=(d!=1);
	for (int i=0;i<v[x].size();++i) ans[v[x][i]]=a[tong[d]];
	for (int i=0;i<ES[x].size();++i)
		if (fa[ES[x][i]]==x)
		{
			if (dp[ES[x][i]]>=d-1&&op) dfs3(ES[x][i],d-(rk[ES[x][i]]==d-1)),op=0;
			else dfs2(ES[x][i],d);
		}
	return;
}
void dfs4(int x,int d)
{
	bool op=(d!=length);
	for (int i=0;i<v[x].size();++i) ans[v[x][i]]=a[tong[d]];
	for (int i=0;i<ES[x].size();++i)
		if (fa[ES[x][i]]==x)
		{
			if (dp2[ES[x][i]]<=d+1&&op) dfs4(ES[x][i],d+(rk2[ES[x][i]]==d+1)),op=0;
			else dfs2(ES[x][i],d);
		}
	return;
}
void dfs5(int x,int d)
{
	if (rk[x]==d)
	{
		dfs3(x,d);
		return;
	}
	for (int i=0;i<ES[x].size();++i)
		if (fa[ES[x][i]]==x&&dp[ES[x][i]]>=d)
		{
			dfs5(ES[x][i],d);
			return;
		}
	return;
}
void dfs6(int x,int d)
{
	if (rk2[x]==d)
	{
		dfs4(x,d);
		return;
	}
	for (int i=0;i<ES[x].size();++i)
		if (fa[ES[x][i]]==x&&dp2[ES[x][i]]<=d)
		{
			dfs6(ES[x][i],d);
			return;
		}
	return;
}
int main()
{
	int x,y,maxn,maxer,minn,miner;
	T=read();
	while (T--)
	{
		n=read(),m=read(),leng=length=cater=sx=sy=dst=0;
		for (int i=1;i<=n;++i) E[i].clear(),dfn[i]=low[i]=num[i]=num2[i]=0,rt[i]=i,st[i].clear();
		for (int i=1;i<=m;++i) x=read(),y=read(),add(x,y,i),cut[i]=0;
		tarjan(1,0);
		for (int i=1;i<=n;++i) a[i]=read();
		sort(a+1,a+n+1);
		for (int i=1;i<=n;++i)
			if (i==n||a[i]!=a[i+1])
				tong[++length]=i;
		for (int i=1;i<=length;++i) num[tong[i]]=i,num2[n-tong[i-1]]=i;
		for (int i=1;i<=n;++i)
			for (int j=0;j<E[i].size();++j)
				if (i<E[i][j].num&&!cut[E[i][j].data]&&find(i)!=find(E[i][j].num))
					unionn(find(i),find(E[i][j].num));
		for (int i=1;i<=n;++i)
			if (find(i)==i)
				belong[i]=++cater;
		for (int i=1;i<=cater;++i) ES[i].clear(),v[i].clear(),used[i]=dp[i]=rk[i]=fa[i]=0,dp2[i]=rk2[i]=length+1;
		for (int i=1;i<=n;++i) v[belong[find(i)]].push_back(i);
		for (int i=1;i<=n;++i)
			for (int j=0;j<E[i].size();++j)
				if (i<E[i][j].num&&cut[E[i][j].data])
					add_edge(belong[find(i)],belong[find(E[i][j].num)]);
		dfs(1);
		if (length==1) sx=sy=cater+1,dst=1;
		for (int i=2;i<=cater;++i)
		{
			if (rk[i]==length-1) sx=i,sy=cater+1,dst=length;
			if (rk2[i]==2) sx=cater+1,sy=i,dst=1;
		}
		if (!sx)
		{
			for (int i=1;i<=cater;++i)
			{
				maxn=-inf,minn=inf,maxer=miner=0;
				for (int j=0;j<ES[i].size();++j)
					if (fa[ES[i][j]]==i)
					{
						if (maxn+2>=dp2[ES[i][j]]) sx=maxer,sy=ES[i][j],dst=maxn+1;
						if (minn-2<=dp[ES[i][j]]) sx=ES[i][j],sy=miner,dst=minn-1;
						if (dp[ES[i][j]]>maxn) maxn=dp[ES[i][j]],maxer=ES[i][j];
						if (dp2[ES[i][j]]<minn) minn=dp2[ES[i][j]],miner=ES[i][j];
					}
			}
		}
		if (!sx)
		{
			puts("No");
			continue;
		}
		for (int i=1;i<=n;++i) ans[i]=a[tong[dst]];
		if (dst!=1) dfs5(sx,dst-1);
		if (dst!=length) dfs6(sy,dst+1);
		puts("Yes");
		for (int i=1;i<=n;++i) printf("%d ",ans[i]);
		puts("");
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 66816kb

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: 0
Accepted
time: 65ms
memory: 66972kb

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
1 1 
Yes
1 1 
Yes
1 1 
Yes
1 1 1 1 
No
Yes
1 1 1 1 1 
Yes
1 3 1 1 1 
Yes
1 1 1 
Yes
1 2 
Yes
1 1 1 1 1 
Yes
1 2 
No
Yes
1 1 
Yes
1 1 1 
Yes
1 1 
Yes
1 1 1 1 
Yes
1 1 
Yes
2 2 2 2 2 
Yes
1 1 1 1 1 
Yes
1 1 
Yes
1 1 1 2 
No
Yes
1 1 
No
Yes
1 1 
N...

result:

ok ok (100000 test cases)

Test #3:

score: -100
Wrong Answer
time: 83ms
memory: 65648kb

input:

83335
9 17
1 4
8 9
5 2
1 3
2 7
1 7
2 8
6 7
2 4
1 8
5 8
7 1
8 6
4 6
4 7
6 9
7 9
7 3 4 4 7 4 2 4 8
6 9
3 1
1 2
3 5
1 2
3 4
4 5
6 3
6 1
6 2
4 3 2 2 1 3
9 8
9 3
5 7
5 9
2 6
1 8
4 1
4 2
4 3
4 2 5 3 4 5 4 5 4
6 7
2 1
1 5
6 1
3 1
6 5
2 4
5 3
2 1 2 1 2 1
4 6
2 1
4 2
1 4
1 2
1 4
3 1
2 2 4 2
6 10
2 3
3 5
2 1
...

output:

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

result:

wrong answer 8-th smallest numbers are not same (test case 1526)