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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3wByxeT ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
//snuke's mod int
template <ll mod>
struct modint{
ll x; // typedef long long ll;
modint(ll x=0):x((x%mod+mod)%mod){}
modint operator-()const{return modint(-x);}
modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
modint operator+(const modint a)const{modint res(*this); return res+=a;}
modint operator-(const modint a)const{modint res(*this); return res-=a;}
modint operator*(const modint a)const{modint res(*this); return res*=a;}
modint pow(ll n)const{
modint res=1,x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modint inv()const{return pow(mod-2);}
};
using mint=modint<1000'000'007>;
signed main(){
_3wByxeT;
int n,m,k;
cin>>n>>m>>k;
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u-=1,v-=1;
adj[u].pb(v);
adj[v].pb(u);
}
using vp=vec(pii);
vp es;
rep(i,m){
int u,v;
cin>>u>>v;
u-=1,v-=1;
es.pb({u,v});
}
mint ans=0;
rng(msk,1,(1<<m)){
std::map<pii,int> mp,admp;
auto dfs=[&](auto self,int v,int par,int nd)->bool{
if(v==nd) return 1;
bool sok=0;
for(auto u:adj[v]){
if(u==par) continue;
bool pok=self(self,u,v,nd);
if(pok){
admp[minmax(u,v)]=1;
mp[minmax(u,v)]=1;
}
sok=sok or pok;
}
return sok;
};
vec(std::map<pii,int>) rbts;
auto add=[&](int u,int v){
mp.clear();
dfs(dfs,v,-1,u);
rbts.pb(mp);
};
rep(i,m){
if(msk>>i&1){
auto [u,v]=es[i];
add(u,v);
}
}
mint now=1;
{
int nen=sz(rbts);
vec(vi) g(nen);
rep(i,nen){
rng(j,i+1,nen){
bool pok=0;
for(auto p:rbts[i]){
if(rbts[j].find(p.fi)!=rbts[j].end()){
pok=1;
break;
}
}
if(pok){
g[i].pb(j);
g[j].pb(i);
}
}
}
vi usd(nen,0);
auto rfs=[&](auto self,int v)->void{
for(auto u:g[v]){
if(!usd[u]){
usd[u]=1;
self(self,u);
}
}
};
rep(i,nen){
if(usd[i]) continue;
now*=mint(k);
rfs(rfs,i);
}
}
if(__builtin_popcount(msk)%2){
ans-=now*mint(k).pow(n-1-sz(admp));
}else{
ans+=now*mint(k).pow(n-1-sz(admp));
}
}
ans+=mint(k).pow(n-1);
print(ans.x);
//
return 0;
}
# | 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... |