#include <bits/stdc++.h>
using namespace std;
/*
this problem can be solved using inclusion exclusion
but the implementation is a little tricky
it's annoying that we need to color edges in this problem
how do we handle this?
number the edges, make an adjacency list containing (dest, edge index)
for each path, get its set of edges using dfs
when processing a subset, make a dsu on sets of edges
add/subtract k^sets
*/
constexpr int MOD = 1e9 + 7;
constexpr int N = 60, M = 15;
pair<int, int> edges[N];
vector<pair<int, int>> adj[N];
pair<int, int> paths[M];
pair<int, int> from[N];
void dfs(int u, int p) {
for (auto [v, idx] : adj[u]) {
if (v != p) {
from[v] = {u, idx};
dfs(v, u);
}
}
}
int dsu[N];
int comps = 0;
int find(int x) {
return dsu[x] < 0 ? x : dsu[x] = find(dsu[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (-dsu[x] < -dsu[y]) {
swap(x, y);
}
dsu[x] += dsu[y];
dsu[y] = x;
comps--;
}
int pow_k[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n - 1; i++) {
auto &[u, v] = edges[i];
cin >> u >> v;
u--; v--;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
for (int i = 0; i < m; i++) {
auto &[a, b] = paths[i];
cin >> a >> b;
a--; b--;
}
pow_k[0] = 1;
for (int i = 1; i < n; i++) {
pow_k[i] = 1ll * pow_k[i - 1] * k % MOD;
}
long long ans = 0;
for (int i = 0; i < (1 << m); i++) {
fill(dsu, dsu + n - 1, -1);
comps = n - 1;
for (int j = 0; j < m; j++) {
if ((i >> j) & 1) {
auto [a, b] = paths[j];
from[a].first = a;
dfs(a, -1);
for (int x = b; from[x].first != a; x = from[x].first) {
unite(from[x].second, from[from[x].first].second);
}
}
}
if (__builtin_popcount(i) & 1) {
ans = (ans - pow_k[comps] + MOD) % MOD;
} else {
ans = (ans + pow_k[comps]) % MOD;
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
456 KB |
Output is correct |
6 |
Correct |
5 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
456 KB |
Output is correct |
25 |
Correct |
5 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
27 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
30 ms |
468 KB |
Output is correct |
31 |
Correct |
7 ms |
348 KB |
Output is correct |
32 |
Correct |
3 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
3 ms |
348 KB |
Output is correct |
35 |
Correct |
15 ms |
456 KB |
Output is correct |
36 |
Correct |
41 ms |
348 KB |
Output is correct |
37 |
Correct |
18 ms |
348 KB |
Output is correct |