Advertisement
Morass

NPLANAR

Mar 21st, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PB push_back
  4. #define ZERO (1e-10)
  5. #define INF (1<<29)
  6. #define CL(A,I) (memset(A,I,sizeof(A)))
  7. #define DEB printf("DEB!\n");
  8. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  9. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef pair<ll,ll> pll;
  13. typedef vector<int> vi;
  14. typedef pair<int,int> ii;
  15. typedef vector<ii> vii;
  16. #define IN(n) int n;scanf("%d",&n);
  17. #define FOR(i, m, n) for (int i(m); i < n; i++)
  18. #define REP(i, n) FOR(i, 0, n)
  19. #define F(n) REP(i, n)
  20. #define FF(n) REP(j, n)
  21. #define FT(m, n) FOR(k, m, n)
  22. #define aa first
  23. #define bb second
  24. void ga(int N,int *A){F(N)scanf("%d",A+i);}
  25. #define MX 3333
  26. struct tr{int a,b,c;}T[256];
  27. #define SQ (47)
  28. struct bs{
  29.     ll A[SQ];
  30.     void clr(){CL(A,0);}
  31.     void operator&=(bs&r){F(SQ)A[i]&=r.A[i];}
  32.     void ad(int u){A[u>>6]|=1ull<<(u&63);}
  33.     bool ct(int v){return (A[v>>6]>>(v&63))&1;}
  34.     int xt(int u){
  35.         while(++u&63)if((A[u>>6]>>(u&63))&1)return u;
  36.         while(u<SQ<<6&&!A[u>>6])++u;
  37.         if(u==SQ<<6)return -1;
  38.         while(!ct(u))++u;
  39.         return u;
  40.     }
  41. }B[MX],t;
  42. vi g[MX];
  43. int fcl(int N){
  44.     int L(0),a;
  45.     F(N)B[i].clr();
  46.     F(N)for(auto&h:g[i])B[i].ad(h);
  47.     F(N)for(auto&h:g[i])if(i<h){
  48.         t=B[i],t&=B[h],a=h;
  49.         while(~(a=t.xt(a)))T[L++]={i,h,a};
  50.     }
  51.     return L;
  52. }
  53. int N,M,a,b,L,cn[MX];
  54. bool con(tr&a,tr&b){return a.a==b.a||a.a==b.b||a.a==b.c||a.b==b.a||a.b==b.b||a.b==b.c||a.c==b.a||a.c==b.b||a.c==b.c;}
  55. unordered_map<ll,int> dp;
  56. int dyn(int u,ll b){
  57.     if(dp.count(b))return dp[b];
  58.     while(b&1ull<<u)++u;
  59.     if(u==L)return 0;
  60.     int S(INF);
  61.     for(auto&h:g[u])if(!(1ull<<h&b))S=min(S,1+dyn(u+1,b|1ull<<h|1ull<<u));
  62.     if(S==INF)S=dyn(u+1,b|1ull<<u)+1;
  63.     return dp[b]=S;
  64. }
  65. int main(void){
  66.     IN(tt)F(tt){
  67.         CL(cn,-1),L=0,dp.clear();for(auto&h:g)h.clear();
  68.         scanf("%d%d",&N,&M);
  69.         F(M)scanf("%d%d",&a,&b),g[--a].PB(--b),g[b].PB(a);
  70.         L=fcl(N);for(auto&h:g)h.clear();
  71.         F(L)FT(i+1,L)if(con(T[i],T[k]))g[i].PB(k);
  72.         printf("%d\n",dyn(0,0));
  73.     }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement