fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 1234567890
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-8
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. #define dbl long double
  28. using namespace std;
  29. // mylittledoge
  30.  
  31. int main() {
  32. cin.sync_with_stdio(0);
  33. cin.tie(0);
  34. int T;
  35. cin >> T;
  36. for(int t =0; t < T; t++) {
  37. int N,G;
  38. cin >> N >> G;
  39. vector< vector<int > > dir(N,vector<int>(4));
  40. for(int i =0; i < N; i++)
  41. for(int j =0; j < 4; j++) {cin >> dir[i][j]; dir[i][j]--;}
  42. vector<int> nxt(4*N,-1);
  43. for(int i =0; i < N; i++) for(int j =0; j < 4; j++) {
  44. for(int k =0; k < 4; k++) if(dir[dir[i][j]][k] == i)
  45. nxt[4*i+j] =4*dir[i][j]+(k+2)%4;
  46. // cout << nxt[4*i+j] << " ";
  47. }
  48. // cout << "\n";
  49.  
  50. int comp =0;
  51. vector<int> isC(4*N,-1);
  52. for(int i =0; i < 4*N; i++) if(isC[i] == -1) {
  53. int akt =i;
  54. while(isC[akt] == -1) {
  55. isC[akt] =comp;
  56. akt =nxt[akt];}
  57. comp++;}
  58. // for(int i =0; i < N; i++) for(int j =0; j < 4; j++) {
  59. // cout << isC[4*i+j] << " ";}
  60. if(isC[(G-1)*4] == isC[(G-1)*4+1] || isC[(G-1)*4] == isC[(G-1)*4+3])
  61. cout << comp/2-1 << "\n";
  62. else cout << comp/2-2 << "\n";}
  63. return 0;}
  64.  
  65. // look at my code
  66. // my code is amazing
Success #stdin #stdout 0s 3480KB
stdin
4
5 5
2 3 4 5
3 4 5 1
4 5 1 2
5 1 2 3
1 2 3 4
5 5
5 3 2 4
4 5 3 1
1 5 2 4
1 3 5 2
1 3 2 4
5 4
5 3 2 4
4 5 3 1
1 5 2 4
1 3 5 2
1 3 2 4
5 5
5 3 2 4
4 5 3 1
1 5 2 4
1 5 2 3
1 3 2 4
stdout
0
0
1
1