Advertisement
Z_Michael

1389

May 13th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. long long int n, m, h = 2433137;
  8.  
  9. vector <vector <int>> g;
  10. vector <int> f;
  11. unordered_map <long long int, pair<int, int>> d;
  12.  
  13. pair <int, bool> rek( int u,  int v) {
  14.     if (g[u].size() > 1 || u == 1) {
  15.  
  16.         bool trig = false;
  17.         int res = 0;
  18.  
  19.         for (auto i : g[u]) {
  20.             if (i != v) {
  21.  
  22.                 pair < int, bool> t = rek(i, u);
  23.  
  24.                 f[i] = 1;
  25.                 res += t.first;
  26.  
  27.                 if (!t.second) {
  28.                     if (!trig) {
  29.                         trig = true;
  30.                         res++;
  31.                         f[i] = 2;
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.  
  37.         return { res , trig };
  38.     }
  39.     else {
  40.         return {0, false};
  41.     }
  42. }
  43.  
  44. void rekk(int u, int v) {
  45.     if (g[u].size() > 1 || u == 1) {
  46.  
  47.         bool trig = true;
  48.  
  49.         for (auto i : g[u]) {
  50.             if (i != v) {
  51.                
  52.                 rekk(i, u);
  53.                
  54.                 if (f[i] == 2) {
  55.                     cout << d[u * h + i].first << " " << d[u * h + i].second << "\n";
  56.                 }
  57.             }
  58.         }
  59.  
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     iostream::sync_with_stdio(false);
  66.     cin.tie(0), cout.tie(0);
  67.  
  68.     cin >> n >> m;
  69.  
  70.     g.resize(n + 1, f);
  71.     f.resize(n + 3, 0);
  72.  
  73.     for (int i = 0; i < m; i++) {
  74.         int a, b;
  75.  
  76.         cin >> a >> b;
  77.  
  78.         g[a].push_back(b);
  79.         g[b].push_back(a);
  80.  
  81.         d[a * h + b] = { a, b };
  82.         d[b * h + a] = { a, b };
  83.     }
  84.    
  85.     cout << rek(1, 0).first << "\n";
  86.     rekk(1, 0);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement