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;
using ll = long long;
const int MXN = 300 * 1000 + 3;
const int LGN = 19, mod = 1e9 + 7;
vector<int> g[MXN];
vector<array<int, 2>> adj[MXN];
int dt[MXN], cmin[MXN], sub[MXN], up[MXN][LGN], col[MXN];
int n, m;
void init(int v = 1, int p = 0) {
for(int i = 1; i < LGN; i++)
up[v][i] = up[up[v][i - 1]][i - 1];
for(int u : g[v]) if(u != p) {
up[u][0] = v;
dt[u] = dt[v] + 1;
cmin[u] = dt[u];
init(u, v);
}
}
int kth(int v, int k) {
for(int i = 0; i < LGN; i++)
if(k >> i & 1)
v = up[v][i];
return v;
}
int LCA(int u, int v) {
if(dt[u] > dt[v])
swap(u, v);
v = kth(v, dt[v] - dt[u]);
if(u == v)
return v;
for(int i = LGN - 1; i >= 0; i--)
if(up[u][i] != up[v][i])
u = up[u][i], v = up[v][i];
return up[u][0];
}
int connect(int v = 1, int p = 1) {
sub[v] = cmin[v];
for(int u : g[v]) if(u != p) {
sub[v] = min(sub[v], connect(u, v));
if(sub[u] < dt[v]) {
adj[u].push_back({v, 0});
adj[v].push_back({u, 0});
}
}
return sub[v];
}
bool dfs(int v, int c) {
col[v] = c;
for(auto &[u, i] : adj[v]) {
if(col[u] != -1 && col[u] != (c ^ i))
return false;
else if(col[u] == -1)
if(!dfs(u, c ^ i))
return false;
}
return true;
}
ll fexpo(ll a, int b) {
ll res = 1;
while(b) {
if(b & 1)
(res *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return res;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
cin >> n >> m;
memset(col, -1, sizeof col);
for(int u, v, i = 1; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
init();
for(int u, v, i = 0; i < m; i++) {
cin >> u >> v;
int lc = LCA(u, v);
//cout << u << ' ' << v << ": " << lc << endl;
for(int x : {u, v})
cmin[x] = min(cmin[x], dt[lc]);
if(u != lc && v != lc) {
adj[u].push_back({v, 1});
adj[v].push_back({u, 1});
}
}
connect();
int cnt = 0;
for(int i = 2; i <= n; i++) if(col[i] < 0) {
if(!dfs(i, 0))
return cout << 0 << '\n', 0;
cnt++;
}
cout << fexpo(2LL, cnt) << '\n';
return 0;
}
# | 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... |