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;
int add (int a, int b) {
a += b; if (a >= MOD) a -= MOD;
return a;
}
int sub (int a, int b) {
a -= b; if (a < 0) a += MOD;
return a;
}
int mul (int a, int b) {
return (a * 1ll * b) % MOD;
}
int power (int a, int b) {
if (!b) return 1;
int u = power(a, b >> 1);
u = mul(u, u);
if (b & 1) u = mul(u, a);
return u;
}
int n, m, k;
vector <pair <int, int>> adj[61];
pair <int, int> val[15];
vector <int> edges[51];
vector <int> st;
bool flag = 0;
void dfs (int pos, int par, int dist) {
if (pos == dist) {
flag = 1;
return;
}
for (auto j : adj[pos]) {
if (j.first == par) continue;
st.push_back(j.second);
dfs(j.first, pos, dist);
if (flag) return;
st.pop_back();
}
}
struct DSU {
int par[61], sze[61];
void init () {
for (int i = 1; i <= n - 1; i++) {
sze[i] = 1; par[i] = i;
}
}
int find (int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void merge (int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (sze[x] > sze[y]) swap(x, y);
sze[y] += sze[x]; par[x] = y; sze[x] = 0;
}
} cur;
int solve (vector <int> x) {
cur.init();
for (int i = 0; i < (int)x.size(); i++) {
for (int j = 1; j < (int)edges[x[i]].size(); j++) {
cur.merge(edges[x[i]][j], edges[x[i]][j - 1]);
}
}
int ans = 1;
int l = 0;
for (int i = 1; i <= n - 1; i++) {
l += cur.sze[i];
if (cur.sze[i]) ans = mul(ans, k);
}
return ans;
}
int main () {
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back({b, i});
adj[b].push_back({a, i});
}
for (int i = 0; i < m; i++) {
cin >> val[i].first >> val[i].second;
flag = 0;
dfs(val[i].first, -1, val[i].second);
edges[i] = st;
st.clear();
}
int ans = power(k, n - 1);
for (int i = 1; i < (1 << m); i++) {
vector <int> s;
for (int j = 0; j < m; j++) if ((i >> j) & 1) s.push_back(j);
if (__builtin_popcount(i) & 1) ans = sub(ans, solve(s));
else ans = add(ans, solve(s));
}
cout << ans << '\n';
}
# | 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... |