#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef long double ld;
const int mod = 1e9+7;
const int maxn = 65;
const ll inf = 1e18+5;
int n, m, k;
int _s, _t;
vector<int> edg[maxn];
vector<int> alle[maxn];
int vis[65];
ll pw[65];
vector<int> tr[maxn];
vector<pair<int, int>> g[maxn];
bool dfs(int x, int p, int id){
//cout << _s << " " << x << "\n";
if(x == _t)return 1;
for(auto v: g[x]){
if(v.f == p)continue;
if(dfs(v.f, x, id)){
//cout << v.s << "\n";
alle[id].push_back(v.s);
return 1;
}
}
return 0;
}
void _fill(int x){
vis[x] = 1;
for(auto v: tr[x])if(!vis[v])_fill(v);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
//cout << (1LL*k*k - k)%mod << "\n";
pw[0] = 1;
for(int i = 1; i <= n; i++)pw[i] = pw[i-1]*k%mod;
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
g[a].push_back({b, i});
g[b].push_back({a, i});
}
for(int i = 0; i < m; i++){
cin >> _s >> _t;
dfs(_s, 0, i);
}
ll ans = 0;
for(int i = 0; i < (1<<m); i++){
for(int j = 1; j < n; j++){
vis[j] = 0;
tr[j].clear();
}
for(int j = 0; j < m; j++){
if(i&(1<<j)){
for(int k = 1; k < alle[j].size(); k++){
int a = alle[j][0], b = alle[j][k];
tr[a].push_back(b);
tr[b].push_back(a);
}
}
}
int cnt = 0;
for(int j = 1; j < n; j++){
if(!vis[j]){
cnt++;
_fill(j);
}
}
if(__builtin_popcount(i)&1)ans -= pw[cnt];
else ans += pw[cnt];
ans%=mod;
}
if(ans < 0)ans += mod;
cout << ans;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:71:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int k = 1; k < alle[j].size(); k++){
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
16 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
16 ms |
212 KB |
Output is correct |
31 |
Correct |
5 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
2 ms |
212 KB |
Output is correct |
35 |
Correct |
9 ms |
328 KB |
Output is correct |
36 |
Correct |
37 ms |
212 KB |
Output is correct |
37 |
Correct |
17 ms |
332 KB |
Output is correct |