#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define BIT(x, i) (((x) >> (i)) & 1)
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 61;
long long binpow(long long n, long long k, long long mod = MOD){ // n^k % mod
long long res = 1;
while (k > 0){
if (k & 1) res = res * n % mod;
n = n * n % mod;
k >>= 1;
}
return res;
}
vector<int> operator+ (vector<int> vx, const vector<int>& vy){
for (int y : vy) vx.push_back(y);
return vx;
}
int d[N][N];
vector<int> p[N][N];
void solve(){
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++){
fill(d[i] + 1, d[i] + n + 1, 1e9);
d[i][i] = 0;
}
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
d[u][v] = d[v][u] = 1;
p[u][v] = p[v][u] = {i};
}
for (int t = 1; t <= n; t++){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (d[i][j] > d[i][t] + d[t][j]){
d[i][j] = d[i][t] + d[t][j];
p[i][j] = p[i][t] + p[t][j];
}
}
}
}
vector<pair<int, int>> pairs(m);
for (int i = 0; i < m; i++) cin >> pairs[i].first >> pairs[i].second;
ll ans = binpow(k, n - 1);
for (int mask = 1; mask < (1 << m); mask++){
set<int> s;
for (int i = 0; i < m; i++){
if (!BIT(mask, i)) continue;
for (int j : p[pairs[i].first][pairs[i].second]){
s.insert(j);
}
}
ll cur = k * binpow(k, n - 1 - (int) s.size()) % MOD;
if (bpc(mask) & 1){
ans += MOD - cur;
} else {
ans += cur;
}
if (ans >= MOD) ans -= MOD;
}
cout << ans << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:95:13: warning: unused variable 'startTime' [-Wunused-variable]
95 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |