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 endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
vector<int>G[67];
ll p[67];
int d[67];
int o[67];
ll ile[(1<<15)+7];
int is[67];
int r[20];
int n,m,k;
void dfs(int v)
{
for(auto u:G[v])
{
if(u==o[v]) continue;
d[u]=d[v]+1;
o[u]=v;
dfs(u);
}
}
void Union(int u,int v)
{
for(int i=0;i<m;i++) if(r[i]==v) r[i]=u;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a,b;
cin>>n>>m>>k;
p[0]=1;
for(int i=1;i<=n;i++) p[i]=p[i-1]*(ll)k%mod;
for(int i=1;i<n;i++)
{
cin>>a>>b;
G[a].pb(b);
G[b].pb(a);
}
dfs(1);
vector<pair<int,int>>V(m);
for(int i=0;i<m;i++) cin>>V[i].st>>V[i].nd;
for(int l=0;l<(1<<m);l++)
{
memset(is,-1,sizeof is);
for(int i=0;i<m;i++) r[i]=i;
for(int i=0;i<m;i++)
{
if(!((1<<i)&l)) continue;
int a=V[i].st,b=V[i].nd;
while(a!=b)
{
if(d[a]<d[b]) swap(a,b);
if(is[a]!=-1) Union(r[is[a]],r[i]);
else is[a]=r[i];
a=o[a];
}
}
int c=0;
for(int i=2;i<=n;i++) if(is[i]==-1) c++;
set<int>S;
for(int i=0;i<m;i++)
{
if(!((1<<i)&l)) continue;
S.insert(r[i]);
}
ile[l]=p[c+sz(S)];
}
for(int i=m-1;i>=0;i--)
{
for(int j=(1<<m)-1;j>=0;j--)
{
if(!(j&(1<<i)))
{
ile[j]-=ile[j^(1<<i)];
if(ile[j]<0) ile[j]+=mod;
}
}
}
cout<<ile[0];
return 0;
}
# | 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... |