#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;
if (x>=mod) 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) add(ans,mod-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
Main.cpp: In function 'int main()':
Main.cpp:157:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
157 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
33104 KB |
Output is correct |
2 |
Correct |
5 ms |
33104 KB |
Output is correct |
3 |
Correct |
5 ms |
33104 KB |
Output is correct |
4 |
Correct |
5 ms |
33104 KB |
Output is correct |
5 |
Correct |
5 ms |
33104 KB |
Output is correct |
6 |
Correct |
5 ms |
33104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
33272 KB |
Output is correct |
2 |
Correct |
5 ms |
33104 KB |
Output is correct |
3 |
Correct |
6 ms |
33104 KB |
Output is correct |
4 |
Correct |
5 ms |
33304 KB |
Output is correct |
5 |
Correct |
5 ms |
33104 KB |
Output is correct |
6 |
Correct |
5 ms |
33104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
33104 KB |
Output is correct |
2 |
Correct |
5 ms |
33104 KB |
Output is correct |
3 |
Correct |
5 ms |
33104 KB |
Output is correct |
4 |
Correct |
5 ms |
33104 KB |
Output is correct |
5 |
Correct |
7 ms |
33104 KB |
Output is correct |
6 |
Correct |
5 ms |
33104 KB |
Output is correct |
7 |
Correct |
6 ms |
33104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
33104 KB |
Output is correct |
2 |
Correct |
5 ms |
33104 KB |
Output is correct |
3 |
Correct |
5 ms |
33232 KB |
Output is correct |
4 |
Correct |
5 ms |
33104 KB |
Output is correct |
5 |
Correct |
7 ms |
33104 KB |
Output is correct |
6 |
Correct |
7 ms |
33104 KB |
Output is correct |
7 |
Correct |
5 ms |
33304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
33104 KB |
Output is correct |
2 |
Correct |
5 ms |
33104 KB |
Output is correct |
3 |
Correct |
5 ms |
33104 KB |
Output is correct |
4 |
Correct |
5 ms |
33104 KB |
Output is correct |
5 |
Correct |
5 ms |
33104 KB |
Output is correct |
6 |
Correct |
5 ms |
33104 KB |
Output is correct |
7 |
Correct |
5 ms |
33272 KB |
Output is correct |
8 |
Correct |
5 ms |
33104 KB |
Output is correct |
9 |
Correct |
6 ms |
33104 KB |
Output is correct |
10 |
Correct |
5 ms |
33304 KB |
Output is correct |
11 |
Correct |
5 ms |
33104 KB |
Output is correct |
12 |
Correct |
5 ms |
33104 KB |
Output is correct |
13 |
Correct |
6 ms |
33104 KB |
Output is correct |
14 |
Correct |
5 ms |
33104 KB |
Output is correct |
15 |
Correct |
5 ms |
33104 KB |
Output is correct |
16 |
Correct |
5 ms |
33104 KB |
Output is correct |
17 |
Correct |
7 ms |
33104 KB |
Output is correct |
18 |
Correct |
5 ms |
33104 KB |
Output is correct |
19 |
Correct |
6 ms |
33104 KB |
Output is correct |
20 |
Correct |
5 ms |
33104 KB |
Output is correct |
21 |
Correct |
5 ms |
33104 KB |
Output is correct |
22 |
Correct |
5 ms |
33232 KB |
Output is correct |
23 |
Correct |
5 ms |
33104 KB |
Output is correct |
24 |
Correct |
7 ms |
33104 KB |
Output is correct |
25 |
Correct |
7 ms |
33104 KB |
Output is correct |
26 |
Correct |
5 ms |
33304 KB |
Output is correct |
27 |
Correct |
9 ms |
33104 KB |
Output is correct |
28 |
Correct |
6 ms |
33104 KB |
Output is correct |
29 |
Correct |
5 ms |
33104 KB |
Output is correct |
30 |
Correct |
9 ms |
33104 KB |
Output is correct |
31 |
Correct |
6 ms |
33272 KB |
Output is correct |
32 |
Correct |
5 ms |
33104 KB |
Output is correct |
33 |
Correct |
5 ms |
33104 KB |
Output is correct |
34 |
Correct |
5 ms |
33104 KB |
Output is correct |
35 |
Correct |
6 ms |
33104 KB |
Output is correct |
36 |
Correct |
11 ms |
33104 KB |
Output is correct |
37 |
Correct |
11 ms |
33104 KB |
Output is correct |