Submission #552224

# Submission time Handle Problem Language Result Execution time Memory
552224 2022-04-22T19:40:19 Z LucaDantas Jail (JOI22_jail) C++17
72 / 100
5000 ms 838288 KB
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 120010;

struct Top {
	vector<int> g[maxn];
	int ingrau[maxn];
	queue<int> q;
	void add_edge(int a, int b) {
		if(a == b) return;
		// 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], depth[maxn], comeca[maxn], termina[maxn]; // salvo o indice do cara que comeca e termina em mim caso exista
int s[maxn], t[maxn];
bool mark[maxn];
vector<int> g[maxn];

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

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

void go(int id) {
	// printf("go id %d\n", id);
	int a = s[id], b = t[id];
	if(termina[a]) // alguem termina em a (eu sei que eu comeco em a)
		top.add_edge(id, termina[a]); // eu tenho q sair antes do cara que termina aqui
	if(comeca[b]) // algue comeca em b (eu termino lá), então tenho q sair depois do cara ter terminado
		top.add_edge(comeca[b], id);
	for(; a != b;) {
		if(depth[a] < depth[b])
			swap(a, b);
		a = p[a];
		// printf("%d\n", a);
		if(termina[a])
			top.add_edge(id, termina[a]);
		if(comeca[a])
			top.add_edge(comeca[a], id);
	}
	// puts("");
}

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);
		int m; scanf("%d", &m);
		for(int i = 1; i <= m; i++)
			scanf("%d %d", s+i, t+i), comeca[s[i]] = i, termina[t[i]] = i;
		for(int i = 1; i <= m; i++)
			go(i);
		puts(top.dag(n) ? "Yes" : "No");
	}
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |  int q; scanf("%d", &q);
      |         ~~~~~^~~~~~~~~~
jail.cpp:79:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   int n; scanf("%d", &n);
      |          ~~~~~^~~~~~~~~~
jail.cpp:82:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |    scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);
      |    ~~~~~^~~~~~~~~~~~~~~~~
jail.cpp:84:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |   int m; scanf("%d", &m);
      |          ~~~~~^~~~~~~~~~
