#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=100;
const int M=16;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int dp[(1<<M)+10],fau[N],dep[N],val[N],oj[N];
ll pot[N];
vector<pair<int,int> >graf[N];
bitset<N>vis;
pair<int,int> w[N];
int Find(int a){
if(fau[a]==a) return a;
return fau[a]=Find(fau[a]);
}
void Union(int a,int b){
fau[Find(a)]=Find(fau[b]);
}
void dfs(int v,int o){
for(auto [u,id]:graf[v]){
if(u==o) continue;
oj[u]=v,val[u]=id;
dep[u]=dep[v]+1;
dfs(u,v);
}
}
void solve(){
int n,m;
ll k;
cin>>n>>m>>k;
pot[0]=1;
for(int i=1;i<=n;i++) pot[i]=(pot[i-1]*k)%mod;
for(int a,b,i=1;i<n;i++){
cin>>a>>b;
graf[a].push_back({b,i}),graf[b].push_back({a,i});
}
dfs(1,1);
for(int i=1;i<=m;i++) cin>>w[i].fi>>w[i].se;
ll res=pot[n-1];
for(int i=1;i<(1<<m);i++){
for(int i=1;i<n;i++) fau[i]=i;
for(int j=0;j<m;j++){
if((1<<j)&i){
int a=w[j+1].fi,b=w[j+1].se,pop=0;
while(a!=b){
if(dep[a]<dep[b]) swap(a,b);
if(pop!=0) Union(pop,val[a]);
else pop=val[a];
a=oj[a];
}
}
}
vis.reset();
int curr=0;
for(int j=1;j<n;j++){
if(!vis[Find(j)]){
curr++;
vis[Find(j)]=1;
}
}
ll akt=pot[curr]*(__builtin_popcount(i)&1?-1:1);
(res+=akt+mod)%=mod;
}
cout<<res<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message
Main.cpp: In function 'void dfs(int, int)':
Main.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [u,id]:graf[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
260 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
260 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
11 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
11 ms |
348 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
6 ms |
340 KB |
Output is correct |
36 |
Correct |
27 ms |
212 KB |
Output is correct |
37 |
Correct |
13 ms |
332 KB |
Output is correct |