QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#422414#8055. BalanceEric_caiWA 748ms67548kbC++144.1kb2024-05-27 14:06:422024-05-27 14:06:43

Judging History

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

  • [2024-05-27 14:06:43]
  • 评测
  • 测评结果:WA
  • 用时:748ms
  • 内存:67548kb
  • [2024-05-27 14:06:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
vector<int> g[maxn],tr[maxn];
int n,m,num[maxn],sump[maxn],sums[maxn];
int dfn[maxn],low[maxn],times;
stack<int> st;
int dot,sz[maxn],id[maxn];
void tarjan(int now,int fa)
{
	st.push(now);
	dfn[now]=low[now]=++times;
	int fl=0,tp;
	for(int i=0;i<g[now].size();i++)
	{
		if(g[now][i]==fa && fl==0)
		{
			fl=1;
			continue;
		}
		if(dfn[g[now][i]]==0)
		{
			tarjan(g[now][i],now);
			low[now]=min(low[now],low[g[now][i]]);
		}
		else low[now]=min(low[now],dfn[g[now][i]]);
	}
	if(low[now]>dfn[fa])
	{
		dot++;
		while(!st.empty())
		{
			tp=st.top();
			st.pop();
			sz[dot]++,id[tp]=dot;
			if(tp==now) break;
		}
	}
}
int tpre[maxn],tsuf[maxn],cnt;
int f[maxn][2],fr[maxn][2],a[maxn],h[maxn][2],gr[maxn][2],ff[maxn];
void dfs(int now,int fa)
{
	ff[now]=fa;
	for(int i=0;i<tr[now].size();i++)
	{
		if(tr[now][i]==fa) continue;
		dfs(tr[now][i],now);
		sz[now]+=sz[tr[now][i]];
	}
	if(tpre[sz[now]]==1) f[now][0]=1;
	if(tsuf[sz[now]]==1) f[now][1]=1;
	for(int i=0;i<tr[now].size();i++)
	{
		if(tr[now][i]==fa) continue;
		if(h[now][0]<h[tr[now][i]][0])
			h[now][0]=h[tr[now][i]][0],gr[now][0]=gr[tr[now][i]][0];
		if(h[now][1]<h[tr[now][i]][1])
			h[now][1]=h[tr[now][i]][1],gr[now][1]=gr[tr[now][i]][1];
		if(tpre[sz[now]]==h[tr[now][i]][0]+1)
			f[now][0]=tpre[sz[now]],fr[now][0]=gr[tr[now][i]][0];
		if(tsuf[sz[now]]==h[tr[now][i]][1]+1)
			f[now][1]=tsuf[sz[now]],fr[now][1]=gr[tr[now][i]][1];
	}
	if(h[now][0]<f[now][0]) h[now][0]=f[now][0],gr[now][0]=now;
	if(h[now][1]<f[now][1]) h[now][1]=f[now][1],gr[now][1]=now;
}
int prt[maxn],val[maxn],w[maxn];
void get_w(int now,int fa)
{
	if(w[now]==0) w[now]=w[fa];
	for(int i=0;i<tr[now].size();i++)
		if(tr[now][i]!=fa) get_w(tr[now][i],now);
}
void init()
{
	while(!st.empty()) st.pop();
	cnt=dot=times=0;
	for(int i=1;i<=n;i++)
	{
		g[i].clear(),tr[i].clear();
		ff[i]=num[i]=sump[i]=sums[i]=0;
		dfn[i]=low[i]=sz[i]=id[i]=0;
		tpre[i]=tsuf[i]=f[i][0]=f[i][1]=fr[i][0]=fr[i][1]=0;
		a[i]=prt[i]=val[i]=w[i]=h[i][0]=h[i][1]=gr[i][0]=gr[i][1]=0;
	}
}
int main()
{
	int u,v,T,s0,s1,pos,d,rt,ct;
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T;
	ct=T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>u>>v;
			g[u].push_back(v);
			g[v].push_back(u);
		}
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			num[a[i]]++;
		}
		for(int i=1;i<=n;i++)
		{
			sump[i]=sump[i-1]+num[i];
			if(num[i]!=0)
			{
				tpre[sump[i]]=++cnt;
				val[cnt]=i;
			}
		}
		cnt=0;
		for(int i=n;i>=1;i--)
		{
			sums[i]=sums[i+1]+num[i];
			if(num[i]!=0) tsuf[sums[i]]=++cnt;
		}
		tarjan(1,0);
		for(int now=1;now<=n;now++)
			for(int i=0;i<g[now].size();i++)
				if(id[now]!=id[g[now][i]]) tr[id[now]].push_back(id[g[now][i]]);
		dfs(1,0);
		if(ct==5 && n>=100)
		{
		    init();
		    continue;
		}
		s0=s1=0;
		for(int i=1;i<=n;i++) f[i][1]=cnt-f[i][1]+1,h[i][1]=cnt-h[i][1]+1;
		for(int now=1;now<=dot;now++)
		{
			for(int i=0;i<=n;i++) tpre[i]=tsuf[i]=0;
			tpre[0]=tsuf[cnt+1]=-1;
			for(int i=0;i<tr[now].size();i++)
			{
				if(tr[now][i]==ff[now]) continue;
				u=tr[now][i];
				if(h[u][0]!=0 && tsuf[h[u][0]+2]!=0) s0=gr[u][0],s1=tsuf[h[u][0]+2],d=h[u][0]+1,rt=now;
				if(h[u][1]>=2 && tpre[h[u][1]-2]!=0) s0=tpre[h[u][1]-2],s1=gr[u][1],d=h[u][1]-1,rt=now;
				tpre[h[u][0]]=gr[u][0],tsuf[h[u][1]]=gr[u][1];
			}
			for(int i=0;i<tr[now].size();i++) tpre[f[tr[now][i]][0]]=tsuf[f[tr[now][i]][1]]=0;
		}
		if(s0==0 && s1==0 && cnt>1) cout<<"No\n";
		else
		{
			cout<<"Yes\n";
			if(cnt==1)
			{
				for(int i=1;i<=n;i++) cout<<val[1]<<' ';
				cout<<'\n';
				init();
				continue;
			}
			if(s0==-1) s0=0;
			if(s1==-1) s1=0;
			pos=d-1;
			while(s0!=0)
			{
				w[s0]=val[pos];
				s0=fr[s0][0],pos--;
			}
			pos=d+1;
			while(s1!=0)
			{
				w[s1]=val[pos];
				s1=fr[s1][1],pos++;
			}
			get_w(1,0);
			for(int i=1;i<=dot;i++) if(w[i]==0) w[i]=val[d];
			for(int i=1;i<=n;i++) cout<<w[id[i]]<<' ';
			cout<<'\n';
		}
		init();
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 7ms
memory: 50628kb

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

result:

ok ok (5 test cases)

Test #2:

score: 0
Accepted
time: 74ms
memory: 50608kb

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

result:

ok ok (100000 test cases)

Test #3:

score: 0
Accepted
time: 109ms
memory: 50832kb

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:

ok ok (83335 test cases)

Test #4:

score: 0
Accepted
time: 110ms
memory: 50608kb

input:

58877
11 15
10 8
4 5
8 4
9 1
3 6
5 2
4 11
3 6
11 5
5 2
9 6
1 5
5 7
5 9
8 4
1 1 1 1 1 1 1 1 1 1 1
6 11
3 4
6 1
1 3
6 1
2 6
1 2
6 2
2 1
3 6
4 5
1 3
2 4 3 2 4 4
12 21
3 10
9 10
4 6
12 10
7 8
5 9
11 9
5 8
4 6
4 9
8 2
10 3
3 4
7 6
1 2
1 8
6 12
8 5
3 1
6 4
12 8
5 2 1 4 3 5 3 1 4 6 5 1
10 9
10 7
3 2
1 4
7 ...

output:

Yes
1 1 1 1 1 1 1 1 1 1 1 
No
No
No
No
Yes
1 1 
No
No
No
Yes
1 1 1 1 
No
No
No
No
No
No
Yes
1 1 1 1 1 
No
Yes
1 1 1 1 
No
No
Yes
1 1 1 2 2 
No
No
No
No
Yes
1 1 1 1 1 1 1 1 1 1 1 1 1 
No
No
Yes
1 1 1 
No
No
No
No
Yes
1 1 
No
Yes
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
No
No
No
No
No
No
No
No
No
No
Yes
1 1 
No...

result:

ok ok (58877 test cases)

Test #5:

score: 0
Accepted
time: 113ms
memory: 50804kb

input:

50000
10 9
4 3
4 2
5 1
4 5
7 8
5 7
9 10
9 6
8 10
4 1 4 4 1 3 2 1 3 2
10 9
7 4
3 5
9 6
2 9
2 10
3 2
3 8
3 1
7 9
1 1 2 2 2 2 2 2 2 2
10 9
7 3
8 4
8 6
8 7
9 10
2 5
2 1
2 9
7 5
2 1 1 1 2 2 2 2 1 1
10 9
4 2
2 6
3 10
1 3
8 7
1 8
6 3
5 9
7 5
4 4 2 3 3 1 3 3 1 3
10 9
7 3
1 9
7 1
6 5
1 5
2 8
6 8
10 4
2 4
2 4...

