Submission #528285

# Submission time Handle Problem Language Result Execution time Memory
528285 2022-02-19T19:29:09 Z rainboy Šarenlist (COCI22_sarenlist) C
110 / 110
18 ms 332 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	60
#define M	15
#define MD	1000000007

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 dd[N], pp[N];

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

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

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

int ds[N];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

void join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
}

int main() {
	static int ii[M], jj[M];
	int n, m, c, h, i, j, k, b, ans;

	scanf("%d%d%d", &n, &m, &c);
	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);
	}
	dfs(-1, 0, 0);
	for (h = 0; h < m; h++)
		scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
	ans = 0;
	for (b = 0; b < 1 << m; b++) {
		int x;

		memset(ds, -1, n * sizeof *ds);
		x = 1;
		for (h = 0; h < m; h++)
			if ((b & 1 << h) != 0) {
				x = MD - x;
				i = ii[h], j = jj[h], k = -1;
				while (i != j)
					if (dd[i] > dd[j]) {
						if (k != -1)
							join(k, i);
						k = i, i = pp[i];
					} else {
						if (k != -1)
							join(k, j);
						k = j, j = pp[j];
					}
			}
		for (i = 1; i < n; i++)
			if (ds[i] < 0)
				x = (long long) x * c % MD;
		ans = (ans + x) % MD;
	}
	printf("%d\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |  scanf("%d%d%d", &n, &m, &c);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
Main.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |   scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 276 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 276 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 272 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 260 KB Output is correct
4 Correct 1 ms 272 KB Output is correct
5 Correct 2 ms 272 KB Output is correct
6 Correct 1 ms 276 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 276 KB Output is correct
2 Correct 0 ms 272 KB Output is correct
3 Correct 0 ms 272 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 276 KB Output is correct
6 Correct 3 ms 272 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 276 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 276 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 276 KB Output is correct
13 Correct 1 ms 272 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 260 KB Output is correct
16 Correct 1 ms 272 KB Output is correct
17 Correct 2 ms 272 KB Output is correct
18 Correct 1 ms 276 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 276 KB Output is correct
21 Correct 0 ms 272 KB Output is correct
22 Correct 0 ms 272 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 276 KB Output is correct
25 Correct 3 ms 272 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 8 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 9 ms 204 KB Output is correct
31 Correct 3 ms 204 KB Output is correct
32 Correct 1 ms 276 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 276 KB Output is correct
35 Correct 5 ms 280 KB Output is correct
36 Correct 18 ms 272 KB Output is correct
37 Correct 8 ms 204 KB Output is correct