#include <bits/stdc++.h>
using namespace std;
const int nx=3e5+5, kx=19, mod=1e9+7;
long long 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) continue;
dfs2(v, u);
dp[u]=min(dp[u], dp[v]);
if (dp[v]<lvl[u]) t[u].push_back({v, 0}), t[v].push_back({u, 0});
}
}
void dfs3(int u)
{
if (f) return;
vs[u]=1;
for (auto [v, s]:t[u])
{
if (!vs[v]) c[v]=(s==0)?c[u]:(1-c[u]), dfs3(v);
else if ((s==0&&c[v]!=c[u])||(s==1&&c[v]==c[u])) f=1;
}
}
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<=n; i++) dp[i]=INT_MAX;
for (int i=1; i<=m; i++)
{
cin>>a>>b;
int l=lca(a, b);
dp[a]=min(dp[a], lvl[l]);
dp[b]=min(dp[b], lvl[l]);
if (l!=a&&l!=b) t[a].push_back({b, 1}), t[b].push_back({a, 1});
}
for (int i=1; i<=n; i++) c[i]=-1;
dfs2(1, 1);
for (int i=2; i<=n; i++)
{
if (vs[i]) continue;
res=(res*2)%mod;
c[i]=0;
dfs3(i);
if (f) return cout<<0, 0;
}
cout<<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
56592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
118768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19036 KB |
Output is correct |
2 |
Correct |
7 ms |
19112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
6 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19548 KB |
Output is correct |
2 |
Correct |
7 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19548 KB |
Output is correct |
2 |
Correct |
6 ms |
19216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
89380 KB |
Output is correct |
2 |
Correct |
360 ms |
91092 KB |
Output is correct |
3 |
Correct |
221 ms |
67124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
93336 KB |
Output is correct |
2 |
Correct |
352 ms |
93188 KB |
Output is correct |
3 |
Correct |
257 ms |
70696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
94104 KB |
Output is correct |
2 |
Correct |
336 ms |
90136 KB |
Output is correct |
3 |
Correct |
237 ms |
70968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
380 ms |
94608 KB |
Output is correct |
2 |
Correct |
346 ms |
95060 KB |
Output is correct |
3 |
Correct |
213 ms |
67668 KB |
Output is correct |