fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <cstdio>
  4. using namespace std;
  5. int countbits(long long int a){
  6. long long int c(1);
  7. int total(0);
  8. for (;c<=a;c=(c<<1))if ((a&c)==c)total++;
  9. return total;
  10. }
  11. int main()
  12. {
  13. int t;
  14. scanf("%d",&t);
  15. for (;t>0;t--){
  16. int k;
  17. scanf("%d",&k);
  18. bool adj[k+1][k+1];
  19. for (int c(0);c<k;c++)
  20. for (int c1(0);c1<k;c1++)
  21. {int temp;scanf("%d",&temp);adj[c][c1]=temp;}
  22. long long result[1<<k];
  23. result[0]=1;
  24.  
  25. for (long long int c(1);c<(1<<k);c++){
  26. result[c]=0;
  27. int bitss(countbits(c));
  28. for (int c1(0);c1<k;c1++){
  29. if (adj[bitss-1][c1]&&(c&(1<<c1))==(1<<c1))result[c]=result[c]+result[c&(~(1<<c1))];
  30.  
  31. }
  32.  
  33.  
  34. }
  35. printf("%lld\n",result[(1<<k)-1]);
  36. }
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 3348KB
stdin
3
3
1 1 1
1 1 1
1 1 1
11
1 0 0 1 0 0 0 0 0 1 1 
1 1 1 1 1 0 1 0 1 0 0 
1 0 0 1 0 0 1 1 0 1 0 
1 0 1 1 1 0 1 1 0 1 1 
0 1 1 1 0 1 0 0 1 1 1 
1 1 1 0 0 1 0 0 0 0 0 
0 0 0 0 1 0 1 0 0 0 1 
1 0 1 1 0 0 0 0 0 0 1 
0 0 1 0 1 1 0 0 0 1 1 
1 1 1 0 0 0 1 0 1 0 1 
1 0 0 0 1 1 1 1 0 0 0 
11
0 1 1 1 0 1 0 0 0 1 0 
0 0 1 1 1 1 1 1 1 1 1 
1 1 0 1 0 0 0 0 0 1 0 
0 1 0 1 0 1 0 1 0 1 1 
1 0 0 1 0 0 0 0 1 0 1 
0 0 1 0 1 1 0 0 0 0 1 
1 0 1 0 1 1 1 0 1 1 0 
1 0 1 1 0 1 1 0 0 1 0 
0 0 1 1 0 1 1 1 1 1 1 
0 1 0 0 0 0 0 0 0 1 1 
0 1 1 0 0 0 0 0 1 0 1 
stdout
6
7588
7426