Submission #831414

# Submission time Handle Problem Language Result Execution time Memory
831414 2023-08-20T08:42:43 Z rainboy LOSTIKS (INOI20_lostiks) C
59 / 100
2000 ms 114668 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	1000000
#define M	20
#define INF	0x3f3f3f3f

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

int n, m;

int *ej[N], *eg[N], eo[N];
int uu[M], vv[M];

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

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

int bb[N];

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

	bb[i] = b;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o], g = eg[i][o];

		if (j != p) {
			if (g == -1)
				dfs1(i, j, b);
			else
				vv[g] = i, dfs1(i, j, b | 1 << g);
		}
	}
}

int dd[N];

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

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

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

int main() {
	static int dds[M], ddt[M], ddd[M][M], dp[1 << M][M];
	int g, g_, h, i, j, u, b, s, t, ans;

	scanf("%d%d%d", &n, &s, &t), s--, t--;
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		eg[i] = (int *) malloc(2 * sizeof *eg[i]);
	}
	m = 0;
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d%d", &i, &j, &u), i--, j--, u--;
		if (u == -1)
			g = -1;
		else
			uu[g = m++] = u;
		append(i, j, g), append(j, i, g);
	}
	dfs1(-1, s, 0);
	if (bb[t] == 0) {
		dfs2(-1, s, 0);
		printf("%d\n", dd[t]);
		return 0;
	}
	for (g_ = 0; g_ < m; g_++) {
		dfs2(-1, uu[g_], 0);
		dds[g_] = dd[s] + dd[vv[g_]];
		for (g = 0; g < m; g++)
			ddd[g][g_] = dd[vv[g]] + dd[vv[g_]];
	}
	dfs2(-1, t, 0);
	for (g = 0; g < m; g++)
		ddt[g] = dd[vv[g]];
	for (b = 1; b < 1 << m; b++)
		memset(dp[b], 0x3f, m * sizeof *dp[b]);
	for (g = 0; g < m; g++)
		if (bb[uu[g]] == 0 && bb[vv[g]] == 0)
			dp[1 << g][g] = dds[g];
	ans = INF;
	for (b = 1; b < 1 << m; b++)
		for (g = 0; g < m; g++) {
			int x = dp[b][g];

			if (x == INF)
				continue;
			if ((bb[t] & b) == bb[t])
				ans = min(ans, x + ddt[g]);
			for (g_ = 0; g_ < m; g_++)
				if ((b & 1 << g_) == 0 && (bb[uu[g_]] & b) == bb[uu[g_]] && (bb[vv[g_]] & b) == bb[vv[g_]])
					dp[b | 1 << g_][g_] = min(dp[b | 1 << g_][g_], x + ddd[g][g_]);
		}
	if (ans == INF)
		ans = -1;
	printf("%d\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Main.c: In function 'main':
Main.c:62:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |  scanf("%d%d%d", &n, &s, &t), s--, t--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:69:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |   scanf("%d%d%d", &i, &j, &u), i--, j--, u--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 61 ms 9880 KB Output is correct
4 Correct 57 ms 9756 KB Output is correct
5 Correct 59 ms 11180 KB Output is correct
6 Correct 69 ms 11216 KB Output is correct
7 Correct 83 ms 11452 KB Output is correct
8 Correct 65 ms 11280 KB Output is correct
9 Correct 58 ms 11452 KB Output is correct
10 Correct 76 ms 11208 KB Output is correct
11 Correct 65 ms 11312 KB Output is correct
12 Correct 89 ms 13196 KB Output is correct
13 Correct 84 ms 13868 KB Output is correct
14 Correct 139 ms 12840 KB Output is correct
15 Correct 107 ms 12984 KB Output is correct
16 Correct 101 ms 14020 KB Output is correct
17 Correct 76 ms 14092 KB Output is correct
18 Correct 71 ms 14524 KB Output is correct
19 Correct 90 ms 18344 KB Output is correct
20 Correct 91 ms 18412 KB Output is correct
21 Correct 121 ms 18344 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 292 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 26 ms 42028 KB Output is correct
6 Correct 28 ms 41940 KB Output is correct
7 Correct 27 ms 41940 KB Output is correct
8 Correct 30 ms 42048 KB Output is correct
9 Correct 29 ms 41972 KB Output is correct
10 Correct 28 ms 42056 KB Output is correct
11 Correct 28 ms 42140 KB Output is correct
12 Correct 29 ms 42028 KB Output is correct
13 Correct 28 ms 42068 KB Output is correct
14 Correct 35 ms 42132 KB Output is correct
15 Correct 29 ms 42096 KB Output is correct
16 Correct 29 ms 42120 KB Output is correct
17 Correct 34 ms 42204 KB Output is correct
18 Correct 27 ms 42188 KB Output is correct
19 Correct 27 ms 42164 KB Output is correct
20 Correct 28 ms 42228 KB Output is correct
21 Correct 29 ms 42320 KB Output is correct
22 Correct 29 ms 42568 KB Output is correct
23 Correct 29 ms 42548 KB Output is correct
24 Correct 28 ms 42580 KB Output is correct
25 Correct 675 ms 82392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 61 ms 9880 KB Output is correct
4 Correct 57 ms 9756 KB Output is correct
5 Correct 59 ms 11180 KB Output is correct
6 Correct 69 ms 11216 KB Output is correct
7 Correct 83 ms 11452 KB Output is correct
8 Correct 65 ms 11280 KB Output is correct
9 Correct 58 ms 11452 KB Output is correct
10 Correct 76 ms 11208 KB Output is correct
11 Correct 65 ms 11312 KB Output is correct
12 Correct 89 ms 13196 KB Output is correct
13 Correct 84 ms 13868 KB Output is correct
14 Correct 139 ms 12840 KB Output is correct
15 Correct 107 ms 12984 KB Output is correct
16 Correct 101 ms 14020 KB Output is correct
17 Correct 76 ms 14092 KB Output is correct
18 Correct 71 ms 14524 KB Output is correct
19 Correct 90 ms 18344 KB Output is correct
20 Correct 91 ms 18412 KB Output is correct
21 Correct 121 ms 18344 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 292 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 26 ms 42028 KB Output is correct
32 Correct 28 ms 41940 KB Output is correct
33 Correct 27 ms 41940 KB Output is correct
34 Correct 30 ms 42048 KB Output is correct
35 Correct 29 ms 41972 KB Output is correct
36 Correct 28 ms 42056 KB Output is correct
37 Correct 28 ms 42140 KB Output is correct
38 Correct 29 ms 42028 KB Output is correct
39 Correct 28 ms 42068 KB Output is correct
40 Correct 35 ms 42132 KB Output is correct
41 Correct 29 ms 42096 KB Output is correct
42 Correct 29 ms 42120 KB Output is correct
43 Correct 34 ms 42204 KB Output is correct
44 Correct 27 ms 42188 KB Output is correct
45 Correct 27 ms 42164 KB Output is correct
46 Correct 28 ms 42228 KB Output is correct
47 Correct 29 ms 42320 KB Output is correct
48 Correct 29 ms 42568 KB Output is correct
49 Correct 29 ms 42548 KB Output is correct
50 Correct 28 ms 42580 KB Output is correct
51 Correct 675 ms 82392 KB Output is correct
52 Execution timed out 2047 ms 114668 KB Time limit exceeded
53 Halted 0 ms 0 KB -