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 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 |
---|
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... |