fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define REP(i, a, b) for (int i = a; i <= b; i++)
  5. #define BACK(i, a, b) for (int i = a; i >= b; i--)
  6. #define MOD 1000000007
  7. #define PI 4 * atan(1)
  8. #define sz(A) (int)A.size()
  9. typedef long long ll;
  10. typedef vector<int> vi;
  11. typedef pair<int, int> pii;
  12. typedef vector<long long> vll;
  13. typedef long int int32;
  14. typedef unsigned long int uint32;
  15. typedef long long int int64;
  16. typedef unsigned long long int uint64;
  17.  
  18. int t,n,u;
  19. set<int> adj[104];
  20. bool visited[104];
  21. int a[104][104];
  22. void dfs(int u){
  23. visited[u] = true;
  24. for(int x: adj[u]){
  25. if(!visited[x]){
  26. dfs(x);
  27. }
  28. }
  29. }
  30. int tplt(){
  31. int res= 0 ;
  32. for(int i=1; i<=n; i++){
  33. if(!visited[i]){
  34. res++;
  35. dfs(i);
  36. }
  37. }
  38. return res;
  39. }
  40.  
  41. void solve(int test){
  42. freopen("CT.INP", "r", stdin);
  43. freopen("CT.OUT", "w",stdout);
  44. cin >> t >> n;
  45. if(t == 2) cin >> u;
  46. for(int i=1; i<=n; i++){
  47. for(int j=1; j<=n; j++){
  48. cin >> a[i][j];
  49. if(a[i][j] && i < j){
  50. adj[i].insert(j);
  51. adj[j].insert(i);
  52. }
  53. }
  54. }
  55. if(t == 1){
  56. int c0 = 0, c1 = 0;
  57. vector<int> deg(n+1 , 0);
  58. for(int i=1; i <=n; i++){
  59. deg[i] += adj[i].size();
  60. }
  61. for(int i=1; i<=n; i++){
  62. if(deg[i] & 1) c1++;
  63. else c0++;
  64. }
  65. if(tplt()> 1){
  66. cout << 0;
  67. return;
  68. }
  69. if(c0 == n) cout << 1;
  70. else if(c1 == 2) cout << 2;
  71. else cout << 0;
  72. }else{
  73. vector<int> ce;
  74. stack<int>st;
  75. st.push(u);
  76. while(!st.empty()){
  77. int f = st.top();
  78. if(adj[f].size() == 0){
  79. ce.push_back(f);
  80. st.pop();
  81. }
  82. else{
  83. int x = *adj[f].begin();
  84. st.push(x);
  85. adj[f].erase(x);
  86. adj[x].erase(f);
  87. }
  88. }
  89. for(int i=ce.size()-1; i >= 0; i--){
  90. cout << ce[i] << " ";
  91. }
  92. }
  93. }
  94. int main(){
  95. ios_base::sync_with_stdio(false);
  96. cin.tie(NULL);
  97. cout.tie(NULL);
  98. int typetest = 0;
  99. if (typetest){
  100. int t;
  101. cin >> t;
  102. cin.ignore();
  103. REP(i, 1, t){
  104. solve(i);
  105. }
  106. }
  107. else solve(0);
  108. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty