题解:[JOISC 2022] 监狱

洛谷P9520

Posted by TH911 on August 12, 2025

题目传送门

朴素图论建模

记 $s_x,t_x$ 分别表示囚犯 $x$ 的起点、终点,$\set{s_x\rightarrow t_x}$ 表示囚犯 $x$ 走过的点集。

对于囚犯 $u,v$:

  • 若 $s_u\in\set{s_v\rightarrow t_v}$,则 $u$ 比 $v$ 先走,否则会被挡住。
  • 若 $t_u\in\set{s_v\rightarrow t_v}$,则 $v$ 比 $u$ 先走,否则会被挡住。

因此,考虑存储上述性质,考虑使用图。

新建一张图,图上有向边 $u\rightarrow v$ 表示 $u$ 比 $v$ 先走

如果存在环,则无解。

否则,可行的解即按照拓扑序走动。

对于每一个囚犯 $x$,暴力查找其路径上的起点/终点对应囚犯,连边即可。

时间复杂度:$\mathcal O(nm)$。期望得分:$\text{72pts}$。特判链上情况可以做到 $\text{77pts}$。

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
constexpr const int N=120000,M=N;
//s[i]:i 为起点的编号 
int n,m,s[N+1],t[N+1];
vector<int>g[N+1],build[M+1];
struct people{
	int s,t;
}a[M+1];
namespace base{
	int father[N+1],depth[N+1];
	void dfs(int x,int fx){
		depth[x]=depth[fx]+1;
		father[x]=fx;
		for(int i:g[x]){
			if(i==fx){
				continue;
			}
			dfs(i,x);
		}
	}
	int main(){
		dfs(1,0);
		return 0;
	}
	void clear(){
		for(int i=1;i<=n;i++){
			g[i].resize(0);
			build[i].resize(0);
		}
		memset(s,0,sizeof(s));
		memset(t,0,sizeof(t));
	}
}
void pre(){
	for(int i=1;i<=m;i++){
		int u=a[i].s,v=a[i].t;
		while(u!=v){
			if(base::depth[u]<base::depth[v]){
				swap(u,v);
			}
			if(s[u]&&s[u]!=i){
				build[s[u]].push_back(i);
			}
			if(t[u]&&t[u]!=i){
				build[i].push_back(t[u]);
			}
			u=base::father[u];
		}
		if(s[u]&&s[u]!=i){
			build[s[u]].push_back(i);
		}
		if(t[u]&&t[u]!=i){
			build[i].push_back(t[u]);
		}
	}
}
bool checkRing(){
	static int in[M+1];
	static bool vis[M+1];
	memset(in,0,sizeof(in));
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=m;i++){
		for(int j:build[i]){
			in[j]++;
		}
	}
	queue<int>q;
	for(int i=1;i<=m;i++){
		if(!in[i]){
			q.push(i);
		}
	}
	while(q.size()){
		int x=q.front();q.pop();
		vis[x]=true;
		for(int i:build[x]){
			in[i]--;
			if(!in[i]){
				q.push(i);
			}
		}
	}
	for(int i=1;i<=m;i++){
		if(!vis[i]){
			return true;
		}
	}
	return false;
} 
int main(){
	/*freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);*/
	
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int T;
	cin>>T;
	while(T--){
		base::clear();
		cin>>n;
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			g[u].push_back(v);
			g[v].push_back(u);
		}
		base::main();
		cin>>m;
		for(int i=1;i<=m;i++){
			cin>>a[i].s>>a[i].t;
			s[a[i].s]=i,t[a[i].t]=i;
		}
		pre();
		if(checkRing()){
			cout<<"No\n";
		}else{
			cout<<"Yes\n";
		}
	}
	
	cout.flush();
	
	/*fclose(stdin);
	fclose(stdout);*/
	return 0;
}
/*
2
7
1 2
2 3
3 4
4 5
3 6
6 7
2
4 1
5 7
4
1 2
1 3
1 4
3
2 3
3 4
4 2

Yes
No
*/

线段树优化建图

参见此处