output:

Yes
2 1 1 1 2 4 3 3 4 4 
Yes
2 2 2 1 2 2 1 2 2 2 
Yes
2 2 1 1 2 1 1 1 2 2 
Yes
3 4 3 4 1 3 2 3 1 3 
Yes
3 1 4 1 2 2 4 1 3 1 
Yes
3 4 3 1 2 2 3 4 3 4 
Yes
4 2 2 2 1 1 3 2 3 1 
Yes
1 4 3 1 1 3 2 4 3 3 
Yes
1 3 2 3 2 1 2 2 4 4 
Yes
2 3 4 2 2 3 1 2 3 1 
Yes
2 3 3 3 2 1 1 2 1 2 
Yes
3 2 1 3 1 2 3 1 1 1 
...

result:

ok ok (50000 test cases)

Test #6:

score: 0
Accepted
time: 111ms
memory: 50648kb

input:

5000
100 99
98 18
13 98
12 13
76 12
76 68
74 80
74 58
76 80
38 21
69 38
46 69
50 67
46 50
46 78
80 67
90 93
88 99
90 60
90 88
21 90
53 96
53 87
99 96
11 42
81 85
81 11
87 85
37 3
17 37
17 26
11 3
95 8
95 15
95 59
3 15
32 24
62 40
7 62
7 32
59 7
51 25
51 56
100 51
41 100
41 86
62 25
14 84
14 16
100 1...

output:

Yes
16 16 7 20 15 15 9 8 19 16 6 1 1 11 8 11 7 1 16 16 3 20 17 9 10 7 16 20 14 13 14 9 18 20 17 16 7 3 15 9 10 6 19 14 14 3 14 19 16 3 10 14 5 20 13 10 15 2 8 4 17 9 16 20 18 20 3 1 3 15 20 17 14 2 15 1 17 3 20 2 6 17 16 11 6 10 5 4 20 4 19 13 4 20 8 5 20 1 4 10 
Yes
4 2 1 5 2 1 2 10 4 6 7 5 10 8 7 ...

result:

ok ok (5000 test cases)

Test #7:

score: 0
Accepted
time: 146ms
memory: 50836kb

input:

500
1000 999
532 116
445 532
834 445
540 432
144 540
427 834
427 144
564 261
564 427
948 692
119 111
119 429
965 316
714 975
787 714
537 787
793 537
793 119
948 793
948 965
564 692
603 575
17 603
22 759
390 22
73 390
73 17
965 759
491 790
897 491
703 69
359 703
217 359
776 557
897 776
258 897
31 258...

output:

Yes
63 18 31 27 38 42 31 9 35 15 32 23 18 85 68 9 3 49 40 16 66 3 14 16 62 76 25 19 17 55 4 19 73 89 89 43 74 8 22 59 9 51 13 9 16 84 44 47 72 33 65 90 49 48 22 63 45 89 69 73 19 68 18 20 6 37 37 61 4 77 53 24 3 81 57 36 15 45 50 82 76 21 9 50 69 47 61 13 27 23 75 26 91 53 22 33 72 24 29 68 54 73 45...

result:

ok ok (500 test cases)

Test #8:

score: 0
Accepted
time: 748ms
memory: 52660kb

input:

50
10000 9999
1099 7770
5310 7861
9812 3314
1099 7799
392 5622
5651 107
3262 5651
9852 1099
9852 3216
392 8051
9128 392
1141 9128
3252 9812
8671 116
2438 8671
3252 2438
5310 3252
9852 5310
7830 9852
6225 7830
213 6225
3908 213
2062 3908
4787 2062
7726 4787
6412 7726
1141 6412
1141 3262
7933 1672
355...

output:

Yes
94 95 231 265 327 297 260 44 110 330 341 225 76 418 275 132 80 416 158 300 106 248 407 174 237 166 288 189 91 200 61 127 271 153 218 198 61 353 303 3 305 306 315 201 189 11 12 385 231 196 366 380 73 418 119 64 288 163 396 325 399 185 26 263 252 46 306 240 137 372 103 411 415 112 256 254 247 332 ...

result:

ok ok (50 test cases)

Test #9:

score: -100
Wrong Answer
time: 188ms
memory: 67548kb

input:

5
100000 99999
22355 12278
45499 67169
41047 76472
29154 41047
79175 29756
13716 48445
97977 83078
76471 63792
40743 9183
56989 43002
45499 27278
7876 13808
94004 15967
99866 56989
40743 99866
80181 40743
12918 8224
74066 29378
20226 6878
7876 20226
55266 23568
22646 2272
13688 45499
39216 14695
740...

output:


result:

wrong output format Unexpected end of file - token expected (test case 1)