#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 500
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll mod=1000000007;//998244353;
const ll inf=1e18*4;
const ld pai=acos(-1);
ll n,m;
ll hight[300009],lev[300009],dp[300009][29],done[300009];
vll v[300009];
vpll g[300009];
void dfs1(ll node,ll p){
dp[node][0]=p;
lev[node]=hight[node]=lev[p]+1;
for(auto u:v[node]){
if(u==p)C;
dfs1(u,node);
}
}
void fill(){
for(ll j=1;j<20;j++){
for(ll i=0;i<n;i++){
dp[i][j]=dp[dp[i][j-1]][j-1];
}
}
}
ll lca(ll a,ll b){
if(lev[a]<lev[b])swap(a,b);
ll l=lev[a]-lev[b];
for(ll i=0;i<20;i++){
if((l&(1<<i)))a=dp[a][i];
}
if(a==b)return a;
for(ll i=19;i>=0;i--){
if(dp[a][i]!=dp[b][i]){
a=dp[a][i];
b=dp[b][i];
}
}
return dp[a][0];
}
void add(ll a,ll b,ll c){
g[a].pb({b,c});
g[b].pb({a,c});
}
void connect(ll node,ll p){
for(auto u:v[node]){
if(u==p)C;
connect(u,node);
hight[node]=min(hight[node],hight[u]);
if(hight[u]<lev[node])add(u,node,0);
}
}
ll check(ll node,ll c){
if(done[node]!=-1)return (done[node]==c);
done[node]=c;
for(auto u:g[node]){
if(!check(u.fi,(c^u.se)))return 0;
}
return 1;
}
int main(){
mem(done,-1);
cin>>n>>m;
for(int i=0;i<n-1;i++){
ll a,b;
cin>>a>>b;
a--,b--;
v[a].pb(b);
v[b].pb(a);
}
dfs1(0,0);
fill();
while(m--){
ll a,b,c;
cin>>a>>b;
a--,b--;
c=lca(a,b);
hight[a]=min(lev[c],hight[a]);
hight[b]=min(lev[c],hight[b]);
if(c!=a && c!=b)add(a,b,1);
}
connect(0,0);
ll ans=1;
for(int i=1;i<n;i++){
if(done[i]!=-1)C;
if(!check(i,0)){
cout<<0<<endl,exit(0);
}
ans*=2;
ans%=mod;
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
50432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
925 ms |
114596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
17144 KB |
Output is correct |
2 |
Correct |
20 ms |
17400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
17152 KB |
Output is correct |
2 |
Correct |
20 ms |
17444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
18408 KB |
Output is correct |
2 |
Correct |
25 ms |
18296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
18424 KB |
Output is correct |
2 |
Correct |
25 ms |
18296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1059 ms |
113528 KB |
Output is correct |
2 |
Correct |
1124 ms |
116600 KB |
Output is correct |
3 |
Correct |
600 ms |
82812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1196 ms |
114300 KB |
Output is correct |
2 |
Correct |
1150 ms |
121952 KB |
Output is correct |
3 |
Correct |
782 ms |
88952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1147 ms |
115280 KB |
Output is correct |
2 |
Correct |
1077 ms |
114680 KB |
Output is correct |
3 |
Correct |
795 ms |
89404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1175 ms |
115392 KB |
Output is correct |
2 |
Correct |
1108 ms |
123864 KB |
Output is correct |
3 |
Correct |
616 ms |
82424 KB |
Output is correct |