Advertisement
Ankit_132

G

Mar 28th, 2024
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll     long long
  5. #define ff     first
  6. #define ss     second
  7. #define pb     push_back
  8.  
  9. int N;
  10. string G[16],W[16];
  11. bool poss[16][16];
  12. bool dp[1<<16][16];
  13.  
  14. int main()
  15. {
  16.     int test;
  17.     cin>>test;
  18.  
  19.     while(test--){
  20.         cin>>N;
  21.         for(int i=0;i<N;i++)
  22.             cin>>G[i]>>W[i];
  23.         for(int i=0;i<N;i++)
  24.         {
  25.             for(int j=0;j<N;j++)
  26.                 poss[i][j]=G[i]==G[j] || W[i]==W[j];
  27.         }
  28.  
  29.         for(int i=0;i<1<<N;i++)
  30.         {
  31.             for(int j=0;j<N;j++)
  32.                 dp[i][j]=false;
  33.         }
  34.  
  35.         for(int i=0;i<N;i++)
  36.             dp[1<<i][i]=true;
  37.  
  38.         int ans=0;
  39.  
  40.         for(int i=1;i<1<<N;i++)
  41.         {
  42.             for(int j=0;j<N;j++)
  43.             {
  44.                 if(dp[i][j])
  45.                 {
  46.                     ans=max(ans,__builtin_popcount(i));
  47.                     for(int k=0;k<N;k++) if(!(i>>k&1) && poss[j][k]) dp[i|1<<k][k]=true;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         cout<<N-ans<<"\n";
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement