#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
3 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
3 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
3 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
3 ms |
204 KB |
Output is correct |
25 |
Correct |
3 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
10 ms |
296 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
13 ms |
320 KB |
Output is correct |
31 |
Correct |
3 ms |
204 KB |
Output is correct |
32 |
Correct |
2 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
2 ms |
204 KB |
Output is correct |
35 |
Correct |
5 ms |
204 KB |
Output is correct |
36 |
Correct |
27 ms |
312 KB |
Output is correct |
37 |
Correct |
11 ms |
312 KB |
Output is correct |