#include <bits/stdc++.h>
using namespace std;
const int N = 60;
const int M = 15;
const int MOD = 1e9 + 7;
struct DSU {
int root[N];
void init(int n) {
for (int i = 0; i < n; ++i) {
root[i] = -1;
}
}
int findRoot(int u) {
return (root[u] < 0? u : (root[u] = findRoot(root[u])));
}
bool unite(int u, int v) {
u = findRoot(u);
v = findRoot(v);
if (u == v) {
return false;
}
if (root[u] > root[v]) {
swap(u, v);
}
root[u] += root[v];
root[v] = u;
return true;
}
} dsu;
int n, m, k, h[N];
array<int, 2> par[N];
vector<array<int, 2>> adj[N];
vector<int> edges[M];
void Dfs(int u, int p) {
for (auto &[v, id] : adj[u]) {
if (v != p) {
h[v] = h[u] + 1;
par[v] = {u, id};
Dfs(v, u);
}
}
}
int Pow(int n, int k) {
if (k == 0) {
return 1;
}
int res = Pow(n, k >> 1);
res = 1ll * res * res % MOD;
if (k & 1) {
res = 1ll * res * n % MOD;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
Dfs(0, -1);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
while (u != v) {
if (h[u] > h[v]) {
swap(u, v);
}
edges[i].push_back(par[v][1]);
v = par[v][0];
}
}
int res = 0;
for (int mask = 0; mask < (1 << m); ++mask) {
int cnt = n - 1;
dsu.init(n - 1);
for (int i = 0; i < m; ++i) {
if ((mask >> i) & 1) {
for (int j = 1; j < (int) edges[i].size(); ++j) {
cnt -= dsu.unite(edges[i][j - 1], edges[i][j]);
}
}
}
if (__builtin_popcount(mask) % 2) {
res = (res - Pow(k, cnt) + MOD) % MOD;
} else {
res = (res + Pow(k, cnt)) % MOD;
}
}
cout << res << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'void Dfs(int, int)':
Main.cpp:39:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for (auto &[v, id] : adj[u]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
328 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
0 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
328 KB |
Output is correct |
24 |
Correct |
2 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
6 ms |
448 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
6 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
328 KB |
Output is correct |
33 |
Correct |
1 ms |
328 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
4 ms |
212 KB |
Output is correct |
36 |
Correct |
13 ms |
336 KB |
Output is correct |
37 |
Correct |
6 ms |
336 KB |
Output is correct |