P3349 [ZJOI2016] 小星星 题解
:::::success[Hint 1] 考虑转化问题,设出朴素的状压 DP。 ::::: :::::success[Hint 2] 考虑容斥。 :::::
对应 Hint 1:
我们考虑更加抽象化地转化这个问题,这个问题本质上就是钦定一个排列
我们考虑设出朴素的状压 DP,以
对应 Hint 2:
我们考虑如何把这个瓶颈优化掉,如果我们仍然记录
这样我们就做完了这道题。
时间复杂度为
:::::success[code]
#include<bits/stdc++.h>
#define inl inline
//#define int long long
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i)
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i)
#define endl '\n'
#define INF 1e16
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int>
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=25;
bool vis[N][N];
vector<int>g[N];
int s,n;
long long f[N][N];
void dfs(int u,int ft){
rep(i,1,n)
if(s>>i-1&1) f[u][i]=1;
else f[u][i]=0;
at(v,g[u])
if(v!=ft){
dfs(v,u);
rep(i,1,n)
if(s>>i-1&1){
long long sum=0;
rep(j,1,n)
if((s>>j-1&1)&&vis[i][j]) sum+=f[v][j];
f[u][i]*=sum;
}
}
}
signed main(){
fst;
int m;
long long ans=0;
cin>>n>>m;
rep(i,1,m){
int u,v;
cin>>u>>v;
vis[u][v]=vis[v][u]=1;
}
rep(i,2,n){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
rep(i,0,(1ll<<n)-1){
s=i;
dfs(1,0);
rep(j,1,n)
if(s>>j-1&1)
if(__builtin_popcount((1ll<<n)-1^i)&1) ans-=f[1][j];
else ans+=f[1][j];
// cout<<i<<' '<<ans<<endl;
// rep(u,1,n){
// rep(j,1,n) cout<<f[u][j]<<' ';
// cout<<endl;
// }
}
cout<<ans<<endl;
return 'T'^'0'^'m'^'l'^'e';
}
:::::