#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
constexpr int MOD = 1e9 + 7;
int mult(int a,int b){
if(a*b>=MOD) return (a*b)%MOD;
return a*b;
}
int eks(int a,int b){
if(a<b) return a-b+MOD;
return a-b;
}
int add(int a,int b){
if(a+b>=MOD) return a+b-MOD;
return a+b;
}
struct DSU{
int comp,comp2;
vector<int> par;
vector<int> siz;
vector<int> vis;
DSU(int n){
comp=n;
comp2=0;
par.resize(n+5);
vis.assign(n+5,0);
siz.assign(n+5,1);
iota(all(par),0);
}
int find(int a){
if(par[a]==a) return a;
return par[a]=find(par[a]);
}
void unite(int a,int b){
if(!vis[a]){
vis[a]=1;
comp2++;
}
if(!vis[b]){
vis[b]=1;
comp2++;
}
a=find(a),b=find(b);
if(a==b) return;
comp--;
comp2--;
if(siz[a]>siz[b]) swap(a,b);
siz[b]+=siz[a];
par[a]=b;
}
};
vector<int> v[65];
int par[65];
int depth[65];
void dfs(int c,int p,int d){
par[c]=p;
depth[c]=d;
for(int x:v[c]){
if(x==p) continue;
dfs(x,c,d+1);
}
}
void solve(){
int n,m,k;
cin >> n >> m >> k;
int ans=0;
int pre[n+5];
pre[0]=1;
for(int i=1;i<=n;i++) pre[i]=mult(pre[i-1],k);
array<int,2> ar[m+5];
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
dfs(1,1,0);
for(int i=0;i<m;i++){
int a,b;
cin >> a >> b;
ar[i]={a,b};
}
for(int mask=0;mask<(1LL<<m);mask++){
DSU dsu(n);
for(int j=0;j<m;j++){
if(mask>>j&1){
int a = ar[j][0];
int b = ar[j][1];
if(depth[a]<depth[b]) swap(a,b);
while(depth[a]>depth[b]){
dsu.unite(a,par[a]);
a=par[a];
}
while(a!=b){
dsu.unite(a,par[a]);
a=par[a];
dsu.unite(b,par[b]);
b=par[b];
}
}
}
int cur=1;
cur=mult(cur,pre[dsu.comp2]);
cur=mult(cur,pre[dsu.comp-1]);
if(__builtin_popcountll(mask)&1) ans-=cur;
else ans+=cur;
}
cout << ans << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |