답안 #636585

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636585 2022-08-29T15:56:19 Z rainboy Jail (JOI22_jail) C
77 / 100
5000 ms 753028 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	120000

int *ej[N], eo[N], *eh[N], eo_[N], fo_[N];

void append(int **ej, int *eo, int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int dd[N], pp[N];

void dfs1(int p, int i, int d) {
	int o;

	dd[i] = d, pp[i] = p;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs1(i, j, d + 1);
	}
}

void dfs2(int h) {
	int o;

	if (fo_[h])
		return;
	fo_[h] = -1;
	for (o = eo_[h]; o--; ) {
		int h_ = eh[h][o];

		fo_[h_]--, dfs2(h_);
	}
}

int main() {
	int t;

	scanf("%d", &t);
	while (t--) {
		static int ii[N], jj[N], hhi[N], hhj[N];
		int n, m, h, h_, i, j, line, yes;

		scanf("%d", &n);
		for (i = 0; i < n; i++)
			ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
		line = 1;
		for (h = 0; h < n - 1; h++) {
			scanf("%d%d", &i, &j), i--, j--;
			if (i + 1 != j)
				line = 0;
			append(ej, eo, i, j), append(ej, eo, j, i);
		}
		dfs1(-1, 0, 0);
		if (line) {
			scanf("%d", &m);
			memset(jj, -1, n * sizeof *jj);
			while (m--) {
				scanf("%d%d", &i, &j), i--, j--;
				jj[i] = j;
			}
			yes = 1;
			for (i = 0, j = -1; i < n; i++)
				if (jj[i] != -1) {
					if (j >= jj[i]) {
						yes = 0;
						break;
					}
					j = jj[i];
				}
			printf(yes ? "Yes\n" : "No\n");
		} else {
			scanf("%d", &m);
			memset(hhi, -1, n * sizeof *hhi), memset(hhj, -1, n * sizeof *hhj);
			for (h = 0; h < m; h++) {
				scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
				hhi[ii[h]] = hhj[jj[h]] = h;
			}
			for (h = 0; h < m; h++)
				eh[h] = (int *) malloc(2 * sizeof *eh[h]), eo_[h] = 0, fo_[h] = 0;
			for (h = 0; h < m; h++) {
				i = ii[h], j = jj[h];
				while (i != j)
					if (dd[i] > dd[j]) {
						h_ = hhi[i];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h_, h), fo_[h]++;
						h_ = hhj[i];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h, h_), fo_[h_]++;
						i = pp[i];
					} else {
						h_ = hhi[j];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h_, h), fo_[h]++;
						h_ = hhj[j];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h, h_), fo_[h_]++;
						j = pp[j];
					}
				h_ = hhi[i];
				if (h_ != -1 && h_ != h)
					append(eh, eo_, h_, h), fo_[h]++;
				h_ = hhj[i];
				if (h_ != -1 && h_ != h)
					append(eh, eo_, h, h_), fo_[h_]++;
			}
			for (h = 0; h < m; h++)
				dfs2(h);
			yes = 1;
			for (h = 0; h < m; h++)
				if (fo_[h] > 0) {
					yes = 0;
					break;
				}
			printf(yes ? "Yes\n" : "No\n");
			for (h = 0; h < m; h++)
				free(eh[h]);
		}
		for (i = 0; i < n; i++)
			free(ej[i]);
	}
	return 0;
}

Compilation message

jail.c: In function 'append':
jail.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
jail.c: In function 'main':
jail.c:47:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
jail.c:52:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |   scanf("%d", &n);
      |   ^~~~~~~~~~~~~~~
jail.c:57:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |    scanf("%d%d", &i, &j), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
jail.c:64:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |    scanf("%d", &m);
      |    ^~~~~~~~~~~~~~~
jail.c:67:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d%d", &i, &j), i--, j--;
      |     ^~~~~~~~~~~~~~~~~~~~~
jail.c:81:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |    scanf("%d", &m);
      |    ^~~~~~~~~~~~~~~
jail.c:84:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 11 ms 324 KB Output is correct
5 Correct 23 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 31 ms 1104 KB Output is correct
10 Correct 33 ms 16216 KB Output is correct
11 Correct 6 ms 212 KB Output is correct
12 Correct 29 ms 212 KB Output is correct
13 Correct 50 ms 16204 KB Output is correct
14 Correct 46 ms 16164 KB Output is correct
15 Correct 48 ms 16328 KB Output is correct
16 Correct 55 ms 16460 KB Output is correct
17 Correct 43 ms 16208 KB Output is correct
18 Correct 54 ms 16236 KB Output is correct
19 Correct 45 ms 16224 KB Output is correct
20 Correct 45 ms 16240 KB Output is correct
21 Correct 56 ms 16240 KB Output is correct
22 Correct 58 ms 16208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 2 ms 212 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 2 ms 212 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 28 ms 1008 KB Output is correct
39 Correct 35 ms 16200 KB Output is correct
40 Correct 71 ms 1500 KB Output is correct
41 Correct 31 ms 724 KB Output is correct
42 Correct 63 ms 1632 KB Output is correct
43 Correct 27 ms 880 KB Output is correct
44 Correct 11 ms 484 KB Output is correct
45 Correct 50 ms 7440 KB Output is correct
46 Correct 44 ms 7352 KB Output is correct
47 Correct 83 ms 12048 KB Output is correct
48 Correct 87 ms 11968 KB Output is correct
49 Correct 46 ms 7584 KB Output is correct
50 Correct 44 ms 7628 KB Output is correct
51 Correct 35 ms 8984 KB Output is correct
52 Correct 34 ms 8920 KB Output is correct
53 Correct 9 ms 852 KB Output is correct
54 Correct 54 ms 7244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 5 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 29 ms 376 KB Output is correct
14 Correct 37 ms 340 KB Output is correct
15 Correct 31 ms 380 KB Output is correct
16 Correct 48 ms 8080 KB Output is correct
17 Correct 109 ms 16644 KB Output is correct
18 Correct 254 ms 37024 KB Output is correct
19 Correct 48 ms 10220 KB Output is correct
20 Correct 52 ms 10384 KB Output is correct
21 Correct 47 ms 10256 KB Output is correct
22 Correct 90 ms 17264 KB Output is correct
23 Correct 79 ms 17452 KB Output is correct
24 Correct 81 ms 17100 KB Output is correct
25 Correct 83 ms 17360 KB Output is correct
26 Correct 85 ms 17224 KB Output is correct
27 Correct 95 ms 22752 KB Output is correct
28 Correct 82 ms 17208 KB Output is correct
29 Correct 76 ms 17128 KB Output is correct
30 Correct 59 ms 15696 KB Output is correct
31 Correct 69 ms 12896 KB Output is correct
32 Correct 74 ms 15784 KB Output is correct
33 Correct 66 ms 12900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 11 ms 324 KB Output is correct
5 Correct 23 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 31 ms 1104 KB Output is correct
10 Correct 33 ms 16216 KB Output is correct
11 Correct 6 ms 212 KB Output is correct
12 Correct 29 ms 212 KB Output is correct
13 Correct 50 ms 16204 KB Output is correct
14 Correct 46 ms 16164 KB Output is correct
15 Correct 48 ms 16328 KB Output is correct
16 Correct 55 ms 16460 KB Output is correct
17 Correct 43 ms 16208 KB Output is correct
18 Correct 54 ms 16236 KB Output is correct
19 Correct 45 ms 16224 KB Output is correct
20 Correct 45 ms 16240 KB Output is correct
21 Correct 56 ms 16240 KB Output is correct
22 Correct 58 ms 16208 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 2 ms 212 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 2 ms 340 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 3 ms 340 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 28 ms 1008 KB Output is correct
61 Correct 35 ms 16200 KB Output is correct
62 Correct 71 ms 1500 KB Output is correct
63 Correct 31 ms 724 KB Output is correct
64 Correct 63 ms 1632 KB Output is correct
65 Correct 27 ms 880 KB Output is correct
66 Correct 11 ms 484 KB Output is correct
67 Correct 50 ms 7440 KB Output is correct
68 Correct 44 ms 7352 KB Output is correct
69 Correct 83 ms 12048 KB Output is correct
70 Correct 87 ms 11968 KB Output is correct
71 Correct 46 ms 7584 KB Output is correct
72 Correct 44 ms 7628 KB Output is correct
73 Correct 35 ms 8984 KB Output is correct
74 Correct 34 ms 8920 KB Output is correct
75 Correct 9 ms 852 KB Output is correct
76 Correct 54 ms 7244 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 0 ms 340 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct
81 Correct 5 ms 212 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 1 ms 212 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 1 ms 212 KB Output is correct
88 Correct 2 ms 340 KB Output is correct
89 Correct 29 ms 376 KB Output is correct
90 Correct 37 ms 340 KB Output is correct
91 Correct 31 ms 380 KB Output is correct
92 Correct 48 ms 8080 KB Output is correct
93 Correct 109 ms 16644 KB Output is correct
94 Correct 254 ms 37024 KB Output is correct
95 Correct 48 ms 10220 KB Output is correct
96 Correct 52 ms 10384 KB Output is correct
97 Correct 47 ms 10256 KB Output is correct
98 Correct 90 ms 17264 KB Output is correct
99 Correct 79 ms 17452 KB Output is correct
100 Correct 81 ms 17100 KB Output is correct
101 Correct 83 ms 17360 KB Output is correct
102 Correct 85 ms 17224 KB Output is correct
103 Correct 95 ms 22752 KB Output is correct
104 Correct 82 ms 17208 KB Output is correct
105 Correct 76 ms 17128 KB Output is correct
106 Correct 59 ms 15696 KB Output is correct
107 Correct 69 ms 12896 KB Output is correct
108 Correct 74 ms 15784 KB Output is correct
109 Correct 66 ms 12900 KB Output is correct
110 Correct 39 ms 1320 KB Output is correct
111 Correct 24 ms 968 KB Output is correct
112 Execution timed out 5132 ms 753028 KB Time limit exceeded
113 Halted 0 ms 0 KB -