Submission #609634

# Submission time Handle Problem Language Result Execution time Memory
609634 2022-07-27T18:36:19 Z HamletPetrosyan Jail (JOI22_jail) C++17
61 / 100
5000 ms 51796 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];
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();
	}
	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];
	}

	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
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 22 ms 10140 KB Output is correct
5 Correct 32 ms 10244 KB Output is correct
6 Correct 7 ms 9864 KB Output is correct
7 Correct 6 ms 9752 KB Output is correct
8 Correct 9 ms 9940 KB Output is correct
9 Correct 120 ms 14096 KB Output is correct
10 Correct 74 ms 48536 KB Output is correct
11 Correct 12 ms 9812 KB Output is correct
12 Correct 80 ms 10320 KB Output is correct
13 Execution timed out 5054 ms 51796 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9696 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 6 ms 9896 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 7 ms 9756 KB Output is correct
10 Correct 9 ms 9812 KB Output is correct
11 Correct 8 ms 9812 KB Output is correct
12 Correct 7 ms 9748 KB Output is correct
13 Correct 6 ms 9752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9696 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 6 ms 9896 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 7 ms 9756 KB Output is correct
10 Correct 9 ms 9812 KB Output is correct
11 Correct 8 ms 9812 KB Output is correct
12 Correct 7 ms 9748 KB Output is correct
13 Correct 6 ms 9752 KB Output is correct
14 Correct 5 ms 9740 KB Output is correct
15 Correct 5 ms 9748 KB Output is correct
16 Correct 6 ms 9872 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 6 ms 9748 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 6 ms 9756 KB Output is correct
21 Correct 7 ms 9752 KB Output is correct
22 Correct 6 ms 9752 KB Output is correct
23 Correct 5 ms 9744 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 8 ms 9748 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 9 ms 9812 KB Output is correct
28 Correct 7 ms 9736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9696 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 6 ms 9896 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 7 ms 9756 KB Output is correct
10 Correct 9 ms 9812 KB Output is correct
11 Correct 8 ms 9812 KB Output is correct
12 Correct 7 ms 9748 KB Output is correct
13 Correct 6 ms 9752 KB Output is correct
14 Correct 5 ms 9740 KB Output is correct
15 Correct 5 ms 9748 KB Output is correct
16 Correct 6 ms 9872 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 6 ms 9748 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 6 ms 9756 KB Output is correct
21 Correct 7 ms 9752 KB Output is correct
22 Correct 6 ms 9752 KB Output is correct
23 Correct 5 ms 9744 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 8 ms 9748 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 9 ms 9812 KB Output is correct
28 Correct 7 ms 9736 KB Output is correct
29 Correct 12 ms 9888 KB Output is correct
30 Correct 16 ms 9876 KB Output is correct
31 Correct 13 ms 9880 KB Output is correct
32 Correct 11 ms 9812 KB Output is correct
33 Correct 7 ms 9752 KB Output is correct
34 Correct 26 ms 9812 KB Output is correct
35 Correct 27 ms 9812 KB Output is correct
36 Correct 30 ms 9852 KB Output is correct
37 Correct 19 ms 9852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9696 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 6 ms 9896 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 7 ms 9756 KB Output is correct
10 Correct 9 ms 9812 KB Output is correct
11 Correct 8 ms 9812 KB Output is correct
12 Correct 7 ms 9748 KB Output is correct
13 Correct 6 ms 9752 KB Output is correct
14 Correct 5 ms 9740 KB Output is correct
15 Correct 5 ms 9748 KB Output is correct
16 Correct 6 ms 9872 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 6 ms 9748 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 6 ms 9756 KB Output is correct
21 Correct 7 ms 9752 KB Output is correct
22 Correct 6 ms 9752 KB Output is correct
23 Correct 5 ms 9744 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 8 ms 9748 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 9 ms 9812 KB Output is correct
28 Correct 7 ms 9736 KB Output is correct
29 Correct 12 ms 9888 KB Output is correct
30 Correct 16 ms 9876 KB Output is correct
31 Correct 13 ms 9880 KB Output is correct
32 Correct 11 ms 9812 KB Output is correct
33 Correct 7 ms 9752 KB Output is correct
34 Correct 26 ms 9812 KB Output is correct
35 Correct 27 ms 9812 KB Output is correct
36 Correct 30 ms 9852 KB Output is correct
37 Correct 19 ms 9852 KB Output is correct
38 Correct 131 ms 13976 KB Output is correct
39 Correct 71 ms 48524 KB Output is correct
40 Correct 686 ms 13408 KB Output is correct
41 Correct 742 ms 11744 KB Output is correct
42 Correct 567 ms 13188 KB Output is correct
43 Correct 45 ms 11920 KB Output is correct
44 Correct 507 ms 10392 KB Output is correct
45 Correct 165 ms 39952 KB Output is correct
46 Correct 145 ms 39736 KB Output is correct
47 Correct 169 ms 43484 KB Output is correct
48 Correct 170 ms 43536 KB Output is correct
49 Correct 159 ms 40024 KB Output is correct
50 Correct 153 ms 39936 KB Output is correct
51 Correct 75 ms 40708 KB Output is correct
52 Correct 69 ms 40944 KB Output is correct
53 Correct 409 ms 12068 KB Output is correct
54 Correct 151 ms 39760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9736 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9736 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 11 ms 9884 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9752 KB Output is correct
8 Correct 5 ms 9740 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 7 ms 9684 KB Output is correct
12 Correct 31 ms 9840 KB Output is correct
13 Correct 210 ms 10368 KB Output is correct
14 Correct 131 ms 10272 KB Output is correct
15 Correct 175 ms 10300 KB Output is correct
16 Execution timed out 5043 ms 40428 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 22 ms 10140 KB Output is correct
5 Correct 32 ms 10244 KB Output is correct
6 Correct 7 ms 9864 KB Output is correct
7 Correct 6 ms 9752 KB Output is correct
8 Correct 9 ms 9940 KB Output is correct
9 Correct 120 ms 14096 KB Output is correct
10 Correct 74 ms 48536 KB Output is correct
11 Correct 12 ms 9812 KB Output is correct
12 Correct 80 ms 10320 KB Output is correct
13 Execution timed out 5054 ms 51796 KB Time limit exceeded
14 Halted 0 ms 0 KB -