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>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef long double ld;
const int mod = 1e9+7;
const int maxn = 65;
const ll inf = 1e18+5;
int n, m, k;
int _s, _t;
vector<int> edg[maxn];
vector<int> alle[maxn];
int vis[65];
ll pw[65];
vector<int> tr[maxn];
vector<pair<int, int>> g[maxn];
bool dfs(int x, int p, int id){
//cout << _s << " " << x << "\n";
if(x == _t)return 1;
for(auto v: g[x]){
if(v.f == p)continue;
if(dfs(v.f, x, id)){
//cout << v.s << "\n";
alle[id].push_back(v.s);
return 1;
}
}
return 0;
}
void _fill(int x){
vis[x] = 1;
for(auto v: tr[x])if(!vis[v])_fill(v);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
//cout << (1LL*k*k - k)%mod << "\n";
pw[0] = 1;
for(int i = 1; i <= n; i++)pw[i] = pw[i-1]*k%mod;
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
g[a].push_back({b, i});
g[b].push_back({a, i});
}
for(int i = 0; i < m; i++){
cin >> _s >> _t;
dfs(_s, 0, i);
}
ll ans = 0;
for(int i = 0; i < (1<<m); i++){
for(int j = 1; j < n; j++){
vis[j] = 0;
tr[j].clear();
}
for(int j = 0; j < m; j++){
if(i&(1<<j)){
for(int k = 1; k < alle[j].size(); k++){
int a = alle[j][0], b = alle[j][k];
tr[a].push_back(b);
tr[b].push_back(a);
}
}
}
int cnt = 0;
for(int j = 1; j < n; j++){
if(!vis[j]){
cnt++;
_fill(j);
}
}
if(__builtin_popcount(i)&1)ans -= pw[cnt];
else ans += pw[cnt];
ans%=mod;
}
if(ans < 0)ans += mod;
cout << ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:71:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int k = 1; k < alle[j].size(); k++){
| ~~^~~~~~~~~~~~~~~~
# | 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... |