#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
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 |
1 |
Correct |
90 ms |
20420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
57872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
812 KB |
Output is correct |
2 |
Correct |
3 ms |
708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
716 KB |
Output is correct |
2 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
323 ms |
31072 KB |
Output is correct |
2 |
Correct |
329 ms |
31132 KB |
Output is correct |
3 |
Correct |
169 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
329 ms |
31308 KB |
Output is correct |
2 |
Correct |
304 ms |
30996 KB |
Output is correct |
3 |
Correct |
210 ms |
20792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
334 ms |
31428 KB |
Output is correct |
2 |
Correct |
345 ms |
31324 KB |
Output is correct |
3 |
Correct |
218 ms |
20548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
315 ms |
31920 KB |
Output is correct |
2 |
Correct |
335 ms |
31892 KB |
Output is correct |
3 |
Correct |
201 ms |
19192 KB |
Output is correct |