• Source
    1. #include <iostream>
    2. #include <queue>
    3. using namespace std;
    4.  
    5. int N;
    6. int G[102][102];
    7. int new_G[102][102];
    8.  
    9. int check_ft[102]; //Fortress_i;
    10. void khoitao ()
    11. {
    12. for (int i=1; i<=N; i++)
    13. {
    14. check_ft[i]=0;
    15. }
    16. }
    17.  
    18. void BFS_queue (int u)
    19. {
    20. queue <int> Q;
    21. Q.push(u);
    22. check_ft[u]=1;
    23. while (!Q.empty())
    24. {
    25. int value = Q.front();
    26. Q.pop();
    27. for (int i=1; i<=N; i++)
    28. {
    29. if (new_G[value][i]==1 && check_ft[i]==0)
    30. {
    31. check_ft[i]=1;
    32. Q.push(i);
    33. }
    34. }
    35. }
    36. }
    37.  
    38. int main ()
    39. {
    40. //IN;
    41. int t;
    42. cin>>t;
    43. for (int k=1; k<=t; k++)
    44. {
    45. cin>>N;
    46. for (int i=1; i<=N; i++)
    47. {
    48. for (int j=1; j<=N; j++)
    49. {
    50. cin>>G[i][j];
    51. new_G[i][j]=G[i][j];
    52. }
    53. }
    54. //OUT;
    55. //Xac dinh slt ban dau;
    56. int sltMax=0;
    57. khoitao ();
    58. for (int i=1; i<=N; i++)
    59. {
    60. if (check_ft[i]==0)
    61. {
    62. sltMax++;
    63. BFS_queue (i);
    64. }
    65. }
    66. //Slt moi
    67. int ft=0;
    68. int sltMax_New=0;
    69. int isDestroy=0;
    70. for (int i=1; i<=N; i++)
    71. {
    72. int xoa=i;
    73. for (int r=1; r<=N; r++)
    74. {
    75. for (int c=1; c<=N; c++)
    76. {
    77. if (r==xoa || c==xoa) new_G[r][c]=0;
    78. else new_G[r][c]=G[c][r];
    79. }
    80. }
    81. khoitao ();
    82. int slt=0;
    83. for (int j=1; j<=N; j++)
    84. {
    85. if (check_ft[j]==0 && j!=xoa)
    86. {
    87. slt++;
    88. BFS_queue (j);
    89. }
    90. }
    91. if (slt!=sltMax && slt>sltMax_New)
    92. {
    93. isDestroy=1;
    94. sltMax_New=slt;
    95. ft=xoa;
    96. }
    97. }
    98. if (isDestroy==1) cout<<ft<<endl;
    99. else cout<<"0"<<endl;
    100. }
    101. return 0;
    102. }