jail.cpp:86:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |    scanf("%d %d", s+i, t+i), comeca[s[i]] = i, termina[t[i]] = i;
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5972 KB Output is correct
4 Correct 13 ms 5984 KB Output is correct
5 Correct 24 ms 5972 KB Output is correct
6 Correct 4 ms 5972 KB Output is correct
7 Correct 4 ms 5972 KB Output is correct
8 Correct 5 ms 5972 KB Output is correct
9 Correct 84 ms 9564 KB Output is correct
10 Correct 236 ms 22968 KB Output is correct
11 Correct 9 ms 6100 KB Output is correct
12 Correct 39 ms 6532 KB Output is correct
13 Correct 147 ms 51120 KB Output is correct
14 Correct 167 ms 64872 KB Output is correct
15 Execution timed out 5119 ms 838288 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 5972 KB Output is correct
6 Correct 6 ms 5972 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 6 ms 5972 KB Output is correct
9 Correct 4 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 3 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 5972 KB Output is correct
6 Correct 6 ms 5972 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 6 ms 5972 KB Output is correct
9 Correct 4 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 3 ms 5972 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 3 ms 5884 KB Output is correct
16 Correct 4 ms 5972 KB Output is correct
17 Correct 4 ms 5972 KB Output is correct
18 Correct 4 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5972 KB Output is correct
22 Correct 4 ms 5972 KB Output is correct
23 Correct 3 ms 5972 KB Output is correct
24 Correct 4 ms 5976 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 3 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 5972 KB Output is correct
6 Correct 6 ms 5972 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 6 ms 5972 KB Output is correct
9 Correct 4 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 3 ms 5972 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 3 ms 5884 KB Output is correct
16 Correct 4 ms 5972 KB Output is correct
17 Correct 4 ms 5972 KB Output is correct
18 Correct 4 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5972 KB Output is correct
22 Correct 4 ms 5972 KB Output is correct
23 Correct 3 ms 5972 KB Output is correct
24 Correct 4 ms 5976 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 3 ms 5972 KB Output is correct
29 Correct 5 ms 6100 KB Output is correct
30 Correct 5 ms 5972 KB Output is correct
31 Correct 5 ms 6080 KB Output is correct
32 Correct 4 ms 6000 KB Output is correct
33 Correct 4 ms 5972 KB Output is correct
34 Correct 5 ms 5964 KB Output is correct
35 Correct 6 ms 6100 KB Output is correct
36 Correct 5 ms 5972 KB Output is correct
37 Correct 4 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 5972 KB Output is correct
6 Correct 6 ms 5972 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 6 ms 5972 KB Output is correct
9 Correct 4 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 3 ms 5972 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 3 ms 5884 KB Output is correct
16 Correct 4 ms 5972 KB Output is correct
17 Correct 4 ms 5972 KB Output is correct
18 Correct 4 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5972 KB Output is correct
22 Correct 4 ms 5972 KB Output is correct
23 Correct 3 ms 5972 KB Output is correct
24 Correct 4 ms 5976 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 3 ms 5972 KB Output is correct
29 Correct 5 ms 6100 KB Output is correct
30 Correct 5 ms 5972 KB Output is correct
31 Correct 5 ms 6080 KB Output is correct
32 Correct 4 ms 6000 KB Output is correct
33 Correct 4 ms 5972 KB Output is correct
34 Correct 5 ms 5964 KB Output is correct
35 Correct 6 ms 6100 KB Output is correct
36 Correct 5 ms 5972 KB Output is correct
37 Correct 4 ms 5972 KB Output is correct
38 Correct 99 ms 8952 KB Output is correct
39 Correct 223 ms 22696 KB Output is correct
40 Correct 54 ms 7884 KB Output is correct
41 Correct 35 ms 6796 KB Output is correct
42 Correct 58 ms 8020 KB Output is correct
43 Correct 28 ms 6788 KB Output is correct
44 Correct 11 ms 6336 KB Output is correct
45 Correct 64 ms 13996 KB Output is correct
46 Correct 56 ms 14072 KB Output is correct
47 Correct 86 ms 17740 KB Output is correct
48 Correct 84 ms 17788 KB Output is correct
49 Correct 56 ms 14292 KB Output is correct
50 Correct 58 ms 14132 KB Output is correct
51 Correct 48 ms 15012 KB Output is correct
52 Correct 50 ms 15116 KB Output is correct
53 Correct 16 ms 6484 KB Output is correct
54 Correct 87 ms 14012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5972 KB Output is correct
4 Correct 3 ms 5972 KB Output is correct
5 Correct 8 ms 5972 KB Output is correct
6 Correct 4 ms 5960 KB Output is correct
7 Correct 3 ms 5972 KB Output is correct
8 Correct 3 ms 5972 KB Output is correct
9 Correct 3 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 5 ms 5972 KB Output is correct
13 Correct 24 ms 6416 KB Output is correct
14 Correct 34 ms 6348 KB Output is correct
15 Correct 33 ms 6336 KB Output is correct
16 Correct 59 ms 14464 KB Output is correct
17 Correct 124 ms 19188 KB Output is correct
18 Correct 280 ms 38952 KB Output is correct
19 Correct 71 ms 14420 KB Output is correct
20 Correct 74 ms 13940 KB Output is correct
21 Correct 70 ms 14076 KB Output is correct
22 Correct 98 ms 20036 KB Output is correct
23 Correct 100 ms 20552 KB Output is correct
24 Correct 100 ms 19576 KB Output is correct
25 Correct 109 ms 19724 KB Output is correct
26 Correct 105 ms 20104 KB Output is correct
27 Correct 114 ms 17836 KB Output is correct
28 Correct 103 ms 17828 KB Output is correct
29 Correct 99 ms 17948 KB Output is correct
30 Correct 95 ms 15896 KB Output is correct
31 Correct 95 ms 15916 KB Output is correct
32 Correct 86 ms 15964 KB Output is correct
33 Correct 80 ms 15932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5972 KB Output is correct
4 Correct 13 ms 5984 KB Output is correct
5 Correct 24 ms 5972 KB Output is correct
6 Correct 4 ms 5972 KB Output is correct
7 Correct 4 ms 5972 KB Output is correct
8 Correct 5 ms 5972 KB Output is correct
9 Correct 84 ms 9564 KB Output is correct
10 Correct 236 ms 22968 KB Output is correct
11 Correct 9 ms 6100 KB Output is correct
12 Correct 39 ms 6532 KB Output is correct
13 Correct 147 ms 51120 KB Output is correct
14 Correct 167 ms 64872 KB Output is correct
15 Execution timed out 5119 ms 838288 KB Time limit exceeded
16 Halted 0 ms 0 KB -