This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]) {
| ^
# | 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... |