Submission #588496

# Submission time Handle Problem Language Result Execution time Memory
588496 2022-07-03T11:34:53 Z valerikk Jail (JOI22_jail) C++17
100 / 100
1290 ms 446272 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <iomanip>

using namespace std;

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)(a).size())
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define y1 y1111

using ll = long long;
using ld = long double;

const int N = 1.2e5 + 7;
const int M = 1e7;

int n, m;
vector<int> g[N];
int s[N], t[N];
int rs[N], rt[N];
int sz[N];
bool used[N];
int f[N];
vector<int> g1[M];
int id[N];
int par[N];
bool ans;
int used1[M];

int dfs(int u, int p) {
	sz[u] = 1;
	for (int v : g[u]) {
		if (!used[v] && v != p) {
			sz[u] += dfs(v, u);
		}
	}
	return sz[u];
}

int centroid(int u, int p, int n1) {
	for (int v : g[u]) {
		if (!used[v] && v != p && 2 * sz[v] > n1) {
			return centroid(v, u, n1);
		}
	}
	return u;
}

void dfs1(int u, int p) {
	if (p == -1) f[u] = u;
	par[u] = p;
	id[u] = m;
	g1[id[u]].clear();
	g1[id[u] + 1].clear();
	m += 2;
	if (p != -1) {
		if (rs[u] != -1) g1[id[u]].pb(rs[u]);
		if (rt[u] != -1) g1[rt[u]].pb(id[u] + 1);
		g1[id[u]].pb(id[p]);
		g1[id[p] + 1].pb(id[u] + 1);
	}
	for (int v : g[u]) {
		if (!used[v] && v != p) {
			f[v] = p == -1 ? v : f[u];
			dfs1(v, u);
		}
	}
}

void dfs2(int u) {
	used1[u] = 1;
	for (int v : g1[u]) {
		if (used1[v] == 1) ans = 0;
		if (!used1[v]) dfs2(v);
	}
	used1[u] = 2;
}

void go(int u, const vector<int> &who) {
	dfs1(u, -1);
	map<int, vector<int>> who1;
	for (int i : who) {
		if (f[s[i]] != f[t[i]]) {
			if (s[i] != u) g1[i].pb(id[par[s[i]]]);
			g1[i].pb(id[t[i]]);
			if (t[i] != u) g1[id[par[t[i]]] + 1].pb(i);
			g1[id[s[i]] + 1].pb(i);
			if (rs[u] != -1 && u != s[i]) {
				g1[i].pb(rs[u]);
			}
			if (rt[u] != -1 && u != t[i]) {
				g1[rt[u]].pb(i);
			}
		} else {
			who1[f[s[i]]].pb(i);
		}
	}
	used[u] = 1;
	for (int v : g[u]) {
		if (!used[v]) {
			dfs(v, -1);
			int c = centroid(v, -1, sz[v]);
			go(c, who1[v]);
		}
	}
}

void solve() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		g[i].clear();
	}
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	cin >> m;
	for (int i = 1; i <= n; ++i) {
		rs[i] = -1;
		rt[i] = -1;
	}
	for (int i = 0; i < m; ++i) {
		cin >> s[i] >> t[i];
		rs[s[i]] = i;
		rt[t[i]] = i;
		g1[i].clear();
	}
	for (int i = 1; i <= n; ++i) {
		used[i] = 0;
	}
	vector<int> who;
	for (int i = 0; i < m; ++i) {
		if (s[i] != t[i]) {
			who.pb(i);
		}
	}
	dfs(1, -1);
	int c = centroid(1, -1, n);
	go(c, who);
	ans = 1;
	for (int i = 0; i < m; ++i) {
		used1[i] = 0;
	}
	for (int i = 0; i < m; ++i) {
		if (!used1[i]) dfs2(i);
	}
	cout << (ans ? "Yes" : "No") << endl;
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int q;
	cin >> q;
	while (q--) {
		solve();
	}
	return 0;	
}
# Verdict Execution time Memory Grader output
1 Correct 105 ms 237956 KB Output is correct
2 Correct 107 ms 237932 KB Output is correct
3 Correct 109 ms 237964 KB Output is correct
4 Correct 135 ms 238280 KB Output is correct
5 Correct 160 ms 238268 KB Output is correct
6 Correct 106 ms 238156 KB Output is correct
7 Correct 114 ms 238120 KB Output is correct
8 Correct 111 ms 238128 KB Output is correct
9 Correct 228 ms 244300 KB Output is correct
10 Correct 368 ms 379000 KB Output is correct
11 Correct 124 ms 238144 KB Output is correct
12 Correct 183 ms 239020 KB Output is correct
13 Correct 493 ms 388424 KB Output is correct
14 Correct 495 ms 388304 KB Output is correct
15 Correct 659 ms 425816 KB Output is correct
16 Correct 941 ms 446272 KB Output is correct
17 Correct 480 ms 389864 KB Output is correct
18 Correct 568 ms 404580 KB Output is correct
19 Correct 496 ms 389760 KB Output is correct
20 Correct 490 ms 389708 KB Output is correct
21 Correct 521 ms 391824 KB Output is correct
22 Correct 518 ms 390056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 237996 KB Output is correct
2 Correct 110 ms 237904 KB Output is correct
3 Correct 111 ms 238156 KB Output is correct
4 Correct 114 ms 238104 KB Output is correct
5 Correct 116 ms 238088 KB Output is correct
6 Correct 108 ms 238120 KB Output is correct
7 Correct 107 ms 238116 KB Output is correct
8 Correct 108 ms 238096 KB Output is correct
9 Correct 120 ms 238188 KB Output is correct
10 Correct 115 ms 238072 KB Output is correct
11 Correct 109 ms 238064 KB Output is correct
12 Correct 106 ms 238120 KB Output is correct
13 Correct 110 ms 238156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 237996 KB Output is correct
2 Correct 110 ms 237904 KB Output is correct
3 Correct 111 ms 238156 KB Output is correct
4 Correct 114 ms 238104 KB Output is correct
5 Correct 116 ms 238088 KB Output is correct
6 Correct 108 ms 238120 KB Output is correct
7 Correct 107 ms 238116 KB Output is correct
8 Correct 108 ms 238096 KB Output is correct
9 Correct 120 ms 238188 KB Output is correct
10 Correct 115 ms 238072 KB Output is correct
11 Correct 109 ms 238064 KB Output is correct
12 Correct 106 ms 238120 KB Output is correct
13 Correct 110 ms 238156 KB Output is correct
14 Correct 105 ms 238012 KB Output is correct
15 Correct 106 ms 237900 KB Output is correct
16 Correct 113 ms 238092 KB Output is correct
17 Correct 108 ms 238148 KB Output is correct
18 Correct 115 ms 238112 KB Output is correct
19 Correct 104 ms 237900 KB Output is correct
20 Correct 115 ms 238060 KB Output is correct
21 Correct 109 ms 238152 KB Output is correct
22 Correct 112 ms 238168 KB Output is correct
23 Correct 107 ms 237972 KB Output is correct
24 Correct 114 ms 237980 KB Output is correct
25 Correct 109 ms 238156 KB Output is correct
26 Correct 105 ms 238080 KB Output is correct
27 Correct 109 ms 238092 KB Output is correct
28 Correct 111 ms 237904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 237996 KB Output is correct
2 Correct 110 ms 237904 KB Output is correct
3 Correct 111 ms 238156 KB Output is correct
4 Correct 114 ms 238104 KB Output is correct
5 Correct 116 ms 238088 KB Output is correct
6 Correct 108 ms 238120 KB Output is correct
7 Correct 107 ms 238116 KB Output is correct
8 Correct 108 ms 238096 KB Output is correct
9 Correct 120 ms 238188 KB Output is correct
10 Correct 115 ms 238072 KB Output is correct
11 Correct 109 ms 238064 KB Output is correct
12 Correct 106 ms 238120 KB Output is correct
13 Correct 110 ms 238156 KB Output is correct
14 Correct 105 ms 238012 KB Output is correct
15 Correct 106 ms 237900 KB Output is correct
16 Correct 113 ms 238092 KB Output is correct
17 Correct 108 ms 238148 KB Output is correct
18 Correct 115 ms 238112 KB Output is correct
19 Correct 104 ms 237900 KB Output is correct
20 Correct 115 ms 238060 KB Output is correct
21 Correct 109 ms 238152 KB Output is correct
22 Correct 112 ms 238168 KB Output is correct
23 Correct 107 ms 237972 KB Output is correct
24 Correct 114 ms 237980 KB Output is correct
25 Correct 109 ms 238156 KB Output is correct
26 Correct 105 ms 238080 KB Output is correct
27 Correct 109 ms 238092 KB Output is correct
28 Correct 111 ms 237904 KB Output is correct
29 Correct 114 ms 238124 KB Output is correct
30 Correct 109 ms 238160 KB Output is correct
31 Correct 113 ms 238156 KB Output is correct
32 Correct 119 ms 238020 KB Output is correct
33 Correct 109 ms 238072 KB Output is correct
34 Correct 115 ms 238124 KB Output is correct
35 Correct 111 ms 238044 KB Output is correct
36 Correct 116 ms 238084 KB Output is correct
37 Correct 113 ms 238036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 237996 KB Output is correct
2 Correct 110 ms 237904 KB Output is correct
3 Correct 111 ms 238156 KB Output is correct
4 Correct 114 ms 238104 KB Output is correct
5 Correct 116 ms 238088 KB Output is correct
6 Correct 108 ms 238120 KB Output is correct
7 Correct 107 ms 238116 KB Output is correct
8 Correct 108 ms 238096 KB Output is correct
9 Correct 120 ms 238188 KB Output is correct
10 Correct 115 ms 238072 KB Output is correct
11 Correct 109 ms 238064 KB Output is correct
12 Correct 106 ms 238120 KB Output is correct
13 Correct 110 ms 238156 KB Output is correct
14 Correct 105 ms 238012 KB Output is correct
15 Correct 106 ms 237900 KB Output is correct
16 Correct 113 ms 238092 KB Output is correct
17 Correct 108 ms 238148 KB Output is correct
18 Correct 115 ms 238112 KB Output is correct
19 Correct 104 ms 237900 KB Output is correct
20 Correct 115 ms 238060 KB Output is correct
21 Correct 109 ms 238152 KB Output is correct
22 Correct 112 ms 238168 KB Output is correct
23 Correct 107 ms 237972 KB Output is correct
24 Correct 114 ms 237980 KB Output is correct
25 Correct 109 ms 238156 KB Output is correct
26 Correct 105 ms 238080 KB Output is correct
27 Correct 109 ms 238092 KB Output is correct
28 Correct 111 ms 237904 KB Output is correct
29 Correct 114 ms 238124 KB Output is correct
30 Correct 109 ms 238160 KB Output is correct
31 Correct 113 ms 238156 KB Output is correct
32 Correct 119 ms 238020 KB Output is correct
33 Correct 109 ms 238072 KB Output is correct
34 Correct 115 ms 238124 KB Output is correct
35 Correct 111 ms 238044 KB Output is correct
36 Correct 116 ms 238084 KB Output is correct
37 Correct 113 ms 238036 KB Output is correct
38 Correct 230 ms 244216 KB Output is correct
39 Correct 375 ms 378908 KB Output is correct
40 Correct 251 ms 244616 KB Output is correct
41 Correct 214 ms 242908 KB Output is correct
42 Correct 189 ms 244792 KB Output is correct
43 Correct 245 ms 244200 KB Output is correct
44 Correct 120 ms 238740 KB Output is correct
45 Correct 418 ms 318216 KB Output is correct
46 Correct 427 ms 320048 KB Output is correct
47 Correct 397 ms 355304 KB Output is correct
48 Correct 419 ms 355276 KB Output is correct
49 Correct 397 ms 332008 KB Output is correct
50 Correct 397 ms 328944 KB Output is correct
51 Correct 389 ms 342816 KB Output is correct
52 Correct 405 ms 343028 KB Output is correct
53 Correct 138 ms 244280 KB Output is correct
54 Correct 621 ms 348000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 237984 KB Output is correct
2 Correct 110 ms 237904 KB Output is correct
3 Correct 116 ms 237984 KB Output is correct
4 Correct 106 ms 237980 KB Output is correct
5 Correct 116 ms 238072 KB Output is correct
6 Correct 105 ms 237976 KB Output is correct
7 Correct 109 ms 238008 KB Output is correct
8 Correct 110 ms 238000 KB Output is correct
9 Correct 106 ms 238004 KB Output is correct
10 Correct 115 ms 237936 KB Output is correct
11 Correct 109 ms 238004 KB Output is correct
12 Correct 111 ms 238124 KB Output is correct
13 Correct 145 ms 238280 KB Output is correct
14 Correct 174 ms 238284 KB Output is correct
15 Correct 155 ms 238112 KB Output is correct
16 Correct 388 ms 294048 KB Output is correct
17 Correct 563 ms 302296 KB Output is correct
18 Correct 780 ms 322448 KB Output is correct
19 Correct 419 ms 294424 KB Output is correct
20 Correct 416 ms 296600 KB Output is correct
21 Correct 429 ms 297500 KB Output is correct
22 Correct 436 ms 301460 KB Output is correct
23 Correct 411 ms 301756 KB Output is correct
24 Correct 397 ms 301404 KB Output is correct
25 Correct 404 ms 301604 KB Output is correct
26 Correct 414 ms 301392 KB Output is correct
27 Correct 364 ms 280584 KB Output is correct
28 Correct 356 ms 284536 KB Output is correct
29 Correct 362 ms 278656 KB Output is correct
30 Correct 339 ms 275588 KB Output is correct
31 Correct 315 ms 277108 KB Output is correct
32 Correct 319 ms 272912 KB Output is correct
33 Correct 348 ms 274996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 237956 KB Output is correct
2 Correct 107 ms 237932 KB Output is correct
3 Correct 109 ms 237964 KB Output is correct
4 Correct 135 ms 238280 KB Output is correct
5 Correct 160 ms 238268 KB Output is correct
6 Correct 106 ms 238156 KB Output is correct
7 Correct 114 ms 238120 KB Output is correct
8 Correct 111 ms 238128 KB Output is correct
9 Correct 228 ms 244300 KB Output is correct
10 Correct 368 ms 379000 KB Output is correct
11 Correct 124 ms 238144 KB Output is correct
12 Correct 183 ms 239020 KB Output is correct
13 Correct 493 ms 388424 KB Output is correct
14 Correct 495 ms 388304 KB Output is correct
15 Correct 659 ms 425816 KB Output is correct
16 Correct 941 ms 446272 KB Output is correct
17 Correct 480 ms 389864 KB Output is correct
18 Correct 568 ms 404580 KB Output is correct
19 Correct 496 ms 389760 KB Output is correct
20 Correct 490 ms 389708 KB Output is correct
21 Correct 521 ms 391824 KB Output is correct
22 Correct 518 ms 390056 KB Output is correct
23 Correct 108 ms 237996 KB Output is correct
24 Correct 110 ms 237904 KB Output is correct
25 Correct 111 ms 238156 KB Output is correct
26 Correct 114 ms 238104 KB Output is correct
27 Correct 116 ms 238088 KB Output is correct
28 Correct 108 ms 238120 KB Output is correct
29 Correct 107 ms 238116 KB Output is correct
30 Correct 108 ms 238096 KB Output is correct
31 Correct 120 ms 238188 KB Output is correct
32 Correct 115 ms 238072 KB Output is correct
33 Correct 109 ms 238064 KB Output is correct
34 Correct 106 ms 238120 KB Output is correct
35 Correct 110 ms 238156 KB Output is correct
36 Correct 105 ms 238012 KB Output is correct
37 Correct 106 ms 237900 KB Output is correct
38 Correct 113 ms 238092 KB Output is correct
39 Correct 108 ms 238148 KB Output is correct
40 Correct 115 ms 238112 KB Output is correct
41 Correct 104 ms 237900 KB Output is correct
42 Correct 115 ms 238060 KB Output is correct
43 Correct 109 ms 238152 KB Output is correct
44 Correct 112 ms 238168 KB Output is correct
45 Correct 107 ms 237972 KB Output is correct
46 Correct 114 ms 237980 KB Output is correct
47 Correct 109 ms 238156 KB Output is correct
48 Correct 105 ms 238080 KB Output is correct
49 Correct 109 ms 238092 KB Output is correct
50 Correct 111 ms 237904 KB Output is correct
51 Correct 114 ms 238124 KB Output is correct
52 Correct 109 ms 238160 KB Output is correct
53 Correct 113 ms 238156 KB Output is correct
54 Correct 119 ms 238020 KB Output is correct
55 Correct 109 ms 238072 KB Output is correct
56 Correct 115 ms 238124 KB Output is correct
57 Correct 111 ms 238044 KB Output is correct
58 Correct 116 ms 238084 KB Output is correct
59 Correct 113 ms 238036 KB Output is correct
60 Correct 230 ms 244216 KB Output is correct
61 Correct 375 ms 378908 KB Output is correct
62 Correct 251 ms 244616 KB Output is correct
63 Correct 214 ms 242908 KB Output is correct
64 Correct 189 ms 244792 KB Output is correct
65 Correct 245 ms 244200 KB Output is correct
66 Correct 120 ms 238740 KB Output is correct
67 Correct 418 ms 318216 KB Output is correct
68 Correct 427 ms 320048 KB Output is correct
69 Correct 397 ms 355304 KB Output is correct
70 Correct 419 ms 355276 KB Output is correct
71 Correct 397 ms 332008 KB Output is correct
72 Correct 397 ms 328944 KB Output is correct
73 Correct 389 ms 342816 KB Output is correct
74 Correct 405 ms 343028 KB Output is correct
75 Correct 138 ms 244280 KB Output is correct
76 Correct 621 ms 348000 KB Output is correct
77 Correct 116 ms 237984 KB Output is correct
78 Correct 110 ms 237904 KB Output is correct
79 Correct 116 ms 237984 KB Output is correct
80 Correct 106 ms 237980 KB Output is correct
81 Correct 116 ms 238072 KB Output is correct
82 Correct 105 ms 237976 KB Output is correct
83 Correct 109 ms 238008 KB Output is correct
84 Correct 110 ms 238000 KB Output is correct
85 Correct 106 ms 238004 KB Output is correct
86 Correct 115 ms 237936 KB Output is correct
87 Correct 109 ms 238004 KB Output is correct
88 Correct 111 ms 238124 KB Output is correct
89 Correct 145 ms 238280 KB Output is correct
90 Correct 174 ms 238284 KB Output is correct
91 Correct 155 ms 238112 KB Output is correct
92 Correct 388 ms 294048 KB Output is correct
93 Correct 563 ms 302296 KB Output is correct
94 Correct 780 ms 322448 KB Output is correct
95 Correct 419 ms 294424 KB Output is correct
96 Correct 416 ms 296600 KB Output is correct
97 Correct 429 ms 297500 KB Output is correct
98 Correct 436 ms 301460 KB Output is correct
99 Correct 411 ms 301756 KB Output is correct
100 Correct 397 ms 301404 KB Output is correct
101 Correct 404 ms 301604 KB Output is correct
102 Correct 414 ms 301392 KB Output is correct
103 Correct 364 ms 280584 KB Output is correct
104 Correct 356 ms 284536 KB Output is correct
105 Correct 362 ms 278656 KB Output is correct
106 Correct 339 ms 275588 KB Output is correct
107 Correct 315 ms 277108 KB Output is correct
108 Correct 319 ms 272912 KB Output is correct
109 Correct 348 ms 274996 KB Output is correct
110 Correct 182 ms 239320 KB Output is correct
111 Correct 161 ms 238668 KB Output is correct
112 Correct 800 ms 385404 KB Output is correct
113 Correct 490 ms 359244 KB Output is correct
114 Correct 488 ms 348148 KB Output is correct
115 Correct 166 ms 262024 KB Output is correct
116 Correct 563 ms 303148 KB Output is correct
117 Correct 1290 ms 384536 KB Output is correct
118 Correct 564 ms 344628 KB Output is correct
119 Correct 596 ms 348144 KB Output is correct
120 Correct 129 ms 243564 KB Output is correct
121 Correct 620 ms 318580 KB Output is correct
122 Correct 709 ms 320228 KB Output is correct
123 Correct 506 ms 365772 KB Output is correct
124 Correct 508 ms 370892 KB Output is correct
125 Correct 514 ms 366340 KB Output is correct
126 Correct 979 ms 413336 KB Output is correct
127 Correct 561 ms 370024 KB Output is correct
128 Correct 494 ms 369940 KB Output is correct
129 Correct 503 ms 369992 KB Output is correct
130 Correct 497 ms 369896 KB Output is correct