답안 #609705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
609705 2022-07-27T19:31:29 Z HamletPetrosyan Jail (JOI22_jail) C++17
72 / 100
5000 ms 427108 KB
/// #if (code == true)

#include <bits/stdc++.h>
using namespace std;

#define pll pair<long long, long long>
#define len(a) ((int)((a).size()))
#define all(a) a.begin(), a.end()
#define add push_back
#define mkp make_pair
#define ll long long
#define fr first
#define sc second

const long long INF = 1000000000ll * 1000000003ll;
const long long MOD = 1000000007ll;
const int N = 2e5 + 5;

ll n, m, s[N], t[N];
vector<int> g[N], dir[N], f[N], h[N];
int color[N];

ll ls[N][25], tin[N], tout[N], timer = 0;

void dfs(int v, int p){
	ls[v][0] = p;
	for(int i = 1; i <= 20; i++){
		ls[v][i] = ls[ls[v][i - 1]][i - 1];
	}

	tin[v] = ++timer;
	for(int i = 0; i < len(g[v]); i++){
		int to = g[v][i];
		if(to == p) continue;
		dfs(to, v);
	}
	tout[v] = ++timer;
}

bool isa(int a, int b){
	return (tin[a] <= tin[b] && tout[b] <= tout[a]);
}

int lsa(int a, int b){
	if(isa(a, b)) return a;
	if(isa(b, a)) return b;
	for(int i = 20; i >= 0; i--){
		if(isa(ls[a][i], b)) continue;
		a = ls[a][i];
	}
	return ls[a][0];
}

void check(int i, int j){
	int a = lsa(s[i], t[i]);
	int b = lsa(s[j], t[j]);
	if(isa(a, s[j]) && (isa(s[j], s[i]) || isa(s[j], t[i]))){
		dir[i].add(j);
	}
	if(isa(b, t[i]) && (isa(t[i], s[j]) || isa(t[i], t[j]))){
		dir[i].add(j);
	}
}

bool check_cycle(int v){
	color[v] = 1;
	for(int i = 0; i < len(dir[v]); i++){
		int to = dir[v][i];
		if(color[to] == 2) continue;
		if(color[to] == 1) {
			return true;
		}
		if(check_cycle(to)){
			return true;
		}
	}
	color[v] = 2;
	return false;
}

void solve(){
	timer = 0;

	cin >> n;
	for(int i = 1; i <= n; i++){
		g[i].clear();
		h[i].clear();
		f[i].clear();
	}
	for(int i = 0; i < m; i++){
		dir[i].clear();
		color[i] = 0;
	}

	int u, v;
	for(int i = 1; i < n; i++){
		cin >> u >> v;
		g[u].add(v);
		g[v].add(u);
	}
	dfs(1, 1);
	cin >> m;
	for(int i = 0; i < m; i++){
		cin >> s[i] >> t[i];
		f[s[i]].add(i);
		h[t[i]].add(i);
	}
	int lc, now;
	for(int i = 0; i < m; i++){
		lc = lsa(s[i], t[i]);
		now = s[i];
		if(len(h[s[i]]) && h[s[i]][0] != i){
			dir[h[s[i]][0]].add(i);
		}
		if(s[i] != lc){
			do {
				now = ls[now][0];
				if(len(f[now]) && f[now][0] != i){
					dir[i].add(f[now][0]);
				}
				if(len(h[now]) && h[now][0] != i){
					dir[h[now][0]].add(i);
				}
			} while(now != lc);
		}
		now = t[i];
		while(now != lc){
			if(len(f[now]) && f[now][0] != i){
				dir[i].add(f[now][0]);
			}
			if(len(h[now]) && h[now][0] != i){
				dir[h[now][0]].add(i);
			}
			now = ls[now][0];
		}
	}

//	for(int i = 0; i < m; i++){
//		cout << i << ": ";
//		for(int j = 0; j < len(dir[i]); j++){
//			cout << dir[i][j] << " ";
//		}
//		cout << endl;
//	}

//	for(int i = 0; i < m; i++){
//		for(int j = 0; j < m; j++){
//			if(i == j) continue;
//			check(i, j);
//		}
//	}
	for(int i = 0; i < m; i++){
		if(color[i] == 0) {
			if(check_cycle(i)){
				cout << "No\n";
				return;
			}
		}
	}

	cout << "Yes\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int _ = 1;
	// cout << fixed;
	// cout.precision(15);

	cin >> _ ;
	while(_--) solve();
	return 0;
}

