QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#453838#8055. Balanceucup-team3282RE 201ms17124kbC++143.3kb2024-06-24 12:52:582024-06-24 12:52:58

Judging History

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

  • [2024-06-24 12:52:58]
  • 评测
  • 测评结果:RE
  • 用时:201ms
  • 内存:17124kb
  • [2024-06-24 12:52:58]
  • 提交

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;
//			cout<<"11 "<<u<<' '<<v<<endl;
		}
		if(a[sz[v]]!=a[sz[v]+1]){
			val[t.second^1]=1;
//			cout<<"11 "<<v<<' '<<u<<endl;
		}
	}
}

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];
deque <int> q;
void bfs(){
	q.push_back(x);
	while(!q.empty()){
		int u=q.front();
		q.pop_front();
		for(int v:dcc[u])
			ans[v]=a[++cc];
		for(auto t:g1[u]){
			int v=t.first,w=val[t.second];
			if(v==fa[u])
				continue;
			if(f[v]&&w)
				q.push_back(v);
			else
				q.push_front(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;x=y=1;
		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;
			bfs();
			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;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 119ms
memory: 14352kb

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: 0
Accepted
time: 136ms
memory: 15532kb

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
3 4 4 4 5 4 5 2 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: 140ms
memory: 15520kb

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: 171ms
memory: 15452kb

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

result:

ok ok (50000 test cases)

Test #6:

score: 0
Accepted
time: 148ms
memory: 15292kb

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: 151ms
memory: 14860kb

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: 201ms
memory: 17124kb

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
Runtime Error

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: