Submission #552261

# Submission time Handle Problem Language Result Execution time Memory
552261 2022-04-23T00:02:57 Z LucaDantas Jail (JOI22_jail) C++17
100 / 100
1426 ms 347264 KB
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 120010, logn = 20;

struct Top {
	vector<int> g[(2*logn+1) * maxn];
	int ingrau[(2*logn+1) * maxn];
	queue<int> q;
	void add_edge(int a, int b) {
		// if(a == b) return; // só colocar esse if não funciona mais já que agr eu tenho arestas virtuais do binary lifting
		// printf("adding %d -> %d\n", a, b);
		ingrau[b]++;
		g[a].push_back(b);
	}
	void clear(int n) {
		for(int i = 0; i <= n; i++)
			ingrau[i] = 0, g[i].clear();
		while(q.size())
			q.pop();
	}
	bool dag(int n) {
		for(int i = 1; i <= n; i++)
			if(!ingrau[i]) q.push(i);
		int vis = 0;
		while(q.size()) {
			int u = q.front(); q.pop();
			++vis;
			for(int v : g[u])
				if(!(--ingrau[v]))
					q.push(v);
		}
		return vis == n;
	}
} top;

int p[maxn][logn], depth[maxn], comeca[maxn][logn], termina[maxn][logn]; // salvo o indice do cara que comeca e termina em mim caso exista
int s[maxn], t[maxn];
int idx;
bool mark[maxn];
vector<int> g[maxn];

void dfs(int u) {
	for(int v : g[u]) if(v != p[u][0]) {
		p[v][0] = u;
		depth[v] = depth[u] + 1;
		dfs(v);
	}
}

void build(int n) {
	idx = n;
	for(int i = 1; i <= n; i++)
		comeca[i][0] = ++idx, termina[i][0] = ++idx;
	for(int l = 1; l < logn; l++) {
		for(int i = 1; i <= n; i++) {
			comeca[i][l] = ++idx, termina[i][l] = ++idx;
			p[i][l] = p[p[i][l-1]][l-1];

			top.add_edge(comeca[i][l-1], comeca[i][l]);
			if(p[i][l-1])
				top.add_edge(comeca[p[i][l-1]][l-1], comeca[i][l]);

			top.add_edge(termina[i][l], termina[i][l-1]);
			if(p[i][l-1])
				top.add_edge(termina[i][l], termina[p[i][l-1]][l-1]);
		}
	}
	// printf("idx %d\n", idx);
}

void go(int id) {
	int a = s[id], b = t[id];
	// printf("comeca %d %d\n", a, b);
	// a != b sempre
	
	top.add_edge(comeca[b][0], id);
	top.add_edge(id, termina[a][0]);

	bool foi = 0;
	if(depth[a] == depth[b]) {
		foi = 1;
		a = p[a][0];
		b = p[b][0];
		if(a == b) {
			top.add_edge(comeca[a][0], id), top.add_edge(id, termina[a][0]);
			return;
		}
	} else {
		if(depth[a] < depth[b])
			swap(a, b); // a é o mais fundo
		a = p[a][0]; // não quero que ele crie uma aresta dele pra ele mesmo, então eu crio subo um andar do mais baixo sem fazer o upd
	}

	for(int l = logn-1; l >= 0; l--)
		if(depth[a] - (1 << l) >= depth[b]) // comeca[a][l] e termina[a][l] não incluem p[a][l] no seu intervalo: [a, p[a][l][
			top.add_edge(comeca[a][l], id), top.add_edge(id, termina[a][l]), a = p[a][l];

	// printf("primeira %d %d\n", a, b);
	if(a == b) return;

	if(!foi) {
		b = p[b][0]; // não quero que o b crie uma aresta pra ele mesmo então eu também subo
		top.add_edge(comeca[a][0], id), top.add_edge(id, termina[a][0]), a = p[a][0];
		// mas com isso também preciso subir o a pra eles ficarem no mesmo nível dnv, só que no a eu add a aresta
		if(a == b) {
			top.add_edge(comeca[a][0], id), top.add_edge(id, termina[a][0]);
			return;
		}
	}

	for(int l = logn-1; l >= 0; l--) {
		if(p[a][l] != p[b][l]) {
			top.add_edge(comeca[a][l], id), top.add_edge(id, termina[a][l]), a = p[a][l];
			top.add_edge(comeca[b][l], id), top.add_edge(id, termina[b][l]), b = p[b][l];
		}
	}
	// comeca/termina[a][1] porque eu também quero colocar o pai (lca(a,b)) que não foi contado, então pego um intervalo de tamanho 2
	top.add_edge(comeca[a][1], id), top.add_edge(id, termina[a][1]);
	top.add_edge(comeca[b][0], id), top.add_edge(id, termina[b][0]);
}

void clear(int n) {
	for(int i = 0; i <= n; i++)
		g[i].clear(), depth[i] = 0, mark[i] = 0, s[i] = 0, t[i] = 0;
	for(int i = 0; i <= n; i++)
		for(int l = 0; l < logn; l++)
			p[i][l] = comeca[i][l] = termina[i][l] = 0;
	top.clear(idx);
	idx = 0;
}

int main() {
	int q; scanf("%d", &q);
	while(q--) {
		int n; scanf("%d", &n);
		clear(n);
		for(int i = 1, a, b; i < n; i++)
			scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);
		
		dfs(1); build(n);

		int m; scanf("%d", &m);
		for(int i = 1; i <= m; i++)
			scanf("%d %d", s+i, t+i), top.add_edge(i, comeca[s[i]][0]), top.add_edge(termina[t[i]][0], i);
		
		for(int i = 1; i <= m; i++)
			go(i);
		
		puts(top.dag(idx) ? "Yes" : "No");
	}
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:134:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |  int q; scanf("%d", &q);
      |         ~~~~~^~~~~~~~~~
jail.cpp:136:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |   int n; scanf("%d", &n);
      |          ~~~~~^~~~~~~~~~
jail.cpp:139:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |    scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);
      |    ~~~~~^~~~~~~~~~~~~~~~~
jail.cpp:143:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |   int m; scanf("%d", &m);
      |          ~~~~~^~~~~~~~~~
jail.cpp:145:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |    scanf("%d %d", s+i, t+i), top.add_edge(i, comeca[s[i]][0]), top.add_edge(termina[t[i]][0], i);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 60 ms 118656 KB Output is correct
2 Correct 62 ms 118696 KB Output is correct
3 Correct 61 ms 118656 KB Output is correct
4 Correct 127 ms 118900 KB Output is correct
5 Correct 211 ms 118904 KB Output is correct
6 Correct 69 ms 119076 KB Output is correct
7 Correct 69 ms 119116 KB Output is correct
8 Correct 71 ms 119076 KB Output is correct
9 Correct 349 ms 129180 KB Output is correct
10 Correct 738 ms 322656 KB Output is correct
11 Correct 88 ms 118696 KB Output is correct
12 Correct 226 ms 118920 KB Output is correct
13 Correct 895 ms 327352 KB Output is correct
14 Correct 628 ms 327376 KB Output is correct
15 Correct 835 ms 329256 KB Output is correct
16 Correct 1257 ms 340212 KB Output is correct
17 Correct 785 ms 330916 KB Output is correct
18 Correct 930 ms 330288 KB Output is correct
19 Correct 782 ms 330816 KB Output is correct
20 Correct 670 ms 330856 KB Output is correct
21 Correct 674 ms 330288 KB Output is correct
22 Correct 643 ms 326128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 118688 KB Output is correct
2 Correct 68 ms 118604 KB Output is correct
3 Correct 72 ms 119032 KB Output is correct
4 Correct 69 ms 119116 KB Output is correct
5 Correct 77 ms 119264 KB Output is correct
6 Correct 82 ms 119040 KB Output is correct
7 Correct 74 ms 119156 KB Output is correct
8 Correct 70 ms 119112 KB Output is correct
9 Correct 70 ms 119092 KB Output is correct
10 Correct 71 ms 119016 KB Output is correct
11 Correct 75 ms 119244 KB Output is correct
12 Correct 70 ms 119112 KB Output is correct
13 Correct 71 ms 119028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 118688 KB Output is correct
2 Correct 68 ms 118604 KB Output is correct
3 Correct 72 ms 119032 KB Output is correct
4 Correct 69 ms 119116 KB Output is correct
5 Correct 77 ms 119264 KB Output is correct
6 Correct 82 ms 119040 KB Output is correct
7 Correct 74 ms 119156 KB Output is correct
8 Correct 70 ms 119112 KB Output is correct
9 Correct 70 ms 119092 KB Output is correct
10 Correct 71 ms 119016 KB Output is correct
11 Correct 75 ms 119244 KB Output is correct
12 Correct 70 ms 119112 KB Output is correct
13 Correct 71 ms 119028 KB Output is correct
14 Correct 65 ms 118724 KB Output is correct
15 Correct 62 ms 118624 KB Output is correct
16 Correct 74 ms 119056 KB Output is correct
17 Correct 90 ms 119112 KB Output is correct
18 Correct 80 ms 119188 KB Output is correct
19 Correct 62 ms 118604 KB Output is correct
20 Correct 72 ms 119084 KB Output is correct
21 Correct 72 ms 119140 KB Output is correct
22 Correct 70 ms 119164 KB Output is correct
23 Correct 65 ms 118820 KB Output is correct
24 Correct 68 ms 118996 KB Output is correct
25 Correct 73 ms 119116 KB Output is correct
26 Correct 72 ms 119144 KB Output is correct
27 Correct 70 ms 119280 KB Output is correct
28 Correct 65 ms 118712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 118688 KB Output is correct
2 Correct 68 ms 118604 KB Output is correct
3 Correct 72 ms 119032 KB Output is correct
4 Correct 69 ms 119116 KB Output is correct
5 Correct 77 ms 119264 KB Output is correct
6 Correct 82 ms 119040 KB Output is correct
7 Correct 74 ms 119156 KB Output is correct
8 Correct 70 ms 119112 KB Output is correct
9 Correct 70 ms 119092 KB Output is correct
10 Correct 71 ms 119016 KB Output is correct
11 Correct 75 ms 119244 KB Output is correct
12 Correct 70 ms 119112 KB Output is correct
13 Correct 71 ms 119028 KB Output is correct
14 Correct 65 ms 118724 KB Output is correct
15 Correct 62 ms 118624 KB Output is correct
16 Correct 74 ms 119056 KB Output is correct
17 Correct 90 ms 119112 KB Output is correct
18 Correct 80 ms 119188 KB Output is correct
19 Correct 62 ms 118604 KB Output is correct
20 Correct 72 ms 119084 KB Output is correct
21 Correct 72 ms 119140 KB Output is correct
22 Correct 70 ms 119164 KB Output is correct
23 Correct 65 ms 118820 KB Output is correct
24 Correct 68 ms 118996 KB Output is correct
25 Correct 73 ms 119116 KB Output is correct
26 Correct 72 ms 119144 KB Output is correct
27 Correct 70 ms 119280 KB Output is correct
28 Correct 65 ms 118712 KB Output is correct
29 Correct 74 ms 119228 KB Output is correct
30 Correct 99 ms 119076 KB Output is correct
31 Correct 82 ms 119204 KB Output is correct
32 Correct 71 ms 119116 KB Output is correct
33 Correct 71 ms 119120 KB Output is correct
34 Correct 71 ms 119088 KB Output is correct
35 Correct 84 ms 119116 KB Output is correct
36 Correct 73 ms 119184 KB Output is correct
37 Correct 68 ms 119124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 118688 KB Output is correct
2 Correct 68 ms 118604 KB Output is correct
3 Correct 72 ms 119032 KB Output is correct
4 Correct 69 ms 119116 KB Output is correct
5 Correct 77 ms 119264 KB Output is correct
6 Correct 82 ms 119040 KB Output is correct
7 Correct 74 ms 119156 KB Output is correct
8 Correct 70 ms 119112 KB Output is correct
9 Correct 70 ms 119092 KB Output is correct
10 Correct 71 ms 119016 KB Output is correct
11 Correct 75 ms 119244 KB Output is correct
12 Correct 70 ms 119112 KB Output is correct
13 Correct 71 ms 119028 KB Output is correct
14 Correct 65 ms 118724 KB Output is correct
15 Correct 62 ms 118624 KB Output is correct
16 Correct 74 ms 119056 KB Output is correct
17 Correct 90 ms 119112 KB Output is correct
18 Correct 80 ms 119188 KB Output is correct
19 Correct 62 ms 118604 KB Output is correct
20 Correct 72 ms 119084 KB Output is correct
21 Correct 72 ms 119140 KB Output is correct
22 Correct 70 ms 119164 KB Output is correct
23 Correct 65 ms 118820 KB Output is correct
24 Correct 68 ms 118996 KB Output is correct
25 Correct 73 ms 119116 KB Output is correct
26 Correct 72 ms 119144 KB Output is correct
27 Correct 70 ms 119280 KB Output is correct
28 Correct 65 ms 118712 KB Output is correct
29 Correct 74 ms 119228 KB Output is correct
30 Correct 99 ms 119076 KB Output is correct
31 Correct 82 ms 119204 KB Output is correct
32 Correct 71 ms 119116 KB Output is correct
33 Correct 71 ms 119120 KB Output is correct
34 Correct 71 ms 119088 KB Output is correct
35 Correct 84 ms 119116 KB Output is correct
36 Correct 73 ms 119184 KB Output is correct
37 Correct 68 ms 119124 KB Output is correct
38 Correct 349 ms 129848 KB Output is correct
39 Correct 699 ms 323604 KB Output is correct
40 Correct 358 ms 131440 KB Output is correct
41 Correct 348 ms 130132 KB Output is correct
42 Correct 299 ms 130988 KB Output is correct
43 Correct 368 ms 131388 KB Output is correct
44 Correct 116 ms 120788 KB Output is correct
45 Correct 641 ms 318984 KB Output is correct
46 Correct 694 ms 319044 KB Output is correct
47 Correct 776 ms 320676 KB Output is correct
48 Correct 793 ms 320768 KB Output is correct
49 Correct 641 ms 321636 KB Output is correct
50 Correct 686 ms 321316 KB Output is correct
51 Correct 743 ms 321372 KB Output is correct
52 Correct 742 ms 322200 KB Output is correct
53 Correct 147 ms 133156 KB Output is correct
54 Correct 1025 ms 319172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 118604 KB Output is correct
2 Correct 67 ms 118616 KB Output is correct
3 Correct 72 ms 118600 KB Output is correct
4 Correct 62 ms 118600 KB Output is correct
5 Correct 99 ms 118632 KB Output is correct
6 Correct 74 ms 119188 KB Output is correct
7 Correct 67 ms 119032 KB Output is correct
8 Correct 65 ms 118732 KB Output is correct
9 Correct 63 ms 118712 KB Output is correct
10 Correct 67 ms 118800 KB Output is correct
11 Correct 69 ms 118684 KB Output is correct
12 Correct 71 ms 119124 KB Output is correct
13 Correct 139 ms 119360 KB Output is correct
14 Correct 218 ms 119676 KB Output is correct
15 Correct 185 ms 119424 KB Output is correct
16 Correct 814 ms 319784 KB Output is correct
17 Correct 830 ms 327412 KB Output is correct
18 Correct 1039 ms 338460 KB Output is correct
19 Correct 833 ms 321188 KB Output is correct
20 Correct 763 ms 320192 KB Output is correct
21 Correct 792 ms 320332 KB Output is correct
22 Correct 819 ms 328408 KB Output is correct
23 Correct 685 ms 328548 KB Output is correct
24 Correct 1044 ms 327736 KB Output is correct
25 Correct 986 ms 327896 KB Output is correct
26 Correct 1044 ms 328820 KB Output is correct
27 Correct 721 ms 324764 KB Output is correct
28 Correct 635 ms 324984 KB Output is correct
29 Correct 605 ms 325076 KB Output is correct
30 Correct 911 ms 320848 KB Output is correct
31 Correct 629 ms 320844 KB Output is correct
32 Correct 990 ms 320924 KB Output is correct
33 Correct 635 ms 320844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 118656 KB Output is correct
2 Correct 62 ms 118696 KB Output is correct
3 Correct 61 ms 118656 KB Output is correct
4 Correct 127 ms 118900 KB Output is correct
5 Correct 211 ms 118904 KB Output is correct
6 Correct 69 ms 119076 KB Output is correct
7 Correct 69 ms 119116 KB Output is correct
8 Correct 71 ms 119076 KB Output is correct
9 Correct 349 ms 129180 KB Output is correct
10 Correct 738 ms 322656 KB Output is correct
11 Correct 88 ms 118696 KB Output is correct
12 Correct 226 ms 118920 KB Output is correct
13 Correct 895 ms 327352 KB Output is correct
14 Correct 628 ms 327376 KB Output is correct
15 Correct 835 ms 329256 KB Output is correct
16 Correct 1257 ms 340212 KB Output is correct
17 Correct 785 ms 330916 KB Output is correct
18 Correct 930 ms 330288 KB Output is correct
19 Correct 782 ms 330816 KB Output is correct
20 Correct 670 ms 330856 KB Output is correct
21 Correct 674 ms 330288 KB Output is correct
22 Correct 643 ms 326128 KB Output is correct
23 Correct 68 ms 118688 KB Output is correct
24 Correct 68 ms 118604 KB Output is correct
25 Correct 72 ms 119032 KB Output is correct
26 Correct 69 ms 119116 KB Output is correct
27 Correct 77 ms 119264 KB Output is correct
28 Correct 82 ms 119040 KB Output is correct
29 Correct 74 ms 119156 KB Output is correct
30 Correct 70 ms 119112 KB Output is correct
31 Correct 70 ms 119092 KB Output is correct
32 Correct 71 ms 119016 KB Output is correct
33 Correct 75 ms 119244 KB Output is correct
34 Correct 70 ms 119112 KB Output is correct
35 Correct 71 ms 119028 KB Output is correct
36 Correct 65 ms 118724 KB Output is correct
37 Correct 62 ms 118624 KB Output is correct
38 Correct 74 ms 119056 KB Output is correct
39 Correct 90 ms 119112 KB Output is correct
40 Correct 80 ms 119188 KB Output is correct
41 Correct 62 ms 118604 KB Output is correct
42 Correct 72 ms 119084 KB Output is correct
43 Correct 72 ms 119140 KB Output is correct
44 Correct 70 ms 119164 KB Output is correct
45 Correct 65 ms 118820 KB Output is correct
46 Correct 68 ms 118996 KB Output is correct
47 Correct 73 ms 119116 KB Output is correct
48 Correct 72 ms 119144 KB Output is correct
49 Correct 70 ms 119280 KB Output is correct
50 Correct 65 ms 118712 KB Output is correct
51 Correct 74 ms 119228 KB Output is correct
52 Correct 99 ms 119076 KB Output is correct
53 Correct 82 ms 119204 KB Output is correct
54 Correct 71 ms 119116 KB Output is correct
55 Correct 71 ms 119120 KB Output is correct
56 Correct 71 ms 119088 KB Output is correct
57 Correct 84 ms 119116 KB Output is correct
58 Correct 73 ms 119184 KB Output is correct
59 Correct 68 ms 119124 KB Output is correct
60 Correct 349 ms 129848 KB Output is correct
61 Correct 699 ms 323604 KB Output is correct
62 Correct 358 ms 131440 KB Output is correct
63 Correct 348 ms 130132 KB Output is correct
64 Correct 299 ms 130988 KB Output is correct
65 Correct 368 ms 131388 KB Output is correct
66 Correct 116 ms 120788 KB Output is correct
67 Correct 641 ms 318984 KB Output is correct
68 Correct 694 ms 319044 KB Output is correct
69 Correct 776 ms 320676 KB Output is correct
70 Correct 793 ms 320768 KB Output is correct
71 Correct 641 ms 321636 KB Output is correct
72 Correct 686 ms 321316 KB Output is correct
73 Correct 743 ms 321372 KB Output is correct
74 Correct 742 ms 322200 KB Output is correct
75 Correct 147 ms 133156 KB Output is correct
76 Correct 1025 ms 319172 KB Output is correct
77 Correct 64 ms 118604 KB Output is correct
78 Correct 67 ms 118616 KB Output is correct
79 Correct 72 ms 118600 KB Output is correct
80 Correct 62 ms 118600 KB Output is correct
81 Correct 99 ms 118632 KB Output is correct
82 Correct 74 ms 119188 KB Output is correct
83 Correct 67 ms 119032 KB Output is correct
84 Correct 65 ms 118732 KB Output is correct
85 Correct 63 ms 118712 KB Output is correct
86 Correct 67 ms 118800 KB Output is correct
87 Correct 69 ms 118684 KB Output is correct
88 Correct 71 ms 119124 KB Output is correct
89 Correct 139 ms 119360 KB Output is correct
90 Correct 218 ms 119676 KB Output is correct
91 Correct 185 ms 119424 KB Output is correct
92 Correct 814 ms 319784 KB Output is correct
93 Correct 830 ms 327412 KB Output is correct
94 Correct 1039 ms 338460 KB Output is correct
95 Correct 833 ms 321188 KB Output is correct
96 Correct 763 ms 320192 KB Output is correct
97 Correct 792 ms 320332 KB Output is correct
98 Correct 819 ms 328408 KB Output is correct
99 Correct 685 ms 328548 KB Output is correct
100 Correct 1044 ms 327736 KB Output is correct
101 Correct 986 ms 327896 KB Output is correct
102 Correct 1044 ms 328820 KB Output is correct
103 Correct 721 ms 324764 KB Output is correct
104 Correct 635 ms 324984 KB Output is correct
105 Correct 605 ms 325076 KB Output is correct
106 Correct 911 ms 320848 KB Output is correct
107 Correct 629 ms 320844 KB Output is correct
108 Correct 990 ms 320924 KB Output is correct
109 Correct 635 ms 320844 KB Output is correct
110 Correct 210 ms 119996 KB Output is correct
111 Correct 178 ms 119436 KB Output is correct
112 Correct 967 ms 335496 KB Output is correct
113 Correct 1020 ms 325468 KB Output is correct
114 Correct 780 ms 329668 KB Output is correct
115 Correct 424 ms 317316 KB Output is correct
116 Correct 1213 ms 324500 KB Output is correct
117 Correct 1128 ms 342624 KB Output is correct
118 Correct 1014 ms 319156 KB Output is correct
119 Correct 1002 ms 319052 KB Output is correct
120 Correct 132 ms 136140 KB Output is correct
121 Correct 1426 ms 328856 KB Output is correct
122 Correct 1323 ms 328668 KB Output is correct
123 Correct 1168 ms 325884 KB Output is correct
124 Correct 737 ms 325900 KB Output is correct
125 Correct 1127 ms 326704 KB Output is correct
126 Correct 1229 ms 347264 KB Output is correct
127 Correct 1112 ms 340216 KB Output is correct
128 Correct 743 ms 337292 KB Output is correct
129 Correct 1247 ms 341768 KB Output is correct
130 Correct 954 ms 341456 KB Output is correct