蓝题训练9.25P2962 [USACO09NOV] Lights G
P2962 [USACO09NOV] Lights G
https://www.luogu.com.cn/problem/P7323
Tag:略
题意:略
题解:meet in the middle第一次见
参考代码
#include<bits/stdc++.h>
using namespace std;
using ll =long long;
ll n,m;
ll f[1000005],g[1000005],ans[40];
vector<ll> a[40];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
ans[0]=1;
for(int i=1;i<=38;i++){
ans[i]=ans[i-1]*2;
}
for(ll x,y, i=1;i<=m;i++){
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
ll s=n/2,t=n-s;
ll now=0;
f[0]=0;
for(ll i=1;i<((1LL<<(s)));i++){
if(__builtin_popcount(i)==1){
now++;
f[i]+=ans[now-1];
for(auto j:a[now]){
f[i]+=ans[j-1];
}
}
else{
ll u=(i&-i);
f[i]=f[i-u]^f[u];
}
}
g[0]=0;
for(ll i=1;i<((1LL<<(t)));i++){
if(__builtin_popcount(i)==1){
now++;
g[i]+=ans[now-1];
for(auto j:a[now]){
g[i]+=ans[j-1];
}
}
else{
ll u=(i&-i);
g[i]=g[i-u]^g[u];
}
}
map<ll,ll> mp;
for(ll i=0;i<((1LL<<(s)));i++){
f[i]^=((1LL<<(n))-1);
if(mp.find(f[i])!=mp.end()) mp[f[i]]=min(mp[f[i]],(ll)__builtin_popcount(i));
else mp[f[i]]=(ll)__builtin_popcount(i);
}
ll v=n+8;
for(ll i=0;i<((1LL<<(t)));i++){
if(mp.find(g[i])!=mp.end()) v=min(v,(ll)__builtin_popcount(i)+mp[g[i]]);
}
cout<<v<<"\n";
return 0;
}

浙公网安备 33010602011771号