#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);
ll ans = 1;
int e = n - 1;
for (int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
pairs[i] = MP(x, y);
int sz = p[x][y].size();
e -= sz;
ll cur = binpow(k, sz) + MOD - k;
ans = ans * cur % MOD;
}
ans *= binpow(k, e);
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:90:13: warning: unused variable 'startTime' [-Wunused-variable]
90 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 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 |
1 ms |
416 KB |
Output is correct |
2 |
Incorrect |
1 ms |
428 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 |
468 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 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 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 |
1 ms |
416 KB |
Output is correct |
8 |
Incorrect |
1 ms |
428 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |