답안 #548873

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
548873 2022-04-14T15:31:14 Z nonsensenonsense1 Jail (JOI22_jail) C++17
100 / 100
564 ms 58428 KB
#include <cstdio>
#include <cstring>
#include <vector>

const int N = 120000;
int n, q, m, dt, cur, s[N], t[N], l[N], in[N], out[N], pr[N], size[N], top[N], u[N];
std::vector<int> g[N], t1[N << 1], t2[N << 1];

void dfs0(int v) {
	size[v] = 1;
	for (int i = 0; i < (int)g[v].size(); ++i) if (g[v][i] == pr[v]) g[v].erase(g[v].begin() + i);
	for (int i = 0; i < (int)g[v].size(); ++i) {
		pr[g[v][i]] = v;
		dfs0(g[v][i]);
		size[v] += size[g[v][i]];
		if (size[g[v][i]] > size[g[v][0]]) std::swap(g[v][i], g[v][0]);
	}
}

void dfs1(int v) {
	in[v] = dt++;
	for (int i = 0; i < (int)g[v].size(); ++i) {
		if (i) top[g[v][i]] = g[v][i];
		else top[g[v][i]] = top[v];
		dfs1(g[v][i]);
	}
}

void update1(int l, int r, int x) {
	for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
		if (l & 1) t1[l++].push_back(x);
		if (r & 1) t1[--r].push_back(x);
	}
}

void update2(int i, int x) {
	for (i += n; i; i >>= 1) t2[i].push_back(x);
}

int take(std::vector<int> &vec) {
	while (!vec.empty()) {
		if (u[vec.back()] == 2) vec.pop_back();
		else if (vec.back() == cur) {
			if (vec.size() == 1) return -1;
			if (vec.front() == vec.back()) vec.pop_back();
			else std::swap(vec.front(), vec.back());
		} else return vec.back();
	}
	return -1;
}

int get1(int i) {
	int x;
	for (i += n; i; i >>= 1) {
		x = take(t1[i]);
		if (x != -1) return x;
	}
	return -1;
}

int get2(int l, int r) {
	int x;
	for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
		if (l & 1) {
			x = take(t2[l++]);
			if (x != -1) return x;
		}
		if (r & 1) {
			x = take(t2[--r]);
			if (x != -1) return x;
		}
	}
	return -1;
}

int lca(int u, int v) {
	while (top[u] != top[v]) {
		if (in[top[u]] < in[top[v]]) std::swap(u, v);
		u = pr[top[u]];
	}
	return in[u] < in[v] ? u : v;
}

void addpath(int v, int l, int x) {
	while (true) {
		update1(std::max(in[top[v]], in[l]), in[v] + 1, x);
		v = top[v];
		if (in[v] <= in[l]) break;
		v = pr[v];
	}
}

int getpath(int v, int l) {
	int x;
	while (true) {
		x = get2(std::max(in[top[v]], in[l]), in[v] + 1);
		if (x != -1) return x;
		v = top[v];
		if (in[v] <= in[l]) break;
		v = pr[v];
	}
	return -1;
}

bool run(int v) {
	u[v] = 1;
	int to;
	while (cur = v, (to = getpath(s[v], l[v])) != -1) if (u[to] == 1 || !run(to)) return 0;
	while (cur = v, (to = getpath(t[v], l[v])) != -1) if (u[to] == 1 || !run(to)) return 0;
	while (cur = v, (to = get1(in[s[v]])) != -1) if (u[to] == 1 || !run(to)) return 0;
	u[v] = 2;
	return 1;
}

int main() {
	scanf("%d", &q);
	while (q--) {
		scanf("%d", &n);
		for (int i = 0; i < n; ++i) g[i].clear();
		for (int i = 1; i < n; ++i) {
			int a, b;
			scanf("%d%d", &a, &b);
			--a;
			--b;
			g[a].push_back(b);
			g[b].push_back(a);
		}
		dt = 0;
		dfs0(0);
		dfs1(0);
		for (int i = 0; i < n << 1; ++i) {
			t1[i].clear();
			t2[i].clear();
		}
		scanf("%d", &m);
		for (int i = 0; i < m; ++i) {
			scanf("%d%d", s + i, t + i);
			--s[i];
			--t[i];
			l[i] = lca(s[i], t[i]);
			addpath(s[i], l[i], i);
			addpath(t[i], l[i], i);
			update2(in[t[i]], i);
		}
		memset(u, 0, m << 2);
		bool ok = 1;
		for (int i = 0; i < m; ++i) if (!u[i] && !run(i)) ok = 0;
		printf("%s\n", ok ? "Yes" : "No");
	}
	return 0;
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
jail.cpp:118:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
jail.cpp:122:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |    scanf("%d%d", &a, &b);
      |    ~~~~~^~~~~~~~~~~~~~~~
jail.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |   scanf("%d", &m);
      |   ~~~~~^~~~~~~~~~
jail.cpp:137:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |    scanf("%d%d", s + i, t + i);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14292 KB Output is correct
2 Correct 10 ms 14292 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 18 ms 14436 KB Output is correct
5 Correct 31 ms 14556 KB Output is correct
6 Correct 8 ms 14380 KB Output is correct
7 Correct 9 ms 14388 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 39 ms 15792 KB Output is correct
10 Correct 52 ms 29516 KB Output is correct
11 Correct 16 ms 14292 KB Output is correct
12 Correct 43 ms 14432 KB Output is correct
13 Correct 129 ms 43616 KB Output is correct
14 Correct 145 ms 44816 KB Output is correct
15 Correct 222 ms 44976 KB Output is correct
16 Correct 400 ms 58428 KB Output is correct
17 Correct 149 ms 44884 KB Output is correct
18 Correct 159 ms 50908 KB Output is correct
19 Correct 138 ms 44604 KB Output is correct
20 Correct 136 ms 44552 KB Output is correct
21 Correct 158 ms 47012 KB Output is correct
22 Correct 116 ms 42692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 8 ms 14328 KB Output is correct
6 Correct 9 ms 14420 KB Output is correct
7 Correct 9 ms 14420 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 10 ms 14416 KB Output is correct
13 Correct 10 ms 14332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 8 ms 14328 KB Output is correct
6 Correct 9 ms 14420 KB Output is correct
7 Correct 9 ms 14420 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 10 ms 14416 KB Output is correct
13 Correct 10 ms 14332 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 9 ms 14420 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 8 ms 14292 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 9 ms 14448 KB Output is correct
22 Correct 9 ms 14420 KB Output is correct
23 Correct 11 ms 14420 KB Output is correct
24 Correct 8 ms 14544 KB Output is correct
25 Correct 9 ms 14420 KB Output is correct
26 Correct 10 ms 14528 KB Output is correct
27 Correct 9 ms 14408 KB Output is correct
28 Correct 8 ms 14388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 8 ms 14328 KB Output is correct
6 Correct 9 ms 14420 KB Output is correct
7 Correct 9 ms 14420 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 10 ms 14416 KB Output is correct
13 Correct 10 ms 14332 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 9 ms 14420 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 8 ms 14292 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 9 ms 14448 KB Output is correct
22 Correct 9 ms 14420 KB Output is correct
23 Correct 11 ms 14420 KB Output is correct
24 Correct 8 ms 14544 KB Output is correct
25 Correct 9 ms 14420 KB Output is correct
26 Correct 10 ms 14528 KB Output is correct
27 Correct 9 ms 14408 KB Output is correct
28 Correct 8 ms 14388 KB Output is correct
29 Correct 10 ms 14420 KB Output is correct
30 Correct 10 ms 14420 KB Output is correct
31 Correct 9 ms 14420 KB Output is correct
32 Correct 9 ms 14420 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 10 ms 14420 KB Output is correct
35 Correct 9 ms 14368 KB Output is correct
36 Correct 10 ms 14416 KB Output is correct
37 Correct 11 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 8 ms 14328 KB Output is correct
6 Correct 9 ms 14420 KB Output is correct
7 Correct 9 ms 14420 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 10 ms 14416 KB Output is correct
13 Correct 10 ms 14332 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14388 KB Output is correct
17 Correct 9 ms 14420 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 8 ms 14292 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 9 ms 14448 KB Output is correct
22 Correct 9 ms 14420 KB Output is correct
23 Correct 11 ms 14420 KB Output is correct
24 Correct 8 ms 14544 KB Output is correct
25 Correct 9 ms 14420 KB Output is correct
26 Correct 10 ms 14528 KB Output is correct
27 Correct 9 ms 14408 KB Output is correct
28 Correct 8 ms 14388 KB Output is correct
29 Correct 10 ms 14420 KB Output is correct
30 Correct 10 ms 14420 KB Output is correct
31 Correct 9 ms 14420 KB Output is correct
32 Correct 9 ms 14420 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 10 ms 14420 KB Output is correct
35 Correct 9 ms 14368 KB Output is correct
36 Correct 10 ms 14416 KB Output is correct
37 Correct 11 ms 14420 KB Output is correct
38 Correct 49 ms 15784 KB Output is correct
39 Correct 57 ms 29480 KB Output is correct
40 Correct 51 ms 15708 KB Output is correct
41 Correct 48 ms 15608 KB Output is correct
42 Correct 42 ms 15852 KB Output is correct
43 Correct 37 ms 15072 KB Output is correct
44 Correct 20 ms 14660 KB Output is correct
45 Correct 78 ms 20384 KB Output is correct
46 Correct 70 ms 20304 KB Output is correct
47 Correct 52 ms 25048 KB Output is correct
48 Correct 53 ms 24996 KB Output is correct
49 Correct 65 ms 20616 KB Output is correct
50 Correct 65 ms 20596 KB Output is correct
51 Correct 53 ms 21616 KB Output is correct
52 Correct 55 ms 21612 KB Output is correct
53 Correct 22 ms 15140 KB Output is correct
54 Correct 69 ms 20320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14416 KB Output is correct
2 Correct 10 ms 14292 KB Output is correct
3 Correct 8 ms 14292 KB Output is correct
4 Correct 9 ms 14304 KB Output is correct
5 Correct 18 ms 14360 KB Output is correct
6 Correct 10 ms 14356 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 10 ms 14292 KB Output is correct
9 Correct 10 ms 14292 KB Output is correct
10 Correct 7 ms 14324 KB Output is correct
11 Correct 8 ms 14292 KB Output is correct
12 Correct 10 ms 14384 KB Output is correct
13 Correct 30 ms 14476 KB Output is correct
14 Correct 49 ms 15108 KB Output is correct
15 Correct 40 ms 14944 KB Output is correct
16 Correct 119 ms 25088 KB Output is correct
17 Correct 250 ms 37208 KB Output is correct
18 Correct 461 ms 48496 KB Output is correct
19 Correct 129 ms 27980 KB Output is correct
20 Correct 140 ms 27208 KB Output is correct
21 Correct 173 ms 27264 KB Output is correct
22 Correct 231 ms 36720 KB Output is correct
23 Correct 175 ms 36536 KB Output is correct
24 Correct 221 ms 35192 KB Output is correct
25 Correct 179 ms 35136 KB Output is correct
26 Correct 196 ms 36996 KB Output is correct
27 Correct 405 ms 43844 KB Output is correct
28 Correct 317 ms 50012 KB Output is correct
29 Correct 326 ms 47156 KB Output is correct
30 Correct 247 ms 39180 KB Output is correct
31 Correct 251 ms 39228 KB Output is correct
32 Correct 231 ms 38176 KB Output is correct
33 Correct 256 ms 38948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14292 KB Output is correct
2 Correct 10 ms 14292 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 18 ms 14436 KB Output is correct
5 Correct 31 ms 14556 KB Output is correct
6 Correct 8 ms 14380 KB Output is correct
7 Correct 9 ms 14388 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 39 ms 15792 KB Output is correct
10 Correct 52 ms 29516 KB Output is correct
11 Correct 16 ms 14292 KB Output is correct
12 Correct 43 ms 14432 KB Output is correct
13 Correct 129 ms 43616 KB Output is correct
14 Correct 145 ms 44816 KB Output is correct
15 Correct 222 ms 44976 KB Output is correct
16 Correct 400 ms 58428 KB Output is correct
17 Correct 149 ms 44884 KB Output is correct
18 Correct 159 ms 50908 KB Output is correct
19 Correct 138 ms 44604 KB Output is correct
20 Correct 136 ms 44552 KB Output is correct
21 Correct 158 ms 47012 KB Output is correct
22 Correct 116 ms 42692 KB Output is correct
23 Correct 7 ms 14292 KB Output is correct
24 Correct 7 ms 14292 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 9 ms 14420 KB Output is correct
27 Correct 8 ms 14328 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 9 ms 14420 KB Output is correct
30 Correct 9 ms 14436 KB Output is correct
31 Correct 10 ms 14420 KB Output is correct
32 Correct 11 ms 14420 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 10 ms 14416 KB Output is correct
35 Correct 10 ms 14332 KB Output is correct
36 Correct 7 ms 14420 KB Output is correct
37 Correct 8 ms 14416 KB Output is correct
38 Correct 8 ms 14388 KB Output is correct
39 Correct 9 ms 14420 KB Output is correct
40 Correct 9 ms 14420 KB Output is correct
41 Correct 8 ms 14292 KB Output is correct
42 Correct 8 ms 14420 KB Output is correct
43 Correct 9 ms 14448 KB Output is correct
44 Correct 9 ms 14420 KB Output is correct
45 Correct 11 ms 14420 KB Output is correct
46 Correct 8 ms 14544 KB Output is correct
47 Correct 9 ms 14420 KB Output is correct
48 Correct 10 ms 14528 KB Output is correct
49 Correct 9 ms 14408 KB Output is correct
50 Correct 8 ms 14388 KB Output is correct
51 Correct 10 ms 14420 KB Output is correct
52 Correct 10 ms 14420 KB Output is correct
53 Correct 9 ms 14420 KB Output is correct
54 Correct 9 ms 14420 KB Output is correct
55 Correct 9 ms 14420 KB Output is correct
56 Correct 10 ms 14420 KB Output is correct
57 Correct 9 ms 14368 KB Output is correct
58 Correct 10 ms 14416 KB Output is correct
59 Correct 11 ms 14420 KB Output is correct
60 Correct 49 ms 15784 KB Output is correct
61 Correct 57 ms 29480 KB Output is correct
62 Correct 51 ms 15708 KB Output is correct
63 Correct 48 ms 15608 KB Output is correct
64 Correct 42 ms 15852 KB Output is correct
65 Correct 37 ms 15072 KB Output is correct
66 Correct 20 ms 14660 KB Output is correct
67 Correct 78 ms 20384 KB Output is correct
68 Correct 70 ms 20304 KB Output is correct
69 Correct 52 ms 25048 KB Output is correct
70 Correct 53 ms 24996 KB Output is correct
71 Correct 65 ms 20616 KB Output is correct
72 Correct 65 ms 20596 KB Output is correct
73 Correct 53 ms 21616 KB Output is correct
74 Correct 55 ms 21612 KB Output is correct
75 Correct 22 ms 15140 KB Output is correct
76 Correct 69 ms 20320 KB Output is correct
77 Correct 9 ms 14416 KB Output is correct
78 Correct 10 ms 14292 KB Output is correct
79 Correct 8 ms 14292 KB Output is correct
80 Correct 9 ms 14304 KB Output is correct
81 Correct 18 ms 14360 KB Output is correct
82 Correct 10 ms 14356 KB Output is correct
83 Correct 8 ms 14420 KB Output is correct
84 Correct 10 ms 14292 KB Output is correct
85 Correct 10 ms 14292 KB Output is correct
86 Correct 7 ms 14324 KB Output is correct
87 Correct 8 ms 14292 KB Output is correct
88 Correct 10 ms 14384 KB Output is correct
89 Correct 30 ms 14476 KB Output is correct
90 Correct 49 ms 15108 KB Output is correct
91 Correct 40 ms 14944 KB Output is correct
92 Correct 119 ms 25088 KB Output is correct
93 Correct 250 ms 37208 KB Output is correct
94 Correct 461 ms 48496 KB Output is correct
95 Correct 129 ms 27980 KB Output is correct
96 Correct 140 ms 27208 KB Output is correct
97 Correct 173 ms 27264 KB Output is correct
98 Correct 231 ms 36720 KB Output is correct
99 Correct 175 ms 36536 KB Output is correct
100 Correct 221 ms 35192 KB Output is correct
101 Correct 179 ms 35136 KB Output is correct
102 Correct 196 ms 36996 KB Output is correct
103 Correct 405 ms 43844 KB Output is correct
104 Correct 317 ms 50012 KB Output is correct
105 Correct 326 ms 47156 KB Output is correct
106 Correct 247 ms 39180 KB Output is correct
107 Correct 251 ms 39228 KB Output is correct
108 Correct 231 ms 38176 KB Output is correct
109 Correct 256 ms 38948 KB Output is correct
110 Correct 47 ms 15488 KB Output is correct
111 Correct 33 ms 14800 KB Output is correct
112 Correct 288 ms 44792 KB Output is correct
113 Correct 136 ms 33856 KB Output is correct
114 Correct 203 ms 43212 KB Output is correct
115 Correct 51 ms 22980 KB Output is correct
116 Correct 287 ms 36600 KB Output is correct
117 Correct 564 ms 52492 KB Output is correct
118 Correct 77 ms 22008 KB Output is correct
119 Correct 76 ms 21836 KB Output is correct
120 Correct 21 ms 16108 KB Output is correct
121 Correct 257 ms 37140 KB Output is correct
122 Correct 293 ms 37036 KB Output is correct
123 Correct 133 ms 36356 KB Output is correct
124 Correct 134 ms 36280 KB Output is correct
125 Correct 186 ms 37048 KB Output is correct
126 Correct 412 ms 58176 KB Output is correct
127 Correct 235 ms 42864 KB Output is correct
128 Correct 143 ms 43504 KB Output is correct
129 Correct 166 ms 43216 KB Output is correct
130 Correct 171 ms 43540 KB Output is correct