This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
assert(x != y);
}
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 (stderr)
Main.cpp: In function 'int main()':
Main.cpp:100:13: warning: unused variable 'startTime' [-Wunused-variable]
100 | clock_t startTime = clock();
| ^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |