Submission #831411

# Submission time Handle Problem Language Result Execution time Memory
831411 2023-08-20T08:40:55 Z rainboy LOSTIKS (INOI20_lostiks) C
0 / 100
88 ms 42148 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)
			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 0 ms 212 KB Output is correct
3 Correct 88 ms 9804 KB Output is correct
4 Incorrect 68 ms 9808 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 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 29 ms 42112 KB Output is correct
6 Correct 30 ms 42148 KB Output is correct
7 Correct 31 ms 42024 KB Output is correct
8 Correct 31 ms 42108 KB Output is correct
9 Correct 30 ms 42088 KB Output is correct
10 Incorrect 31 ms 42132 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 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 88 ms 9804 KB Output is correct
4 Incorrect 68 ms 9808 KB Output isn't correct
5 Halted 0 ms 0 KB -