Tranvick

Untitled

Dec 15th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <map>
  4. using namespace std;
  5. #define ll long long
  6. #define rep(i,n) for (int i=0;i<(n);i++)
  7. #define N 2222
  8.  
  9. int ef[N],es[N],first[N],next[N],c,n,m,ans=N,l;
  10. map<ll,int> t1,t2;
  11.  
  12. void add(int x,int y){
  13.     next[++c]=first[x];first[x]=c;
  14.     ef[c]=x;es[c]=y;
  15.     next[++c]=first[y];first[y]=c;
  16.     ef[c]=y;es[c]=x;
  17. }
  18.  
  19. void init(){
  20.     freopen("lights.in","r",stdin);
  21.     freopen("lights.out","w",stdout);
  22.     scanf("%d%d",&n,&m);
  23.     rep(i,m){
  24.         int x,y;
  25.         scanf("%d%d",&x,&y);
  26.         add(x,y);
  27.     }
  28. }
  29.  
  30. int main(){
  31.     init();
  32.     m=n/2,l=n-m;
  33.     rep(i,1<<m){
  34.         ll cur=0;int ff=0;
  35.         rep(j,m) if (i & (1<<j)){
  36.             cur^=(1ll<<j);++ff;
  37.             for (int h=first[j+1];h;h=next[h]) cur^=(1ll<<(es[h]-1));
  38.         }
  39.         if (t1.count(cur)) t1[cur]=min(t1[cur],ff);
  40.         else t1[cur]=ff;
  41.     }
  42.     rep(i,1<<l){
  43.         ll cur=0;int ff=0;
  44.         rep(j,l) if (i & (1<<j)){
  45.             cur^=(1ll<<(j+m));++ff;
  46.             for (int h=first[j+m+1];h;h=next[h]) cur^=(1ll<<(es[h]-1));
  47.         }
  48.         if (t2.count(cur)) t2[cur]=min(t2[cur],ff);
  49.         else t2[cur]=ff;
  50.     }  
  51.     ll finish=(1ll<<n)-1;
  52.     if (t1.count(finish)) ans=t1[finish];
  53.     if (t2.count(finish)) ans=min(ans,t2[finish]);
  54.     for (map<ll,int>::iterator it=t1.begin();it!=t1.end();it++){
  55.         ll ff=it->first ^ finish;
  56.         if (t2.count(ff)) ans=min(ans,it->second+t2[ff]);
  57.     }
  58.     printf("%d\n",ans);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment