Advertisement
Korotkodul

форд 3

Mar 27th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33. for (bool x: v) cout<<x<<' ';
  34. cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string> v){
  38. for (auto a: v){
  39. cout<<a<<"\n";
  40. }
  41. }
  42.  
  43.  
  44.  
  45.  
  46. struct ed{
  47. int fr, to, w;
  48. };
  49.  
  50. int inf = 2e9;
  51.  
  52. vector <int> to_comp;
  53. vector <int> clr;
  54. vector <vector <int> > Gra;//список смеж
  55.  
  56. void dfs(int v, int pr, int comp_v){
  57. clr[v] = 1;
  58. to_comp[v] = comp_v;
  59. for (int u: Gra[v]){
  60. if (u == pr) continue;
  61. if (clr[u] == 0){
  62. dfs(u, v, comp_v);
  63. }
  64. }
  65. clr[v] = 2;
  66. }
  67.  
  68.  
  69. void Frd(vector <ed> G, int str){
  70. cout<<"GO\n";
  71. int m = G.size();
  72. int n;
  73. set <int> al_v;
  74. for (ed x: G){
  75. al_v.insert(x.to);
  76. al_v.insert(x.fr);
  77. }
  78. n = al_v.size();
  79. cout<<"al_v\n";
  80. for (int v: al_v) cout<<v<<' ';
  81.  
  82. cout<<"\n n m = "<<n<<' '<<m<<"\n";
  83.  
  84. vector <int> d(n, inf);
  85. vector <int> pred(n); //может надо как-то для начала его заполнить?
  86. for (int i = 0; i < n; ++i) pred[i] = i;
  87.  
  88. d[str] = 0;
  89. int upd_lst;
  90. for (int i = 0; i < n; ++i){
  91. upd_lst = -1;
  92. cout<<"i = "<<i<<"\n";
  93. for (int j = 0; j < m; ++j){
  94. if (d[ G[j].fr ] == inf){
  95. continue;
  96. }
  97. if (d[ G[j].to ] > d[ G[j].fr ] + G[j].w){
  98. d[ G[j].to ] = d[ G[j].fr ] + G[j].w;
  99. upd_lst = G[j].to;
  100. pred[ G[j].to ] = G[j].fr;
  101. }
  102. }
  103. }
  104.  
  105. if (upd_lst == -1) {
  106. cout<<"NO cycle\n";
  107. return;
  108. }
  109.  
  110. int v = upd_lst;
  111. for (int i = 0; i < n; ++i){
  112. v = pred[v];
  113. }
  114. str = v;
  115. v = pred[v];
  116. vector <int> ans = {str};
  117. while (v != str){
  118. ans.push_back(v);
  119. v = pred[v];
  120. }
  121. ans.push_back(str);
  122. reverse(ans.begin(), ans.end());
  123. cout<<"YES\n";
  124. for (int i: ans) cout<<i<<' ';
  125. exit(0);
  126. }
  127.  
  128.  
  129. int main()
  130. {
  131. /*ios::sync_with_stdio(0);
  132. cin.tie(0);
  133. cout.tie(0);*/
  134. int n; cin>>n;
  135. int m = 0;
  136. vector <int> d(n, inf);
  137. d[0]=0;
  138. vector <ed> G;//список ребер
  139. Gra.resize(n);
  140. vector <int> pred(n);
  141. for (int i = 0; i < n ;++i){
  142. for (int j = 0; j < n;++j){
  143. int ds; cin>>ds;
  144. if (ds == 100000) continue;
  145. ed x = {i, j, ds};
  146. Gra[x.fr].push_back(x.to);
  147. m++;
  148. G.push_back(x);
  149. }
  150. }
  151. for (ed x: G){
  152. // cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
  153. }
  154. to_comp.assign(n, -1);
  155. clr.assign(n, 0);
  156. int cnt = -1;
  157. for (int i = 0; i < n; ++i){
  158. if (to_comp[i] == -1){
  159. cnt++;
  160. dfs(i, i, cnt);
  161. }
  162. }
  163. //cout<<"to_comp\n";
  164. for (int i = 0; i < n;++i){
  165. //cout<<i<<' '<<to_comp[i]<<"\n";
  166. }
  167. //cout<<"cnt= "<<cnt;
  168. vector <vector <ed> > comp(cnt+1);
  169. for (int i = 0; i < m; ++i){
  170. comp[ to_comp[ G[i].fr ] ].push_back( G[i] );
  171. }
  172.  
  173.  
  174.  
  175. cout<<"comp\n";
  176. for (int i = 0; i < cnt+1;++i){
  177. cout<<i<<"\n";
  178. for(auto x: comp[i]){
  179. cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
  180. }
  181. }
  182.  
  183. for (int i = 0; i < cnt + 1; ++i){
  184. cout<<"COMPONENT = "<<i<<"\n";
  185. cout<<"sz = "<<comp[i].size()<<"\n";
  186. int str = comp[i][0].fr;
  187. cout<<"str = "<<str<<"\n";
  188. Frd(comp[i], str);
  189. }
  190. cout<<"NO\n";
  191. }
  192. /*
  193. 13
  194. 100000 1 100000 100 100000 100000 100000 100000 100000 100000 100000 100000 100000
  195. 100000 100000 10 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  196. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  197. 100000 -10 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  198. 100000 100000 100000 100000 100000 1 100000 100000 100000 100000 100000 100000 100000
  199. 100000 100000 100000 100000 100000 100000 2 100000 -10 100000 100000 100000 100000
  200. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  201. 100000 100000 100000 100000 100000 100000 3 100000 4 100000 100000 100000 100000
  202. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  203. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 1 100000 3
  204. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 2 100000
  205. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 -7
  206. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 4 0 100000
  207. */
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement