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;
}
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);
}
}
vector<int>bi[mxn+10];
void checkbi(int cur,int col){
if(val[cur]!=0&&val[cur]!=col)return void(ans=0);
val[cur]=col;
for(auto &i:bi[cur]){
i=find(i);
if(val[i]!=0&&val[i]!=((col==1)?-1:1))ans=0;
else if(val[i]==0)checkbi(i,((col==1)?-1:1));
}
}
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);
}
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);
if(i.f==node||i.s==node)continue;
bi[i.f].pb(i.s);
bi[i.s].pb(i.f);
}
for(int i=2;i<=n;i++)if(!val[find(i)])checkbi(i,1),ans=(ans*2)%mod;
cout<<ans<<'\n';
}
# | 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... |