#include <bits/stdc++.h>
using namespace std;
const int nx=3e5+5, kx=19, mod=1e9+7;
int n, m, u, v, lvl[nx], pa[nx][kx], a, b, dp[nx], vs[nx], c[nx], cnt, f, res=1;
vector<int> d[nx];
vector<pair<int, int>> t[nx];
void dfs(int u, int p)
{
lvl[u]=lvl[p]+1;
pa[u][0]=p;
for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
for (auto v:d[u]) if (v!=p) dfs(v, u);
}
int lca(int u, int v)
{
if (lvl[u]>lvl[v]) swap(u, v);
for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
if (u==v) return u;
for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
return pa[u][0];
}
void dfs2(int u, int p)
{
for (auto v:d[u]) if (v!=p) dfs2(v, u), dp[u]+=dp[v];
if (dp[u]!=0)
{
for (auto v:d[u]) if (v!=p&&dp[v]!=0) t[v].push_back({u, 0}), t[u].push_back({v, 0}); //cout<<"edge "<<u<<' '<<v<<'\n';
}
}
void dfs3(int u)
{
//cout<<"node "<<u<<' '<<c[u]<<'\n';
vs[u]=1;
for (auto [v, s]:t[u])
{
if (vs[v]&&s==0&&c[v]!=c[u]) f=1;
if (vs[v]&&s==1&&c[v]==c[u]) f=1;
if (vs[v]) continue;
if (s==0) c[v]=c[u], dfs3(v);
if (s==1) c[v]=(1-c[u]), dfs3(v);
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
dfs(1, 1);
for (int i=1; i<=m; i++)
{
cin>>a>>b;
int l=lca(a, b);
dp[a]++;
dp[b]++;
dp[l]-=2;
//cout<<"lca "<<a<<' '<<b<<' '<<l<<'\n';
if (l!=a&&l!=b) t[a].push_back({b, 1}), t[b].push_back({a, 1});
}
dfs2(1, 1);
for (int i=2; i<=n; i++)
{
if (vs[i]) continue;
//cout<<"root "<<i<<'\n';
cnt++;
c[i]=0;
dfs3(i);
if (f) return cout<<0, 0;
}
return cout<<0, 0;
for (int i=1; i<=cnt; i++) res=(res*2)%mod;
cout<<res;
}
/*
5 2
1 2
2 3
3 4
4 5
3 4
2 5
5 2
1 2
2 3
1 4
4 5
3 4
1 5
4 3
2 1
2 3
2 4
1 3
1 4
3 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
93 ms |
44364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
192 ms |
89380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
19032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
19036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
19292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
19292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
347 ms |
54756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
305 ms |
59016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
287 ms |
59404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
324 ms |
59996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |