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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,ll>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
using namespace std;
#define int long long
#define double long double
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
const int mod=1e9+7,mxn=3e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
int n,m;
vector<int>adj[mxn+10];
int up[mxn+10][lg+5],h[mxn+10],pa[mxn+10],val[mxn+10],sz[mxn+10];
int ans=1;
int find(int u){return ((pa[u]==u)?u:pa[u]=find(pa[u]));}
void get(int cur,int p){for(auto i:adj[cur])if(i!=p)h[i]=h[cur]+1,up[i][0]=cur,get(i,cur);}
void merg(int a,int b){
a=find(a),b=find(b);
if(a==b)return;
if(h[a]>h[b])swap(a,b);
pa[b]=a;
sz[a]+=sz[b];
if(val[a]==0)swap(a,b);
if(val[b]!=0&&val[b]!=val[a])ans=0;
else val[a]=val[b];
}
int lca(int a,int b){
if(h[a]<h[b])swap(a,b);
int k=h[a]-h[b];
for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
if(a==b)return a;
for(int i=lg;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
return up[a][0];
}
void goup(int cur,int node){
while(h[cur]>h[node]+1){
merg(cur,up[cur][0]);
cur=find(cur);
}
}
int p[mxn+10];
bool yes[mxn+10];
int32_t main(){
fastio
cin>>n>>m;
vector<pii>check(m);
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
p[0]=1;
for(int i=1;i<=n;i++)p[i]=(p[i-1]*2)%mod;
get(1,-1);
for(int i=1;i<=n;i++)pa[i]=i,sz[i]=1;
for(int i=0;i<m;i++)cin>>check[i].f>>check[i].s;
for(int j=0;j<=lg;j++)up[1][j]=1;
for(int j=1;j<=lg;j++)for(int i=2;i<=n;i++)up[i][j]=up[up[i][j-1]][j-1];
for(auto i:check){
int node=lca(i.f,i.s);
if(i.f!=node)goup(i.f,node);
if(i.s!=node)goup(i.s,node);
int a=val[find(i.f)],b=val[find(i.s)];
if(i.f==node)i.f=0;
if(i.s==node)i.s=0;
if(a==0)swap(a,b),swap(i.s,i.f);
if(a==0){
val[find(i.f)]=1;
val[find(i.s)]=-1;
}
else if(a==b)ans=0;
else if(a==1)val[find(i.s)]=-1;
else val[find(i.s)]=1;
if(ans==0)return cout<<ans,0;
}
int cnt=0;
for(int i=1;i<=n;i++){
if(!yes[find(i)]&&val[find(i)])ans=(ans*2)%mod,cnt+=sz[find(i)],yes[find(i)]=true;
}
cout<<(ans*p[n-1-cnt])%mod;
}
# | 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... |