# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1107706 | LTTrungCHL | Šarenlist (COCI22_sarenlist) | C++17 | 12 ms | 504 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///***LTT***///
/// ->NHAT QUOC GIA<- ///
#include<bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
using namespace std;
vector <int> lg2;
//void MAKE_LOG_ARR(int n){
// lg2.resize(n + 3);
// for (int i = 1;i <= n;++i){
// lg2[i] = __lg(i);
// }
//}
const long long oo = 1e9+7;
const long long e = 1e9+7;
const int N =100;
int h[N], parent[N], idpa[N];
int sz[N];
int muk[N];
int m, k;
int last[N];
int pa[N];
int n;
vector <pair <int ,int>> adj[N];
bool dd[N];
vector <int> dk[20];
void pre(int u, int p){
h[u] = h[p] + 1;
for (int i = 0;i < adj[u].size();++i){
int v = adj[u][i].F;
int id = adj[u][i].S;
if (v == p) continue;
pre(v, u);
pa[v] = u;
idpa[v] = id;
}
}
int finds(int u){
if (u == parent[u]) return u;
int pu = finds(parent[u]);
parent[u] = pu;
return pu;
}
void unions(int u, int v){
u = finds(u);
v = finds(v);
if (u != v){
if (sz[u] < sz[v]) swap(u, v);
parent[v] = u;
sz[u] += sz[v];
}
return;
}
void solve(){
cin >> n >> m >> k;
for (int i = 1;i < n;++i){
int u, v;
cin >> u >> v;
adj[u].pb({v, i});
adj[v].pb({u, i});
}
pre(1,0);
for (int i = 1;i <= m;++i){
int x, y;
cin >> x >> y;
while (x != y){
if (h[x] > h[y]){
dk[i].pb(idpa[x]);
x = pa[x];
} else {
dk[i].pb(idpa[y]);
y = pa[y];
}
}
}
int ans = 1;
muk[0] = 1;
for (int i = 1;i <= n;++i){
muk[i] = 1ll * muk[i - 1] * k % e;
}
for (int i = 1;i < n;++i){
ans = 1ll * ans * k % e;
// ans %= e;
}
for (int mask = 1;mask < (1 << m);++mask){
for (int i = 1;i < n;++i){
last[i] = 0;
}
for (int i = 1;i <= m;++i){
parent[i] = i;
dd[i] = false;
sz[i] = 1;
}
int cnt = 0, cntbit = 0;
for (int i = 0;i < m;++i){
if ((1 << i) & mask){
cntbit++;
for (int z : dk[i + 1]){
// cout << z <<"\n";
if (last[z]){
unions(i + 1, last[z]);
}
if (!last[z]){
++cnt;
}
last[z] = i + 1;
}
}
}
int cnt2 = 0;
for (int i = 1;i <= m;++i){
if (!dd[finds(i)] and ((1 << (i - 1)) & mask) ){
dd[finds(i)] = true;
++cnt2;
}
}
if (cntbit & 1){
ans += e;
// cout << mask <<" th1 "<< 1ll * muk[n - 1 - cnt] * muk[cnt2]%e <<"\n";
ans -= 1ll * muk[n - 1 - cnt] * muk[cnt2]%e;
if (ans >= e) ans -= e;
} else {
// cout << mask <<" th2 "<< 1ll * muk[n - 1 - cnt] * muk[cnt2]%e <<"\n";
ans += 1ll * muk[n - 1 - cnt] * muk[cnt2]%e;
if (ans >= e) ans -= e;
}
}
cout << ans;
return;
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("ltt.inp", "r")){
freopen("ltt.inp", "r", stdin);
freopen("ltt.out", "w", stdout);
}
// int t;
// cin >> t;
// while(t--){
solve();
// }
return 0;
}
Compilation message (stderr)
# | 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... |