Advertisement
Tbl_Mne_Ne_Dryg

Untitled

Sep 29th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // trip to new record: 2100 hard (last record: 1800)
  6.  
  7. vector<vector<pair<int, int>>> g;
  8. vector<bool> used;
  9. vector<int> tin;
  10. vector<int> tup;
  11. int timer = 0;
  12. vector<int> ans;
  13. vector<bool> has_first;
  14.  
  15. void dfs1(int v, int pr, int ind) { // dfs tree
  16.     used[v] = 1;
  17.     tin[v] = timer++;
  18.     tup[v] = tin[v];
  19.     for(int i = 0; i < g[v].size(); i++) {
  20.         int to = g[v][i].first;
  21.         if(g[v][i].second == ind) continue;
  22.         if(!used[to]) {
  23.             dfs1(to, v, g[v][i].second);
  24.             tup[v] = min(tup[v], tup[to]);
  25.         }
  26.         else {
  27.             tup[v] = min(tup[v], tin[to]);
  28.         }
  29.     }
  30.     if(tin[pr] < tup[v]) {
  31.         ans.push_back(ind);
  32.     }
  33. }
  34.  
  35. vector<bool> used3;
  36. void dfs2(int v) { // 1 case
  37.    
  38. }
  39.  
  40. vector<int> pr;
  41. vector<bool> used2;
  42. void dfs3(int v, int pred) { // 2 case
  43.     used2[v] = true;
  44.     bool is_find = false;
  45.     for(int i = 0; i < g[v].size(); i++) {
  46.         int to = g[v][i].first;
  47.         if(to == pred) continue;
  48.         if(!used2[to]) {
  49.             pr[to] = v;
  50.             if(tup[v] == 0) is_find = true;
  51.             dfs3(to, v);
  52.         }
  53.     }
  54.     if(!is_find && tup[v] == 0 && v > 0) {
  55.         int ver = v;
  56.         vector<int> path;
  57.         while(ver != 0) {
  58.             path.push_back(ver);
  59.             ver = pr[ver];
  60.         }
  61.         path.push_back(0);
  62.         reverse(path.begin(), path.end());
  63.         for(auto& i : path) i++;
  64.         cout << path.size() << '\n';
  65.         for(int i = 0; i < path.size(); i++) cout << path[i] << ' ';
  66.     }
  67. }
  68.  
  69. int main() {
  70.     int n, m;
  71.     cin >> n >> m;
  72.     g.resize(n);
  73.     tin.resize(n);
  74.     tup.resize(n);
  75.     used.resize(n);
  76.     pr.resize(n);
  77.     used2.resize(n);
  78.     has_first.resize(n);
  79.     used.resize(n);
  80.     for(int i = 0; i < m; i++) {
  81.         int a, b;
  82.         cin >> a >> b;
  83.         if(a > b) {
  84.             swap(a, b);
  85.         }
  86.         a--;
  87.         b--;
  88.         if(a == 0) {
  89.             has_first[b] = 1;
  90.         }
  91.         g[a].push_back({b, i});
  92.         g[b].push_back({a, i});
  93.     }
  94.     for(int i = 0; i < n; i++) {
  95.         if(!used[i]) {
  96.             dfs1(i, i, -1);
  97.         }
  98.     }
  99.  
  100.     if(count(tup.begin(), tup.end(), 0) == 1) {
  101.         cout << -1;
  102.         dfs2(0);
  103.     }
  104.     else {
  105.         dfs3(0, 0);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement