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>
//#include<ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC target("avx2")
//#pragma comment(linker, "/STACK:268435456")
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define F first
#define S second
#define pb push_back
#define sz(x) int(x.size())
#define el '\n'
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef short int si;
typedef unsigned long long ull;
typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 65;
const int M = 1e9 + 7;
int used[N], mk[N], pr[N], siz[N], from[N], to[N];
int n, m, k, p[N], pred[N], u, v, tin[N], tout[N], tim;
vector <int> g[N];
int mult(ll a, ll b) {return (a * b) % M;}
int sum(int a, int b) {return (a + b) % M;}
bool is_ac(int v, int u) {return (tin[v] <= tin[u] && tout[v] >= tout[u]);}
void dfs(int v, int pr) {
pred[v] = pr;
tin[v] = tim++;
for (auto u : g[v]) {
if (u == pr) {
continue;
}
dfs(u, v);
}
tout[v] = tim++;
}
int f(int v) {return (pr[v] == v ? v : pr[v] = f(pr[v]));}
void link(int a, int b) {
a = f(a); b = f(b);
pr[a] = b;
siz[b] += siz[a];
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("anti.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
p[0] = 1;
for (int i = 1; i < N; i++) {
p[i] = mult(p[i - 1], k);
}
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[v].pb(u);
g[u].pb(v);
}
dfs(1, -1);
for (int i = 0; i < m; i++) {
cin >> from[i] >> to[i];
}
int ans = 0;
for (int mask = 0; mask < (1 << m); mask++) {
for (int i = 1; i <= n; i++) {
mk[i] = -1;
}
for (int i = 0; i < m; i++) {
pr[i] = i;
siz[i] = 1;
used[i] = 0;
}
for (int i = 0; i < m; i++) {
if (!(mask & (1 << i))) {
continue;
}
int v = from[i];
while (!is_ac(v, to[i])) {
if (mk[v] == -1) {
mk[v] = i;
} else {
link(i, mk[v]);
}
v = pred[v];
}
v = to[i];
while (!is_ac(v, from[i])) {
if (mk[v] == -1) {
mk[v] = i;
} else {
link(i, mk[v]);
}
v = pred[v];
}
}
int pw = 0;
for (int i = 2; i <= n; i++) {
if (mk[i] == -1) {
pw++;
} else {
if (!used[f(mk[i])]) {
used[f(mk[i])] = 1;
pw++;
}
}
}
if (__builtin_popcount(mask) & 1) {
ans = sum(ans, M - p[pw]);
} else {
ans = sum(ans, p[pw]);
}
}
cout << ans;
}
# | 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... |