#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
40940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
87052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15700 KB |
Output is correct |
2 |
Correct |
11 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15704 KB |
Output is correct |
2 |
Correct |
10 ms |
15836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16400 KB |
Output is correct |
2 |
Correct |
11 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16340 KB |
Output is correct |
2 |
Correct |
11 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
62084 KB |
Output is correct |
2 |
Correct |
400 ms |
63964 KB |
Output is correct |
3 |
Correct |
237 ms |
46724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
445 ms |
66080 KB |
Output is correct |
2 |
Correct |
440 ms |
66108 KB |
Output is correct |
3 |
Correct |
264 ms |
50444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
66764 KB |
Output is correct |
2 |
Correct |
378 ms |
63052 KB |
Output is correct |
3 |
Correct |
338 ms |
50492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
67272 KB |
Output is correct |
2 |
Correct |
415 ms |
67704 KB |
Output is correct |
3 |
Correct |
276 ms |
47172 KB |
Output is correct |