Submission #609603

# Submission time Handle Problem Language Result Execution time Memory
609603 2022-07-27T18:01:52 Z Tigryonochekk Jail (JOI22_jail) C++17
77 / 100
5000 ms 369576 KB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <string>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int N = 1.2e5 + 2, lgN = 17, M = N;

int n, ln;
vector<int> g[N];
int m;
int b[N], w[N];

pii sb[N], sw[N];

int p[N];
int tin[N], tout[N];
int timer;
int d[N][lgN];

set<int> pp[M];
int col[M];

int rb[N], rw[N]; // reverce bed and workplace

void LEVI() {
	for (int i = 1; i <= n; i++) {
		g[i].clear();
	}
	fill(p + 1, p + n + 1, 0);
	fill(tin, tin + n + 1, 0);
	fill(tout, tout + n + 1, 0);
	timer = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < ln; j++) {
			d[i][j] = 0;
		}
	}
	for (int i = 1; i <= m; i++) {
		pp[i].clear();
	}
	fill(col, col + m + 1, 0);
	fill(rb, rb + n + 1, 0);
	fill(rw, rw + n + 1, 0);
}

void dfs(int v) {
	tin[v] = timer++;
	for (int to : g[v]) {
		if (p[v] == to) continue;
		p[to] = v;
		d[to][0] = v;
		for (int i = 1; i < ln; i++) {
			d[to][i] = d[d[to][i - 1]][i - 1];
		}
		dfs(to);
	}
	tout[v] = timer++;
}

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

int lca(int a, int b) {
	if (check(a, b)) return a;
	if (check(b, a)) return b;
	for (int i = ln - 1; i >= 0; i--) {
		if (!check(d[a][i], b))
			a = d[a][i];
	}
	return p[a];
}

bool belong(int a, int b, int c) {
	return check(lca(a, b), c) && (check(c, a) || check(c, b));
}

int compare(int i, int j) { //0 - lriv, 1-skizb, 2-verj, 3-vochinch
	if (belong(b[i], w[i], b[j])) {
		if (belong(b[i], w[i], w[j])) {
			return 0;
		}
		else
			return 1;
	}
	else {
		if (belong(b[i], w[i], w[j]))
			return 2;
		else
			return 3;
	}
}

void edging(int i, int j) {
	if (j == i) return;
	int com = compare(i, j);
	if (com == 0) {
		pp[i].insert(j);
		pp[j].insert(i);
	}
	else if (com == 1) {
		pp[i].insert(j);
	}
	else if (com == 2) {
		pp[j].insert(i);
	}
}

bool dps(int v) {
	bool ret = 1;
	col[v] = 1;
	for (auto to : pp[v]) {
		if (col[to] == 2) continue;
		if (col[to] == 1) return 0;
		col[to] = 1;
		ret &= dps(to);
	}
	col[v] = 2;
	return ret;
}

void solve() {
	LEVI();
	cin >> n;
	ln = log2(n) + 1;
	bool flag = true;
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		if (x != i || y != i + 1)
			flag = false;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	cin >> m;
	for (int i = 1; i <= m; i++) {
		cin >> b[i] >> w[i];
		rb[b[i]] = i;
		rw[w[i]] = i;
	}
	if (flag) {
		for (int i = 1; i <= m; i++) {
			sb[i] = pii(b[i], i);
			sw[i] = pii(w[i], i);
		}
		sort(sb + 1, sb + m + 1);
		sort(sw + 1, sw + m + 1);
		for (int i = 1; i <= m; i++) {
			if (sb[i].second != sw[i].second) {
				cout << "No\n";
				return;
			}
		}
		cout << "Yes\n";
	}
	else {
		p[1] = 1;
		for (int i = 0; i < ln; i++) {
			d[1][i] = 1;
		}
		dfs(1);
		for (int i = 1; i <= m; i++) {
			int l = lca(b[i], w[i]);
			for (int j = b[i]; j != l; j = p[j]) {
				if (rw[j]) {
					edging(i, rw[j]);
				}
				if (rb[j]) {
					edging(i, rb[j]);
				}
			}
			for (int j = w[i]; j != l; j = p[j]) {
				if (rw[j]) {
					edging(i, rw[j]);
				}
				if (rb[j]) {
					edging(i, rb[j]);
				}
			}
			if (rw[l]) {
				edging(i, rw[l]);
			}
			if (rb[l]) {
				edging(i, rb[l]);
			}
		}
		bool ans = 1;
		for (int i = 1; i <= m; i++) {
			if (col[i] == 0) {
				ans &= dps(i);
			}
		}
		//cout << "imbluedabudidabudaidabudidabudaidabudidabudai\n";
		if (ans)
			cout << "Yes\n";
		else
			cout << "No\n";
	}
}

int main() {
	int t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8788 KB Output is correct
2 Correct 4 ms 8788 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 24 ms 8788 KB Output is correct
5 Correct 46 ms 8788 KB Output is correct
6 Correct 6 ms 8788 KB Output is correct
7 Correct 6 ms 8788 KB Output is correct
8 Correct 7 ms 8788 KB Output is correct
9 Correct 62 ms 9428 KB Output is correct
10 Correct 75 ms 12592 KB Output is correct
11 Correct 15 ms 8788 KB Output is correct
12 Correct 75 ms 8788 KB Output is correct
13 Correct 100 ms 14524 KB Output is correct
14 Correct 106 ms 14604 KB Output is correct
15 Correct 108 ms 14668 KB Output is correct
16 Correct 195 ms 16252 KB Output is correct
17 Correct 108 ms 14352 KB Output is correct
18 Correct 137 ms 16212 KB Output is correct
19 Correct 110 ms 14452 KB Output is correct
20 Correct 114 ms 14412 KB Output is correct
21 Correct 116 ms 14864 KB Output is correct
22 Correct 105 ms 14864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8796 KB Output is correct
3 Correct 7 ms 8788 KB Output is correct
4 Correct 6 ms 8788 KB Output is correct
5 Correct 7 ms 8788 KB Output is correct
6 Correct 9 ms 8788 KB Output is correct
7 Correct 7 ms 8780 KB Output is correct
8 Correct 7 ms 8788 KB Output is correct
9 Correct 8 ms 8788 KB Output is correct
10 Correct 7 ms 8788 KB Output is correct
11 Correct 7 ms 8780 KB Output is correct
12 Correct 7 ms 8784 KB Output is correct
13 Correct 7 ms 8788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8796 KB Output is correct
3 Correct 7 ms 8788 KB Output is correct
4 Correct 6 ms 8788 KB Output is correct
5 Correct 7 ms 8788 KB Output is correct
6 Correct 9 ms 8788 KB Output is correct
7 Correct 7 ms 8780 KB Output is correct
8 Correct 7 ms 8788 KB Output is correct
9 Correct 8 ms 8788 KB Output is correct
10 Correct 7 ms 8788 KB Output is correct
11 Correct 7 ms 8780 KB Output is correct
12 Correct 7 ms 8784 KB Output is correct
13 Correct 7 ms 8788 KB Output is correct
14 Correct 4 ms 8776 KB Output is correct
15 Correct 5 ms 8788 KB Output is correct
16 Correct 7 ms 8780 KB Output is correct
17 Correct 7 ms 8788 KB Output is correct
18 Correct 7 ms 8788 KB Output is correct
19 Correct 5 ms 8772 KB Output is correct
20 Correct 7 ms 8828 KB Output is correct
21 Correct 7 ms 8788 KB Output is correct
22 Correct 7 ms 8788 KB Output is correct
23 Correct 5 ms 8772 KB Output is correct
24 Correct 5 ms 8788 KB Output is correct
25 Correct 7 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 7 ms 8780 KB Output is correct
28 Correct 5 ms 8788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8796 KB Output is correct
3 Correct 7 ms 8788 KB Output is correct
4 Correct 6 ms 8788 KB Output is correct
5 Correct 7 ms 8788 KB Output is correct
6 Correct 9 ms 8788 KB Output is correct
7 Correct 7 ms 8780 KB Output is correct
8 Correct 7 ms 8788 KB Output is correct
9 Correct 8 ms 8788 KB Output is correct
10 Correct 7 ms 8788 KB Output is correct
11 Correct 7 ms 8780 KB Output is correct
12 Correct 7 ms 8784 KB Output is correct
13 Correct 7 ms 8788 KB Output is correct
14 Correct 4 ms 8776 KB Output is correct
15 Correct 5 ms 8788 KB Output is correct
16 Correct 7 ms 8780 KB Output is correct
17 Correct 7 ms 8788 KB Output is correct
18 Correct 7 ms 8788 KB Output is correct
19 Correct 5 ms 8772 KB Output is correct
20 Correct 7 ms 8828 KB Output is correct
21 Correct 7 ms 8788 KB Output is correct
22 Correct 7 ms 8788 KB Output is correct
23 Correct 5 ms 8772 KB Output is correct
24 Correct 5 ms 8788 KB Output is correct
25 Correct 7 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 7 ms 8780 KB Output is correct
28 Correct 5 ms 8788 KB Output is correct
29 Correct 7 ms 8816 KB Output is correct
30 Correct 8 ms 8916 KB Output is correct
31 Correct 10 ms 8916 KB Output is correct
32 Correct 7 ms 8788 KB Output is correct
33 Correct 10 ms 8884 KB Output is correct
34 Correct 12 ms 8928 KB Output is correct
35 Correct 24 ms 9084 KB Output is correct
36 Correct 7 ms 8916 KB Output is correct
37 Correct 7 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8796 KB Output is correct
3 Correct 7 ms 8788 KB Output is correct
4 Correct 6 ms 8788 KB Output is correct
5 Correct 7 ms 8788 KB Output is correct
6 Correct 9 ms 8788 KB Output is correct
7 Correct 7 ms 8780 KB Output is correct
8 Correct 7 ms 8788 KB Output is correct
9 Correct 8 ms 8788 KB Output is correct
10 Correct 7 ms 8788 KB Output is correct
11 Correct 7 ms 8780 KB Output is correct
12 Correct 7 ms 8784 KB Output is correct
13 Correct 7 ms 8788 KB Output is correct
14 Correct 4 ms 8776 KB Output is correct
15 Correct 5 ms 8788 KB Output is correct
16 Correct 7 ms 8780 KB Output is correct
17 Correct 7 ms 8788 KB Output is correct
18 Correct 7 ms 8788 KB Output is correct
19 Correct 5 ms 8772 KB Output is correct
20 Correct 7 ms 8828 KB Output is correct
21 Correct 7 ms 8788 KB Output is correct
22 Correct 7 ms 8788 KB Output is correct
23 Correct 5 ms 8772 KB Output is correct
24 Correct 5 ms 8788 KB Output is correct
25 Correct 7 ms 8788 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 7 ms 8780 KB Output is correct
28 Correct 5 ms 8788 KB Output is correct
29 Correct 7 ms 8816 KB Output is correct
30 Correct 8 ms 8916 KB Output is correct
31 Correct 10 ms 8916 KB Output is correct
32 Correct 7 ms 8788 KB Output is correct
33 Correct 10 ms 8884 KB Output is correct
34 Correct 12 ms 8928 KB Output is correct
35 Correct 24 ms 9084 KB Output is correct
36 Correct 7 ms 8916 KB Output is correct
37 Correct 7 ms 8784 KB Output is correct
38 Correct 68 ms 10604 KB Output is correct
39 Correct 76 ms 13904 KB Output is correct
40 Correct 293 ms 14068 KB Output is correct
41 Correct 79 ms 10592 KB Output is correct
42 Correct 246 ms 14608 KB Output is correct
43 Correct 70 ms 10796 KB Output is correct
44 Correct 42 ms 9460 KB Output is correct
45 Correct 121 ms 24300 KB Output is correct
46 Correct 112 ms 24272 KB Output is correct
47 Correct 122 ms 28020 KB Output is correct
48 Correct 118 ms 28028 KB Output is correct
49 Correct 109 ms 24456 KB Output is correct
50 Correct 106 ms 24396 KB Output is correct
51 Correct 93 ms 25120 KB Output is correct
52 Correct 99 ms 25428 KB Output is correct
53 Correct 26 ms 10012 KB Output is correct
54 Correct 153 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 5 ms 8788 KB Output is correct
5 Correct 15 ms 8768 KB Output is correct
6 Correct 5 ms 8788 KB Output is correct
7 Correct 5 ms 8784 KB Output is correct
8 Correct 5 ms 8788 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 5 ms 8768 KB Output is correct
11 Correct 4 ms 8788 KB Output is correct
12 Correct 9 ms 8916 KB Output is correct
13 Correct 80 ms 9404 KB Output is correct
14 Correct 67 ms 9580 KB Output is correct
15 Correct 72 ms 9548 KB Output is correct
16 Correct 127 ms 25548 KB Output is correct
17 Correct 614 ms 60824 KB Output is correct
18 Correct 2946 ms 203472 KB Output is correct
19 Correct 142 ms 24944 KB Output is correct
20 Correct 134 ms 25236 KB Output is correct
21 Correct 153 ms 25432 KB Output is correct
22 Correct 659 ms 68412 KB Output is correct
23 Correct 541 ms 68400 KB Output is correct
24 Correct 527 ms 67660 KB Output is correct
25 Correct 546 ms 70232 KB Output is correct
26 Correct 580 ms 69632 KB Output is correct
27 Correct 284 ms 36244 KB Output is correct
28 Correct 291 ms 38676 KB Output is correct
29 Correct 282 ms 35744 KB Output is correct
30 Correct 212 ms 31040 KB Output is correct
31 Correct 192 ms 31600 KB Output is correct
32 Correct 191 ms 30896 KB Output is correct
33 Correct 200 ms 31564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8788 KB Output is correct
2 Correct 4 ms 8788 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 24 ms 8788 KB Output is correct
5 Correct 46 ms 8788 KB Output is correct
6 Correct 6 ms 8788 KB Output is correct
7 Correct 6 ms 8788 KB Output is correct
8 Correct 7 ms 8788 KB Output is correct
9 Correct 62 ms 9428 KB Output is correct
10 Correct 75 ms 12592 KB Output is correct
11 Correct 15 ms 8788 KB Output is correct
12 Correct 75 ms 8788 KB Output is correct
13 Correct 100 ms 14524 KB Output is correct
14 Correct 106 ms 14604 KB Output is correct
15 Correct 108 ms 14668 KB Output is correct
16 Correct 195 ms 16252 KB Output is correct
17 Correct 108 ms 14352 KB Output is correct
18 Correct 137 ms 16212 KB Output is correct
19 Correct 110 ms 14452 KB Output is correct
20 Correct 114 ms 14412 KB Output is correct
21 Correct 116 ms 14864 KB Output is correct
22 Correct 105 ms 14864 KB Output is correct
23 Correct 5 ms 8788 KB Output is correct
24 Correct 5 ms 8796 KB Output is correct
25 Correct 7 ms 8788 KB Output is correct
26 Correct 6 ms 8788 KB Output is correct
27 Correct 7 ms 8788 KB Output is correct
28 Correct 9 ms 8788 KB Output is correct
29 Correct 7 ms 8780 KB Output is correct
30 Correct 7 ms 8788 KB Output is correct
31 Correct 8 ms 8788 KB Output is correct
32 Correct 7 ms 8788 KB Output is correct
33 Correct 7 ms 8780 KB Output is correct
34 Correct 7 ms 8784 KB Output is correct
35 Correct 7 ms 8788 KB Output is correct
36 Correct 4 ms 8776 KB Output is correct
37 Correct 5 ms 8788 KB Output is correct
38 Correct 7 ms 8780 KB Output is correct
39 Correct 7 ms 8788 KB Output is correct
40 Correct 7 ms 8788 KB Output is correct
41 Correct 5 ms 8772 KB Output is correct
42 Correct 7 ms 8828 KB Output is correct
43 Correct 7 ms 8788 KB Output is correct
44 Correct 7 ms 8788 KB Output is correct
45 Correct 5 ms 8772 KB Output is correct
46 Correct 5 ms 8788 KB Output is correct
47 Correct 7 ms 8788 KB Output is correct
48 Correct 5 ms 8788 KB Output is correct
49 Correct 7 ms 8780 KB Output is correct
50 Correct 5 ms 8788 KB Output is correct
51 Correct 7 ms 8816 KB Output is correct
52 Correct 8 ms 8916 KB Output is correct
53 Correct 10 ms 8916 KB Output is correct
54 Correct 7 ms 8788 KB Output is correct
55 Correct 10 ms 8884 KB Output is correct
56 Correct 12 ms 8928 KB Output is correct
57 Correct 24 ms 9084 KB Output is correct
58 Correct 7 ms 8916 KB Output is correct
59 Correct 7 ms 8784 KB Output is correct
60 Correct 68 ms 10604 KB Output is correct
61 Correct 76 ms 13904 KB Output is correct
62 Correct 293 ms 14068 KB Output is correct
63 Correct 79 ms 10592 KB Output is correct
64 Correct 246 ms 14608 KB Output is correct
65 Correct 70 ms 10796 KB Output is correct
66 Correct 42 ms 9460 KB Output is correct
67 Correct 121 ms 24300 KB Output is correct
68 Correct 112 ms 24272 KB Output is correct
69 Correct 122 ms 28020 KB Output is correct
70 Correct 118 ms 28028 KB Output is correct
71 Correct 109 ms 24456 KB Output is correct
72 Correct 106 ms 24396 KB Output is correct
73 Correct 93 ms 25120 KB Output is correct
74 Correct 99 ms 25428 KB Output is correct
75 Correct 26 ms 10012 KB Output is correct
76 Correct 153 ms 24172 KB Output is correct
77 Correct 5 ms 8788 KB Output is correct
78 Correct 5 ms 8788 KB Output is correct
79 Correct 5 ms 8788 KB Output is correct
80 Correct 5 ms 8788 KB Output is correct
81 Correct 15 ms 8768 KB Output is correct
82 Correct 5 ms 8788 KB Output is correct
83 Correct 5 ms 8784 KB Output is correct
84 Correct 5 ms 8788 KB Output is correct
85 Correct 5 ms 8788 KB Output is correct
86 Correct 5 ms 8768 KB Output is correct
87 Correct 4 ms 8788 KB Output is correct
88 Correct 9 ms 8916 KB Output is correct
89 Correct 80 ms 9404 KB Output is correct
90 Correct 67 ms 9580 KB Output is correct
91 Correct 72 ms 9548 KB Output is correct
92 Correct 127 ms 25548 KB Output is correct
93 Correct 614 ms 60824 KB Output is correct
94 Correct 2946 ms 203472 KB Output is correct
95 Correct 142 ms 24944 KB Output is correct
96 Correct 134 ms 25236 KB Output is correct
97 Correct 153 ms 25432 KB Output is correct
98 Correct 659 ms 68412 KB Output is correct
99 Correct 541 ms 68400 KB Output is correct
100 Correct 527 ms 67660 KB Output is correct
101 Correct 546 ms 70232 KB Output is correct
102 Correct 580 ms 69632 KB Output is correct
103 Correct 284 ms 36244 KB Output is correct
104 Correct 291 ms 38676 KB Output is correct
105 Correct 282 ms 35744 KB Output is correct
106 Correct 212 ms 31040 KB Output is correct
107 Correct 192 ms 31600 KB Output is correct
108 Correct 191 ms 30896 KB Output is correct
109 Correct 200 ms 31564 KB Output is correct
110 Correct 92 ms 9852 KB Output is correct
111 Correct 47 ms 9424 KB Output is correct
112 Execution timed out 5096 ms 369576 KB Time limit exceeded
113 Halted 0 ms 0 KB -