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 300000
#define MD 1000000007
int min(int a, int b) { return a < b ? a : b; }
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], qq[N], ta[N], inv[N], cnt;
int dfs1(int p, int i, int d) {
int o, s, k_, j_;
dd[i] = d, pp[i] = p;
s = 1, k_ = 0, j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
int k = dfs1(i, j, d + 1);
s += k;
if (k_ < k)
k_ = k, j_ = j;
}
}
qq[i] = j_;
return s;
}
void dfs2(int p, int i, int q) {
int j_, o;
inv[ta[i] = cnt++] = i;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(i, j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && j != j_)
dfs2(i, j, j);
}
}
int lca(int i, int j) {
while (qq[i] != qq[j])
if (dd[qq[i]] > dd[qq[j]])
i = pp[qq[i]];
else
j = pp[qq[j]];
return dd[i] < dd[j] ? i : j;
}
int ancestor(int i, int d) {
while (dd[qq[i]] > d)
i = pp[qq[i]];
return inv[ta[i] - (dd[i] - d)];
}
int ds[N * 2];
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 kk[N];
void dfs3(int p, int i) {
int o;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs3(i, j);
kk[i] += kk[j];
}
}
if (kk[i] > 0)
join(pp[i] << 1 | 0, i << 1 | 0), join(pp[i] << 1 | 1, i << 1 | 1);
}
int main() {
int n, m, h, i, j, k, ans;
scanf("%d%d", &n, &m);
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);
}
dfs1(-1, 0, 0);
dfs2(-1, 0, 0);
memset(ds, -1, n * 2 * sizeof *ds);
for (h = 0; h < m; h++) {
int a, b, c;
scanf("%d%d", &i, &j), i--, j--;
a = lca(i, j), b = i == a ? a : ancestor(i, dd[a] + 1), c = j == a ? a : ancestor(j, dd[a] + 1);
if (i != a && j != a)
join(b << 1 | 0, c << 1 | 1), join(b << 1 | 1, c << 1 | 0);
kk[i]++, kk[b]--, kk[j]++, kk[c]--;
}
dfs3(-1, 0);
for (i = 0; i < n; i++)
if (find(i << 1 | 0) == find(i << 1 | 1)) {
printf("0\n");
return 0;
}
k = 0;
for (i = 0; i < n * 2; i++)
if (ds[i] < 0)
k++;
k = k / 2 - 1;
ans = 1;
while (k--)
ans = ans * 2 % MD;
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
usmjeri.c: In function 'append':
usmjeri.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
usmjeri.c: In function 'main':
usmjeri.c:112:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
usmjeri.c:116:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
usmjeri.c:125:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | 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... |