#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++) {
int x, y;
cin >> x >> y;
pairs[i] = MP(x, y);
}
if (n <= 15 && k == 2){
int ans = 0;
for (int mask = 0; mask < (1 << (n - 1)); mask++){
vector<int> color(n, 1);
for (int i = 0; i < n - 1; i++){
if (mask & (1 << i)){
color[i + 1] = 2;
}
}
bool ok = true;
for (auto [x, y] : pairs){
int c1 = false, c2 = false;
for (int j : p[x][y]){
if (color[j] == 1) c1 = true;
if (color[j] == 2) c2 = true;
}
if (!c1 || !c2) ok = false;
}
if (ok) ans++;
}
cout << ans << endl;
return;
}
ll ans = 1;
int e = n - 1;
for (auto [x, y] : pairs){
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:120:13: warning: unused variable 'startTime' [-Wunused-variable]
120 | 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 |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 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 |
596 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 |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
340 KB |
Output is correct |
7 |
Correct |
4 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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |