#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX_NODE 75
#define MAX_REQ 16
int numNode, numEdge, numColor;
vector<pair<int, int>> G[MAX_NODE];
int u[MAX_REQ], v[MAX_REQ];
void add(int &x, int y){
x += y;
if(x >= Mod) x %= Mod;
while(x < 0) x += Mod;
}
int mul(int a, int b){
return 1LL * a * b % Mod;
}
int pa[MAX_NODE], comp, sz[MAX_NODE];
void Init(void){
comp = numNode - 1;
for (int i = 1; i < numNode; ++i){
pa[i] = i;
sz[i] = 1;
}
}
int find(int u){
return (u == pa[u] ? u : pa[u] = find(pa[u]));
}
bool joint (int u, int v){
u = find(u), v = find(v);
if(u == v) return false;
--comp;
if(sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
pa[v] = u;
return true;
}
int edge_ID[MAX_NODE];
int depth[MAX_NODE], up[MAX_NODE];
void dfs(int u, int p = -1){
for(pair<int, int>&x : G[u]) {
int v = x.first, id = x.second;
if(v ^ p){
depth[v] = depth[u] + 1;
up[v] = u;
edge_ID[v] = id;
dfs(v, u);
}
}
}
void addEdge(int u, int v){
if(depth[u] < depth[v]) swap(u, v);
while(depth[u] > depth[v]){
if(up[u] != v){
int ID_u = edge_ID[u];
int ID_v = edge_ID[up[u]];
joint(ID_u, ID_v);
}
u = up[u];
}
while(u != v){
if(up[u] != up[v]){
int ID_u = edge_ID[u];
int ID_v = edge_ID[up[u]];
joint(ID_u, ID_v);
ID_u = edge_ID[v];
ID_v = edge_ID[up[v]];
joint(ID_u, ID_v);
}
else{
joint(edge_ID[u], edge_ID[v]);
}
u = up[u], v = up[v];
}
}
void process(void){
cin >> numNode >> numEdge >> numColor;
for (int i = 1; i < numNode; ++i){
int u, v; cin >> u >> v;
G[u].emplace_back(v, i);
G[v].emplace_back(u, i);
}
dfs(1);
for (int i = 0; i < numEdge; ++i){
cin >> u[i] >> v[i];
}
Init();
int ans = 0;
for (int mask = 0; mask < MASK(numEdge); ++mask){
Init();
for (int i = 0; i < numEdge; ++i) if(BIT(mask, i)){
addEdge(u[i], v[i]);
}
int sign = (__builtin_popcount(mask) & 1 ? -1 : 1);
int pw = 1;
for (int i = 1; i <= comp; ++i){
pw = mul(pw, numColor);
}
add(ans, sign * pw);
}
cout << ans;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
21 |
Correct |
1 ms |
504 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
504 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
7 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
0 ms |
336 KB |
Output is correct |
30 |
Correct |
7 ms |
472 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
504 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
36 |
Correct |
13 ms |
472 KB |
Output is correct |
37 |
Correct |
6 ms |
336 KB |
Output is correct |