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>
//code
#define fl(i,x,y,z) for(int i=x;i<=y;i=i+z)
#define fn(i,x,y,z) for(int i=x;i>=y;i=i-z)
#define rep(i,x,y) for(int i=x;i<y;i=i+1)
#define all(v) v.begin(),v.end()
#define pb emplace_back
#define tle cout<<"tle"<<endl
#define edl cout<<"\n"
#define el "\n"
#define getbit(x,i) ((x>>i)&1)
#define bitcnt __builtin_popcountll
//ham
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define ld long double
#define inf 0x3f3f3f3f
#define int long long
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int maxn=1e6+5;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
int n,m,k;
int lab[maxn];
int high[maxn],lowest[maxn][9],g[maxn][9],par[maxn];
vector <int> e[maxn];
int pw[maxn];
int ans=0;
void DFS(int u,int par)
{
for (int v: e[u])
{
if (v==par) continue;
high[v]=high[u]+1;
lowest[v][0]=u;
g[v][0]=(1ll<<v);
fl(j,1,7,1)
{
lowest[v][j]=lowest[lowest[v][j-1]][j-1];
g[v][j]=(g[v][j-1] | g[lowest[v][j-1]][j-1]);
}
DFS(v,u);
}
}
ll lca(int u,int v)
{
ll ans=0;
if (high[u]<high[v]) swap(u,v);
int ebsing=high[u]-high[v];
fl(i,0,7,1)
if (getbit(ebsing,i))
{
ans|= g[u][i];
u=lowest[u][i];
}
if (u==v) return ans;
fn(i,7,0,1)
if (lowest[u][i]!=lowest[v][i])
{
ans |= g[u][i];
ans |= g[v][i];
u=lowest[u][i];
v=lowest[v][i];
}
ans|=g[u][0];
ans|=g[v][0];
return ans;
}
int findset(int u)
{
if (lab[u]<0) return u;
else return lab[u]=findset(lab[u]);
}
void joinset(int u,int v)
{
if (lab[u]>lab[v]) swap(u,v);
lab[u]+=lab[v];
lab[v]=u;
}
void inp()
{
cin>>n>>m>>k;
rep(i,1,n)
{
int u,v; cin>>u>>v;
e[u].pb(v);
e[v].pb(u);
}
DFS(1,0);
rep(i,0,m)
{
int u,v; cin>>u>>v;
par[i]=lca(u,v);
}
}
void sol()
{
pw[0]=1;
rep(i,1,n)
pw[i]=1ll*pw[i-1]*k%mod;
rep(mask,0,1<<m)
{
fl(i,0,m,1) lab[i]=-1;
vector <int> save;
ll t=0; int cnt=0;
rep(i,0,m) if (getbit(mask,i))
{
for (int j: save) if (par[i] & par[j])
{
int u=findset(i);
int v=findset(j);
if (u!=v) joinset(u,v);
}
save.pb(i);
t |= par[i];
}
for (int i: save) if (lab[i]<0) cnt++;
cnt= cnt+n-1-(bitcnt(t));
if (bitcnt(mask)&1) del(ans,pw[cnt]);
else add(ans,pw[cnt]);
// cout<<cnt<< " "<<t<< el;
}
cout<<ans;
}
signed main()
{
#define name "seaw"
if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();sol();
return 0;
}
/* /\_/\
( ._. )
/ >V< \
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
164 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |