Submission #1072314

# Submission time Handle Problem Language Result Execution time Memory
1072314 2024-08-23T16:52:34 Z rainboy Summer Driving (CCO24_day1problem3) C
25 / 25
2854 ms 58692 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	300000
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N))) */
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

int n, r, a, b;
int *ej[N], eo[N];

void append(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 ii[N], dd[N], pp[N], pp_[N], ll[N], ta[N], tb[N], qu[N];

void dfs(int i) {
	static int t_;
	static int stack[N];
	int o;

	qu[ta[i] = t_++] = i;
	stack[dd[i]] = i;
	pp_[i] = dd[i] < a - 1 ? -1 : stack[dd[i] - a + 1];
	ll[i] = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != pp[i]) {
			dfs(j);
			ll[i] = max(ll[i], ll[j] + 1);
		}
	}
	tb[i] = t_;
}

int st[N_ * 2], n_;

void pul(int i) {
	st[i] = min(st[i << 1 | 0], st[i << 1 | 1]);
}

void pull(int i) {
	while (i > 1)
		pul(i >>= 1);
}

char dp[N], dq[N];

void build(int n) {
	int a, i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	memset(st, 0x3f, n_ * 2 * sizeof *st);
	for (a = 0; a < n; a++) {
		i = qu[a];
		dq[i] = 1, st[n_ + a] = dd[i];
	}
	for (i = n_ - 1; i > 0; i--)
		pul(i);
}

void mark_(int i, int d) {
	if (st[i] > d)
		return;
	if (i >= n_) {
		int a = i - n_;

		i = qu[a];
		dq[i] = 0, st[n_ + a] = INF;
		return;
	}
	mark_(i << 1 | 0, d), mark_(i << 1 | 1, d), pul(i);
}

void mark(int l, int r, int d) {
	int l_ = l += n_, r_ = r += n_;

	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			mark_(l++, d);
		if ((r & 1) == 0)
			mark_(r--, d);
	}
	pull(l_), pull(r_);
}

int solve(int k) {
	static char dq_[N];
	static int dd1[N], dd2[N];
	int h, h_, i, j, j_, j1, j2, o, bad;

	for (h = n - 1; h >= 0; h--) {
		i = ii[h];
		dd1[i] = i < k ? 0 : INF;
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (j != pp[i])
				dd1[i] = min(dd1[i], dd1[j] + 1);
		}
	}
	build(n);
	memset(dq_, 0, n * sizeof *dq_);
	for (h = n - 1, h_ = n - 1; h >= 0; h--) {
		i = ii[h];
		while (h_ >= 0 && dd[j = ii[h_]] - dd[i] >= a) {
			if (dq[j] && pp_[j] != -1)
				dq_[pp_[j]] = 1;
			h_--;
		}
		if (ll[i] < a) {
			if (dd1[i] > b)
				dp[i] = 1;
			else {
				dp[i] = 0;
				j_ = i >= k ? -1 : -2;
				for (o = eo[i]; o--; ) {
					j = ej[i][o];
					if (j != pp[i] && dd1[j] < b)
						j_ = j_ == -1 ? j : -2;
				}
				if (j_ == -2)
					mark(ta[i], tb[i] - 1, dd[i] + b);
				else
					mark(ta[i], ta[j_] - 1, dd[i] + b), mark(tb[j_], tb[i] - 1, dd[i] + b);
			}
		} else {
			j_ = -1;
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				if (j != pp[i] && dq_[j])
					j_ = j_ == -1 ? j : -2;
			}
			if (j_ != -1)
				dp[i] = 1;
			else {
				dp[i] = 0;
				j_ = -2;
				for (o = eo[i]; o--; ) {
					j = ej[i][o];
					if (j != pp[i] && ll[j] >= a - 1) {
						j_ = j;
						break;
					}
				}
				mark(ta[i], ta[j_] - 1, dd[i] + b), mark(tb[j_], tb[i] - 1, dd[i] + b);
			}
			if (j_ != -2) {
				bad = i < k;
				for (o = eo[i]; o--; ) {
					j = ej[i][o];
					if (j != pp[i] && j != j_ && (ll[j] >= a - 1 || dd1[j] < b)) {
						bad = 1;
						break;
					}
				}
				if (bad)
					mark(ta[j_], tb[j_] - 1, dd[i] + b);
			}
		}
		j1 = j2 = -1;
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (j != pp[i]) {
				if (j1 == -1 || dd2[j1] > dd2[j])
					j2 = j1, j1 = j;
				else if (j2 == -1 || dd2[j2] > dd2[j])
					j2 = j;
			}
		}
		dd2[i] = !dp[i] ? 0 : (j1 == -1 || dd2[j1] == INF ? INF : dd2[j1] + 1);
		if (j1 != -1)
			mark(ta[i], ta[i], dd[i] + b - (dd2[j1] + 1));
		for (o = eo[i]; o--; ) {
			j = ej[i][o], j_ = j != j1 ? j1 : j2;
			if (j != pp[i] && j_ != -1)
				mark(ta[j], tb[j] - 1, dd[i] + b - (dd2[j_] + 1));
		}
	}
	return dp[r];
}

int main() {
	int cnt, h, i, j, o, k, lower, upper;

	scanf("%d%d%d%d", &n, &r, &a, &b), r--;
	if (a <= b) {
		printf("1\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	cnt = 0;
	dd[r] = 0, pp[r] = -1, ii[cnt++] = r;
	for (h = 0; h < cnt; h++) {
		i = ii[h];
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (j != pp[i])
				dd[j] = dd[i] + 1, pp[j] = i, ii[cnt++] = j;
		}
	}
	dfs(r);
	lower = 0, upper = n;
	while (upper - lower > 1) {
		k = (lower + upper) / 2;
		if (solve(k))
			lower = k;
		else
			upper = k;
	}
	printf("%d\n", upper);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   18 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:196:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  196 |  scanf("%d%d%d%d", &n, &r, &a, &b), r--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:204:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  204 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2492 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2246 ms 56168 KB Output is correct
2 Correct 2854 ms 58448 KB Output is correct
3 Correct 1667 ms 58324 KB Output is correct
4 Correct 1677 ms 58452 KB Output is correct
5 Correct 2815 ms 56664 KB Output is correct
6 Correct 2126 ms 58196 KB Output is correct
7 Correct 1980 ms 58432 KB Output is correct
8 Correct 2120 ms 58692 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 2078 ms 58468 KB Output is correct
11 Correct 2744 ms 58452 KB Output is correct
12 Correct 0 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2584 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2584 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 7 ms 2648 KB Output is correct
15 Correct 6 ms 2824 KB Output is correct
16 Correct 7 ms 2648 KB Output is correct
17 Correct 7 ms 4952 KB Output is correct
18 Correct 8 ms 2820 KB Output is correct
19 Correct 6 ms 2652 KB Output is correct
20 Correct 6 ms 2652 KB Output is correct
21 Correct 5 ms 2652 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 5 ms 4696 KB Output is correct
24 Correct 7 ms 2652 KB Output is correct
25 Correct 6 ms 2652 KB Output is correct
26 Correct 6 ms 2652 KB Output is correct
27 Correct 4 ms 2652 KB Output is correct
28 Correct 4 ms 2648 KB Output is correct
29 Correct 4 ms 2648 KB Output is correct
30 Correct 4 ms 2628 KB Output is correct
31 Correct 4 ms 2652 KB Output is correct
32 Correct 4 ms 2652 KB Output is correct
33 Correct 6 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 496 ms 11296 KB Output is correct
2 Correct 393 ms 12624 KB Output is correct
3 Correct 494 ms 12888 KB Output is correct
4 Correct 489 ms 12536 KB Output is correct
5 Correct 543 ms 12836 KB Output is correct
6 Correct 450 ms 12636 KB Output is correct
7 Correct 400 ms 12636 KB Output is correct
8 Correct 315 ms 13076 KB Output is correct
9 Correct 404 ms 12624 KB Output is correct
10 Correct 455 ms 12624 KB Output is correct
11 Correct 442 ms 12628 KB Output is correct
12 Correct 296 ms 13136 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 338 ms 10392 KB Output is correct
16 Correct 228 ms 11348 KB Output is correct
17 Correct 233 ms 12872 KB Output is correct
18 Correct 238 ms 13072 KB Output is correct
19 Correct 739 ms 20816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2584 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 7 ms 2648 KB Output is correct
15 Correct 6 ms 2824 KB Output is correct
16 Correct 7 ms 2648 KB Output is correct
17 Correct 7 ms 4952 KB Output is correct
18 Correct 8 ms 2820 KB Output is correct
19 Correct 6 ms 2652 KB Output is correct
20 Correct 6 ms 2652 KB Output is correct
21 Correct 5 ms 2652 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 5 ms 4696 KB Output is correct
24 Correct 7 ms 2652 KB Output is correct
25 Correct 6 ms 2652 KB Output is correct
26 Correct 6 ms 2652 KB Output is correct
27 Correct 4 ms 2652 KB Output is correct
28 Correct 4 ms 2648 KB Output is correct
29 Correct 4 ms 2648 KB Output is correct
30 Correct 4 ms 2628 KB Output is correct
31 Correct 4 ms 2652 KB Output is correct
32 Correct 4 ms 2652 KB Output is correct
33 Correct 6 ms 2908 KB Output is correct
34 Correct 544 ms 12892 KB Output is correct
35 Correct 408 ms 12624 KB Output is correct
36 Correct 518 ms 12676 KB Output is correct
37 Correct 424 ms 12620 KB Output is correct
38 Correct 561 ms 12892 KB Output is correct
39 Correct 385 ms 12720 KB Output is correct
40 Correct 405 ms 12712 KB Output is correct
41 Correct 316 ms 12892 KB Output is correct
42 Correct 0 ms 2392 KB Output is correct
43 Correct 349 ms 10368 KB Output is correct
44 Correct 240 ms 13064 KB Output is correct
45 Correct 246 ms 13140 KB Output is correct
46 Correct 222 ms 13008 KB Output is correct
47 Correct 313 ms 15604 KB Output is correct
48 Correct 516 ms 21968 KB Output is correct
49 Correct 406 ms 13804 KB Output is correct
50 Correct 381 ms 13904 KB Output is correct
51 Correct 392 ms 13892 KB Output is correct
52 Correct 234 ms 14276 KB Output is correct
53 Correct 240 ms 14500 KB Output is correct
54 Correct 234 ms 14164 KB Output is correct
55 Correct 493 ms 21584 KB Output is correct
56 Correct 509 ms 21840 KB Output is correct
57 Correct 287 ms 13652 KB Output is correct
58 Correct 350 ms 13648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2492 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2480 KB Output is correct
6 Correct 2246 ms 56168 KB Output is correct
7 Correct 2854 ms 58448 KB Output is correct
8 Correct 1667 ms 58324 KB Output is correct
9 Correct 1677 ms 58452 KB Output is correct
10 Correct 2815 ms 56664 KB Output is correct
11 Correct 2126 ms 58196 KB Output is correct
12 Correct 1980 ms 58432 KB Output is correct
13 Correct 2120 ms 58692 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 2078 ms 58468 KB Output is correct
16 Correct 2744 ms 58452 KB Output is correct
17 Correct 0 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 4440 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2584 KB Output is correct
29 Correct 0 ms 2396 KB Output is correct
30 Correct 0 ms 2396 KB Output is correct
31 Correct 7 ms 2648 KB Output is correct
32 Correct 6 ms 2824 KB Output is correct
33 Correct 7 ms 2648 KB Output is correct
34 Correct 7 ms 4952 KB Output is correct
35 Correct 8 ms 2820 KB Output is correct
36 Correct 6 ms 2652 KB Output is correct
37 Correct 6 ms 2652 KB Output is correct
38 Correct 5 ms 2652 KB Output is correct
39 Correct 1 ms 2396 KB Output is correct
40 Correct 5 ms 4696 KB Output is correct
41 Correct 7 ms 2652 KB Output is correct
42 Correct 6 ms 2652 KB Output is correct
43 Correct 6 ms 2652 KB Output is correct
44 Correct 4 ms 2652 KB Output is correct
45 Correct 4 ms 2648 KB Output is correct
46 Correct 4 ms 2648 KB Output is correct
47 Correct 4 ms 2628 KB Output is correct
48 Correct 4 ms 2652 KB Output is correct
49 Correct 4 ms 2652 KB Output is correct
50 Correct 6 ms 2908 KB Output is correct
51 Correct 496 ms 11296 KB Output is correct
52 Correct 393 ms 12624 KB Output is correct
53 Correct 494 ms 12888 KB Output is correct
54 Correct 489 ms 12536 KB Output is correct
55 Correct 543 ms 12836 KB Output is correct
56 Correct 450 ms 12636 KB Output is correct
57 Correct 400 ms 12636 KB Output is correct
58 Correct 315 ms 13076 KB Output is correct
59 Correct 404 ms 12624 KB Output is correct
60 Correct 455 ms 12624 KB Output is correct
61 Correct 442 ms 12628 KB Output is correct
62 Correct 296 ms 13136 KB Output is correct
63 Correct 0 ms 2396 KB Output is correct
64 Correct 0 ms 2396 KB Output is correct
65 Correct 338 ms 10392 KB Output is correct
66 Correct 228 ms 11348 KB Output is correct
67 Correct 233 ms 12872 KB Output is correct
68 Correct 238 ms 13072 KB Output is correct
69 Correct 739 ms 20816 KB Output is correct
70 Correct 544 ms 12892 KB Output is correct
71 Correct 408 ms 12624 KB Output is correct
72 Correct 518 ms 12676 KB Output is correct
73 Correct 424 ms 12620 KB Output is correct
74 Correct 561 ms 12892 KB Output is correct
75 Correct 385 ms 12720 KB Output is correct
76 Correct 405 ms 12712 KB Output is correct
77 Correct 316 ms 12892 KB Output is correct
78 Correct 0 ms 2392 KB Output is correct
79 Correct 349 ms 10368 KB Output is correct
80 Correct 240 ms 13064 KB Output is correct
81 Correct 246 ms 13140 KB Output is correct
82 Correct 222 ms 13008 KB Output is correct
83 Correct 313 ms 15604 KB Output is correct
84 Correct 516 ms 21968 KB Output is correct
85 Correct 406 ms 13804 KB Output is correct
86 Correct 381 ms 13904 KB Output is correct
87 Correct 392 ms 13892 KB Output is correct
88 Correct 234 ms 14276 KB Output is correct
89 Correct 240 ms 14500 KB Output is correct
90 Correct 234 ms 14164 KB Output is correct
91 Correct 493 ms 21584 KB Output is correct
92 Correct 509 ms 21840 KB Output is correct
93 Correct 287 ms 13652 KB Output is correct
94 Correct 350 ms 13648 KB Output is correct
95 Correct 2236 ms 34812 KB Output is correct
96 Correct 1744 ms 34304 KB Output is correct
97 Correct 2178 ms 34644 KB Output is correct
98 Correct 1726 ms 34292 KB Output is correct
99 Correct 2475 ms 34800 KB Output is correct
100 Correct 1970 ms 34128 KB Output is correct
101 Correct 1857 ms 34132 KB Output is correct
102 Correct 1377 ms 35156 KB Output is correct
103 Correct 0 ms 2396 KB Output is correct
104 Correct 1196 ms 26168 KB Output is correct
105 Correct 1083 ms 35152 KB Output is correct
106 Correct 1040 ms 34900 KB Output is correct
107 Correct 1069 ms 34900 KB Output is correct
108 Correct 1112 ms 42580 KB Output is correct
109 Correct 2185 ms 58448 KB Output is correct
110 Correct 1678 ms 34132 KB Output is correct
111 Correct 1712 ms 34152 KB Output is correct
112 Correct 1654 ms 34128 KB Output is correct
113 Correct 1104 ms 35748 KB Output is correct
114 Correct 1093 ms 35692 KB Output is correct
115 Correct 1163 ms 35668 KB Output is correct
116 Correct 2184 ms 57424 KB Output is correct
117 Correct 2111 ms 58440 KB Output is correct
118 Correct 1057 ms 33872 KB Output is correct
119 Correct 1552 ms 33740 KB Output is correct