#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int kN = 60;
const int kM = 15;
vector<pair<int, int>> g[1 + kN];
map<pair<int, int>, int> indexOfPath;
int64_t edges[kM];
vector<int> G[kM];
bitset<kM> vis;
void addSelf(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
void multSelf(int &x, const int &y) {
x = (int64_t)x * y % mod;
}
int Pow(int x, int n) {
int ans = 1;
while (n) {
if (n & 1) {
multSelf(ans, x);
}
multSelf(x, x);
n >>= 1;
}
return ans;
}
void dfs1(int root, int u, int par, int64_t mask) {
if (indexOfPath.find({root, u}) != indexOfPath.end()) {
edges[indexOfPath[{root, u}]] = mask;
}
for (auto it : g[u]) {
int v, e;
tie(v, e) = it;
if (v != par) {
dfs1(root, v, u, mask | (1LL << e));
}
}
}
void dfs2(int u, int mask) {
vis[u] = true;
for (int v : G[u]) {
if (!vis[v] && (mask & (1 << v))) {
dfs2(v, mask);
}
}
}
void testCase() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
vector<pair<int, int>> paths;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
if (v < u) {
swap(u, v);
}
if (indexOfPath.find({u, v}) != indexOfPath.end()) {
i -= 1;
m -= 1;
continue;
}
paths.emplace_back(u, v);
indexOfPath[{u, v}] = i;
}
for (int v = 1; v <= n; ++v) {
dfs1(v, v, 0, 0);
}
for (int i = 0; i < m - 1; ++i) {
for (int j = i + 1; j < m; ++j) {
if (edges[i] & edges[j]) {
G[i].emplace_back(j);
G[j].emplace_back(i);
}
}
}
int ans = 0;
for (int mask = 0; mask < (1 << m); ++mask) {
int64_t allEdges = 0;
int cnt = 0;
vis.reset();
for (int i = 0; (1 << i) <= mask; ++i) {
if (mask & (1 << i)) {
allEdges |= edges[i];
if (!vis[i]) {
cnt += 1;
dfs2(i, mask);
}
}
}
cnt += n - 1 - __builtin_popcountll(allEdges);
if (__builtin_popcount(mask) % 2 == 0) {
addSelf(ans, Pow(k, cnt));
} else {
addSelf(ans, mod - Pow(k, cnt));
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
324 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 |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
7 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
7 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
4 ms |
324 KB |
Output is correct |
36 |
Correct |
17 ms |
332 KB |
Output is correct |
37 |
Correct |
8 ms |
212 KB |
Output is correct |