This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |