#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 2e5 + 5;
vector<vector<pair<int,int>>> graph;
vector<vector<int>> edges;
int cnt;
bool found = false;
void dfs(int node, int parent, int target, int idx) {
if (node == target) {
found = true;
return ;
}
if (found)
return ;
for (auto u : graph[node]) {
if (found)
return ;
if (u.f != parent) {
edges[idx].push_back(u.s);
dfs(u.f, node, target, idx);
if (!found)
edges[idx].pop_back();
else
break;
}
}
}
ll expo(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b /= 2;
}
return res;
}
int par[65], sz[65];
int find(int node) {
if (par[node] == node)
return node;
return par[node] = find(par[node]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return ;
sz[a] += sz[b];
par[b] = a;
}
void dsu() {
for (int i = 0; i <= 62; i++)
par[i] = i, sz[i] = 1;
}
void dfs2(int node, int parent, bool comp) {
for (auto u : graph[node]) {
if (u.f == parent)
continue;
if (find(u.s) == 0) {
if (comp)
dfs2(u.f, node, true);
else {
comp = true;
cnt++;
dfs2(u.f, node, true);
}
} else
dfs2(u.f, node, false);
}
}
int main() {
fast
ll n, m, k, a, b;
cin >> n >> m >> k;
graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
edges = vector<vector<int>>(m, vector<int>());
for (int i = 1; i < n; i++) {
cin >> a >> b;
graph[a].push_back({b, i});
graph[b].push_back({a, i});
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
found = false;
dfs(a, a, b, i);
}
dsu();
for (int i = 0; i < m; i++) {
for (auto u : edges[i])
unite(0, u);
}
ll ans = 0;
const int mx_mask = (1<<m);
for (int msk = 0; msk < mx_mask; msk++) {
dsu();
for (int j = 0; j < m; j++) {
if (((1<<j) & msk) == 0)
continue;
for (auto u : edges[j])
unite(0, u);
}
cnt = 0;
dfs2(1, 1, false);
int s = sz[0] - 1;
if (__builtin_popcount(msk) % 2 == 0)
ans = (ans + expo(k, n-1-s) * expo(k, cnt)) % MOD;
else {
ans = (ans - expo(k, n-1-s) * expo(k, cnt) % MOD) % MOD;
while (ans < 0)
ans += MOD;
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |