답안 #1072304

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1072304 2024-08-23T16:42:40 Z rainboy Summer Driving (CCO24_day1problem3) C
9 / 25
6000 ms 52820 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	300000
#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_;
}

char dp[N], dq[N];

void mark(int l, int r, int d) {
	int i, a;

	for (a = l; a <= r; a++) {
		i = qu[a];
		if (dd[i] <= d && dq[i])
			dq[i] = 0;
	}
}

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);
		}
	}
	memset(dq, 1, n * sizeof *dq), 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:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   17 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:153:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |  scanf("%d%d%d%d", &n, &r, &a, &b), r--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2492 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6074 ms 52820 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 4444 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 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 4444 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 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 3 ms 2652 KB Output is correct
15 Correct 3 ms 2744 KB Output is correct
16 Correct 3 ms 2652 KB Output is correct
17 Correct 4 ms 2652 KB Output is correct
18 Correct 3 ms 2652 KB Output is correct
19 Correct 3 ms 2648 KB Output is correct
20 Correct 4 ms 2648 KB Output is correct
21 Correct 4 ms 4700 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 5 ms 2652 KB Output is correct
24 Correct 7 ms 2908 KB Output is correct
25 Correct 6 ms 2648 KB Output is correct
26 Correct 5 ms 2652 KB Output is correct
27 Correct 2 ms 2700 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 3 ms 2904 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 10 ms 3040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 287 ms 11612 KB Output is correct
2 Correct 225 ms 11388 KB Output is correct
3 Correct 228 ms 11604 KB Output is correct
4 Correct 276 ms 11440 KB Output is correct
5 Correct 254 ms 11600 KB Output is correct
6 Correct 241 ms 12628 KB Output is correct
7 Correct 244 ms 11344 KB Output is correct
8 Correct 148 ms 13136 KB Output is correct
9 Correct 318 ms 11344 KB Output is correct
10 Correct 358 ms 11348 KB Output is correct
11 Correct 361 ms 11348 KB Output is correct
12 Correct 134 ms 11868 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 740 ms 10368 KB Output is correct
16 Correct 94 ms 11660 KB Output is correct
17 Correct 91 ms 11596 KB Output is correct
18 Correct 95 ms 11620 KB Output is correct
19 Execution timed out 6038 ms 20836 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 4444 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 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 3 ms 2652 KB Output is correct
15 Correct 3 ms 2744 KB Output is correct
16 Correct 3 ms 2652 KB Output is correct
17 Correct 4 ms 2652 KB Output is correct
18 Correct 3 ms 2652 KB Output is correct
19 Correct 3 ms 2648 KB Output is correct
20 Correct 4 ms 2648 KB Output is correct
21 Correct 4 ms 4700 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 5 ms 2652 KB Output is correct
24 Correct 7 ms 2908 KB Output is correct
25 Correct 6 ms 2648 KB Output is correct
26 Correct 5 ms 2652 KB Output is correct
27 Correct 2 ms 2700 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 3 ms 2904 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 10 ms 3040 KB Output is correct
34 Correct 263 ms 11576 KB Output is correct
35 Correct 248 ms 11440 KB Output is correct
36 Correct 248 ms 12888 KB Output is correct
37 Correct 251 ms 11344 KB Output is correct
38 Correct 228 ms 11600 KB Output is correct
39 Correct 223 ms 11352 KB Output is correct
40 Correct 263 ms 11348 KB Output is correct
41 Correct 147 ms 13148 KB Output is correct
42 Correct 0 ms 2396 KB Output is correct
43 Correct 707 ms 10188 KB Output is correct
44 Correct 96 ms 11612 KB Output is correct
45 Correct 102 ms 11704 KB Output is correct
46 Correct 89 ms 11604 KB Output is correct
47 Execution timed out 6022 ms 14280 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2492 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Execution timed out 6074 ms 52820 KB Time limit exceeded
7 Halted 0 ms 0 KB -