/// #else
/// #include <bits/stdc++.h> using namespace std; int main() { cout << "Hello World!"; }
/// #endif
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 24 ms 19188 KB Output is correct
5 Correct 33 ms 19156 KB Output is correct
6 Correct 11 ms 19156 KB Output is correct
7 Correct 10 ms 19156 KB Output is correct
8 Correct 11 ms 19292 KB Output is correct
9 Correct 118 ms 23224 KB Output is correct
10 Correct 893 ms 57136 KB Output is correct
11 Correct 16 ms 19156 KB Output is correct
12 Correct 49 ms 19196 KB Output is correct
13 Correct 187 ms 89576 KB Output is correct
14 Correct 219 ms 103232 KB Output is correct
15 Execution timed out 5090 ms 427108 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 12 ms 19152 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 13 ms 19216 KB Output is correct
5 Correct 10 ms 19096 KB Output is correct
6 Correct 11 ms 19156 KB Output is correct
7 Correct 11 ms 19212 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 11 ms 19168 KB Output is correct
10 Correct 11 ms 19156 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19172 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 12 ms 19152 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 13 ms 19216 KB Output is correct
5 Correct 10 ms 19096 KB Output is correct
6 Correct 11 ms 19156 KB Output is correct
7 Correct 11 ms 19212 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 11 ms 19168 KB Output is correct
10 Correct 11 ms 19156 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19172 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
14 Correct 10 ms 19028 KB Output is correct
15 Correct 9 ms 19100 KB Output is correct
16 Correct 11 ms 19228 KB Output is correct
17 Correct 13 ms 19156 KB Output is correct
18 Correct 14 ms 19204 KB Output is correct
19 Correct 13 ms 19132 KB Output is correct
20 Correct 11 ms 19156 KB Output is correct
21 Correct 11 ms 19156 KB Output is correct
22 Correct 10 ms 19156 KB Output is correct
23 Correct 11 ms 19156 KB Output is correct
24 Correct 10 ms 19156 KB Output is correct
25 Correct 11 ms 19156 KB Output is correct
26 Correct 10 ms 19156 KB Output is correct
27 Correct 11 ms 19212 KB Output is correct
28 Correct 9 ms 19104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 12 ms 19152 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 13 ms 19216 KB Output is correct
5 Correct 10 ms 19096 KB Output is correct
6 Correct 11 ms 19156 KB Output is correct
7 Correct 11 ms 19212 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 11 ms 19168 KB Output is correct
10 Correct 11 ms 19156 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19172 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
14 Correct 10 ms 19028 KB Output is correct
15 Correct 9 ms 19100 KB Output is correct
16 Correct 11 ms 19228 KB Output is correct
17 Correct 13 ms 19156 KB Output is correct
18 Correct 14 ms 19204 KB Output is correct
19 Correct 13 ms 19132 KB Output is correct
20 Correct 11 ms 19156 KB Output is correct
21 Correct 11 ms 19156 KB Output is correct
22 Correct 10 ms 19156 KB Output is correct
23 Correct 11 ms 19156 KB Output is correct
24 Correct 10 ms 19156 KB Output is correct
25 Correct 11 ms 19156 KB Output is correct
26 Correct 10 ms 19156 KB Output is correct
27 Correct 11 ms 19212 KB Output is correct
28 Correct 9 ms 19104 KB Output is correct
29 Correct 12 ms 19232 KB Output is correct
30 Correct 11 ms 19156 KB Output is correct
31 Correct 11 ms 19156 KB Output is correct
32 Correct 11 ms 19156 KB Output is correct
33 Correct 11 ms 19156 KB Output is correct
34 Correct 11 ms 19212 KB Output is correct
35 Correct 11 ms 19280 KB Output is correct
36 Correct 11 ms 19212 KB Output is correct
37 Correct 11 ms 19140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 12 ms 19152 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 13 ms 19216 KB Output is correct
5 Correct 10 ms 19096 KB Output is correct
6 Correct 11 ms 19156 KB Output is correct
7 Correct 11 ms 19212 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 11 ms 19168 KB Output is correct
10 Correct 11 ms 19156 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19172 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
14 Correct 10 ms 19028 KB Output is correct
15 Correct 9 ms 19100 KB Output is correct
16 Correct 11 ms 19228 KB Output is correct
17 Correct 13 ms 19156 KB Output is correct
18 Correct 14 ms 19204 KB Output is correct
19 Correct 13 ms 19132 KB Output is correct
20 Correct 11 ms 19156 KB Output is correct
21 Correct 11 ms 19156 KB Output is correct
22 Correct 10 ms 19156 KB Output is correct
23 Correct 11 ms 19156 KB Output is correct
24 Correct 10 ms 19156 KB Output is correct
25 Correct 11 ms 19156 KB Output is correct
26 Correct 10 ms 19156 KB Output is correct
27 Correct 11 ms 19212 KB Output is correct
28 Correct 9 ms 19104 KB Output is correct
29 Correct 12 ms 19232 KB Output is correct
30 Correct 11 ms 19156 KB Output is correct
31 Correct 11 ms 19156 KB Output is correct
32 Correct 11 ms 19156 KB Output is correct
33 Correct 11 ms 19156 KB Output is correct
34 Correct 11 ms 19212 KB Output is correct
35 Correct 11 ms 19280 KB Output is correct
36 Correct 11 ms 19212 KB Output is correct
37 Correct 11 ms 19140 KB Output is correct
38 Correct 131 ms 23164 KB Output is correct
39 Correct 928 ms 57040 KB Output is correct
40 Correct 101 ms 22092 KB Output is correct
41 Correct 53 ms 20928 KB Output is correct
42 Correct 67 ms 22220 KB Output is correct
43 Correct 46 ms 20832 KB Output is correct
44 Correct 19 ms 19540 KB Output is correct
45 Correct 104 ms 48388 KB Output is correct
46 Correct 99 ms 48248 KB Output is correct
47 Correct 433 ms 52060 KB Output is correct
48 Correct 431 ms 52040 KB Output is correct
49 Correct 86 ms 48420 KB Output is correct
50 Correct 90 ms 48480 KB Output is correct
51 Correct 68 ms 49520 KB Output is correct
52 Correct 70 ms 49520 KB Output is correct
53 Correct 20 ms 21260 KB Output is correct
54 Correct 92 ms 48236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 11 ms 19028 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 11 ms 19076 KB Output is correct
5 Correct 15 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 10 ms 19028 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 9 ms 19156 KB Output is correct
12 Correct 11 ms 19156 KB Output is correct
13 Correct 30 ms 19160 KB Output is correct
14 Correct 42 ms 19200 KB Output is correct
15 Correct 35 ms 19292 KB Output is correct
16 Correct 102 ms 49468 KB Output is correct
17 Correct 238 ms 58508 KB Output is correct
18 Correct 535 ms 82264 KB Output is correct
19 Correct 135 ms 50212 KB Output is correct
20 Correct 130 ms 50252 KB Output is correct
21 Correct 124 ms 50220 KB Output is correct
22 Correct 195 ms 59092 KB Output is correct
23 Correct 184 ms 59308 KB Output is correct
24 Correct 191 ms 59448 KB Output is correct
25 Correct 213 ms 59476 KB Output is correct
26 Correct 200 ms 59492 KB Output is correct
27 Correct 266 ms 66604 KB Output is correct
28 Correct 258 ms 67928 KB Output is correct
29 Correct 255 ms 64948 KB Output is correct
30 Correct 182 ms 57592 KB Output is correct
31 Correct 217 ms 58120 KB Output is correct
32 Correct 201 ms 57332 KB Output is correct
33 Correct 181 ms 58088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 24 ms 19188 KB Output is correct
5 Correct 33 ms 19156 KB Output is correct
6 Correct 11 ms 19156 KB Output is correct
7 Correct 10 ms 19156 KB Output is correct
8 Correct 11 ms 19292 KB Output is correct
9 Correct 118 ms 23224 KB Output is correct
10 Correct 893 ms 57136 KB Output is correct
11 Correct 16 ms 19156 KB Output is correct
12 Correct 49 ms 19196 KB Output is correct
13 Correct 187 ms 89576 KB Output is correct
14 Correct 219 ms 103232 KB Output is correct
15 Execution timed out 5090 ms 427108 KB Time limit exceeded
16 Halted 0 ms 0 KB -