Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <stack>
  7. #include <fstream>
  8. #include <unordered_map>
  9. #include <string>
  10. #include <algorithm>
  11. #include <sstream>
  12. #include <cmath>
  13. #include <queue>
  14. #include <time.h>
  15. #include <stdio.h>
  16. #include <list>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <list>
  20. #include <functional>
  21. #include <random>
  22. using namespace std;
  23. //#define JUDJE 1488
  24.  
  25. #ifndef JUDJE
  26.  
  27. ifstream in("input.in");
  28. ofstream out("output.out");
  29. #define cin in
  30. #define cout out
  31.  
  32. #endif
  33.  
  34. #define ll long long
  35. #define MOD 1000000007
  36. #define mp(a, b) make_pair(a, b)
  37. #define PI 3.1415926535
  38. #define EPS 0.00000001
  39. #define pii pair<int, int>
  40. #define INF 1000000000
  41.  
  42. bool matr[2000][2000];
  43.  
  44. vector<vector<int> > g;
  45.  
  46. bool used[2000];
  47. vector<int> path;
  48. int n, nn, x;
  49. set<pii> notUsedEdges;
  50.  
  51. bool toCircle()
  52. {
  53. if( matr[ path[0] ] [ path.back() ] )
  54. {
  55. path.push_back(path[0]);
  56. return 0;
  57. }
  58. int u = path.front();
  59. int v = path.back();
  60. if(u == v)
  61. return 0;
  62. for(int i = 1; i < path.size()-1; ++i)
  63. {
  64. int curVa = path[i];
  65. int predVa = path[i-1];
  66. vector<int> vvv;
  67. if(matr[u][curVa] && matr[v][predVa])
  68. {
  69. vvv.push_back(u);
  70. vvv.push_back(curVa);
  71. for(int j = i+1; j < path.size(); ++j)
  72. vvv.push_back( path[j] );
  73. vvv.push_back(predVa);
  74. for(int j = i-2; j >= 0; --j)
  75. vvv.push_back(path[j]);
  76. swap(vvv, path);
  77. return 0;
  78. }
  79. }
  80.  
  81.  
  82. for(int i = 1; i <= n; ++i)
  83. {
  84. if(!used[i])
  85. {
  86. if(matr[u][i] && matr[v][i])
  87. {
  88. for(int ii = 1; ii <= n; ++ii)
  89. if(used[ii] && matr[ii][i])
  90. notUsedEdges.erase(mp(ii, i));
  91. for(int ii = 1; ii <= n; ++ii)
  92. if(!used[ii] && matr[ii][i])
  93. notUsedEdges.insert(mp(i, ii));
  94. vector<int> vvv;
  95. used[i] = 1;
  96. vvv.push_back(i);
  97. for(int ii = 0; ii < path.size(); ++ii)
  98. vvv.push_back(path[ii]);
  99. vvv.push_back(i);
  100. swap(vvv, path);
  101. return 1;
  102. }
  103. }
  104. }
  105.  
  106. return 1;
  107. }
  108.  
  109. void addVertexToCircle()
  110. {
  111. pii a = *notUsedEdges.begin();
  112. for(int i = 1; i <= n; ++i)
  113. if(matr[i][a.second] && !used[i])
  114. notUsedEdges.insert( mp(a.second, i));
  115. for(int i = 1; i <= n; ++i)
  116. if(matr[a.second][i] && used[i])
  117. notUsedEdges.erase(mp(i, a.second));
  118. int j = 0;
  119. while(path[j] != a.first)
  120. ++j;
  121. ++j;
  122. vector<int> vvv;
  123. for(j; j < path.size() - 1; ++j)
  124. vvv.push_back(path[j]);
  125. for(int i = 0; path[i] != a.first; ++i)
  126. vvv.push_back(path[i]);
  127. vvv.push_back(a.first);
  128. vvv.push_back(a.second);
  129. used[a.second] = 1;
  130. swap(vvv, path);
  131. }
  132.  
  133. int main()
  134. {
  135. ios_base::sync_with_stdio(0);
  136. cin >> n >> nn >> x;
  137. g.resize(n+1);
  138. for(int i = 1; i <= n; ++i)
  139. {
  140. for(int j = 0; j < nn; ++j)
  141. {
  142. int a;
  143. cin >> a;
  144. g[i].push_back(a);
  145. matr[i][a] = 1;
  146. }
  147. }
  148. bool isFind = 1;
  149. int curV = x;
  150. used[x] = 1;
  151. path.push_back(x);
  152. while(isFind)
  153. {
  154. int i = 0;
  155. for(; i < nn; ++i)
  156. {
  157. if(!used[g[curV][i]])
  158. {
  159. used[g[curV][i]] = 1;
  160. path.push_back( g[curV][i] );
  161. curV = g[curV][i];
  162. isFind = 1;
  163. break;
  164. }
  165. }
  166. if(i == nn)
  167. isFind = 0;
  168. }
  169.  
  170. for(int i = 0; i < path.size(); ++i)
  171. for(int j = 1; j <= n; ++j)
  172. if(matr[path[i]][j] && !used[j])
  173. notUsedEdges.insert(mp(path[i], j));
  174.  
  175. for(int i = path.size(); i < n; ++i)
  176. if(!toCircle())
  177. addVertexToCircle();
  178. if(path.size() == n)
  179. toCircle();
  180. vector<int> ans;
  181. int jj = 0;
  182. if(path[0] == x)
  183. {
  184. for(int i = 0; i < n; ++i)
  185. cout << path[i] << " ";
  186. cout << path[0];
  187. return 0;
  188. }
  189.  
  190. while(path[jj] != x)
  191. ++jj;
  192. for(; jj < path.size(); ++jj)
  193. ans.push_back(path[jj]);
  194. for(int i = 1; path[i] != x; ++i)
  195. ans.push_back(path[i]);
  196. swap(ans,path);
  197. for(int i = 0; i < n; ++i)
  198. cout << path[i] << " ";
  199. cout << path[0];
  200. }
  201.  
  202. /*
  203.  
  204. 8 4 1
  205. 5 6 7 8
  206. 4 6 7 8
  207. 4 5 7 8
  208. 2 3 5 8
  209. 1 3 4 6
  210. 1 2 5 7
  211. 1 2 3 6
  212. 1 2 3 4
  213.  
  214. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement