Submission #831405

# Submission time Handle Problem Language Result Execution time Memory
831405 2023-08-20T08:31:29 Z rainboy LOSTIKS (INOI20_lostiks) C
0 / 100
73 ms 11212 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 ii[M], kk[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
				ii[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, k, 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, &k), i--, j--, k--;
		if (k == -1)
			g = -1;
		else
			kk[g = m++] = k;
		append(i, j, g), append(j, i, g);
	}
	dfs1(-1, s, 0);
	dfs2(-1, s, 0);
	if (bb[t] == 0) {
		printf("%d\n", dd[t]);
		return 0;
	}
	for (g = 0; g < m; g++)
		dds[g] = dd[kk[g]];
	dfs2(-1, t, 0);
	for (g = 0; g < m; g++)
		ddt[g] = dd[kk[g]];
	for (g = 0; g < m; g++) {
		dfs2(-1, kk[g], 0);
		for (g_ = 0; g_ < m; g_++)
			ddd[g][g_] = dd[kk[g_]];
	}
	for (b = 1; b < 1 << m; b++)
		memset(dp[b], 0x3f, m * sizeof *dp[b]);
	for (g = 0; g < m; g++)
		if (bb[kk[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[kk[g_]] & b) == bb[kk[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, &k), i--, j--, k--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 73 ms 11212 KB Output is correct
4 Incorrect 65 ms 11108 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 1 ms 428 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 73 ms 11212 KB Output is correct
4 Incorrect 65 ms 11108 KB Output isn't correct
5 Halted 0 ms 0 KB -