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 <bits/stdc++.h>
using namespace std;
const int N = 300010, LOG = 20;
const int MOD = 1000000007;
inline int add(int u, int v) {u += v; if (u >= MOD) u -= MOD; return u;}
inline int sub(int u, int v) {u -= v; if (u < 0) u += MOD; return u;}
inline int mul(int u, int v) {return (long long)u * v % MOD;};
inline int power(int u, int v) {int res = 1; while (v) {if (v & 1) res = mul(res, u); u = mul(u, u); v >>= 1;} return res;}
inline int inv(int u) {return power(u, MOD - 2);}
int n, m;
vector<int> adj[N];
vector<pair<int, int> > adj2[N];
int lv[N], anc[LOG][N], used[N], dir[N];
int minLv[N];
int lca(int u, int v) {
if (lv[u] > lv[v]) {
swap(u, v);
}
int h = lv[v] - lv[u];
for (int i = LOG - 1; i >= 0; i--) {
if ((h >> i) & 1) {
v = anc[i][v];
}
}
if (u == v) {
return u;
}
for (int i = LOG - 1; i >= 0; i--) {
if (anc[i][u] != anc[i][v]) {
u = anc[i][u];
v = anc[i][v];
}
}
return anc[0][u];
}
void dfsInit(int u, int p) {
lv[u] = lv[p] + 1;
minLv[u] = lv[u];
anc[0][u] = p;
for (int i = 1; i < LOG; i++) {
anc[i][u] = anc[i - 1][anc[i - 1][u]];
}
for (int v : adj[u]) {
if (v != p) {
dfsInit(v, u);
}
}
}
void dfsCalc(int u, int p) {
for (int v : adj[u]) {
if (v != p) {
dfsCalc(v, u);
minLv[u] = min(minLv[u], minLv[v]);
if (minLv[v] < lv[u]) {
adj2[u].emplace_back(v, 0);
adj2[v].emplace_back(u, 0);
}
}
}
}
bool dfsRes(int u, int d) {
if (used[u]) {
return dir[u] == d;
}
used[u] = 1;
dir[u] = d;
for (pair<int, int> v : adj2[u]) {
if (!dfsRes(v.first, d ^ v.second)) {
return 0;
}
}
return 1;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfsInit(1, 0);
while (m--) {
int u, v;
scanf("%d %d", &u, &v);
int w = lca(u, v);
minLv[u] = min(minLv[u], lv[w]);
minLv[v] = min(minLv[v], lv[w]);
if (u != w && v != w) {
adj2[u].emplace_back(v, 1);
adj2[v].emplace_back(u, 1);
}
}
dfsCalc(1, 0);
int res = 1;
for (int i = 2; i <= n; i++) {
if (!used[i]) {
res = mul(res, 2);
if (!dfsRes(i, 0)) {
res = 0;
}
}
}
cout << res << endl;
return 0;
}
Compilation message (stderr)
usmjeri.cpp: In function 'int main()':
usmjeri.cpp:84:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
^
usmjeri.cpp:87:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
^
usmjeri.cpp:94:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
^
# | 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... |