Advertisement
zhukov000

Task 405

Apr 8th, 2020
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. int n, m, timer;
  4. std::vector< std::vector<int> > adj;
  5. std::vector< std::vector<int> > comp_sizes; // size of components
  6. std::vector< int > tin, fup;
  7. std::vector< bool > visited;
  8.  
  9. void dfs(int v, int p)
  10. {
  11.   visited[v] = true;
  12.   tin[v] = fup[v] = timer++;
  13.   int children = 0;
  14.   for(auto u: adj[v])
  15.   {
  16.     if (u == p) continue; // skip parent
  17.     if (visited[u])
  18.     { // back edge
  19.       fup[v] = std::min(fup[v], tin[u]);
  20.     }
  21.     else
  22.     {
  23.       int cnt1 = timer; // count vertex before U
  24.       dfs(u, v);
  25.       int cnt2 = timer; // count vertex after U
  26.       fup[v] = std::min(fup[v], fup[u]);
  27.       ++children;
  28.       if ((fup[u] >= tin[v] && p != 0) || (p == 0 && children > 1))
  29.       { // vertex V cut graph: components that start from U has size (cnt2 - cnt1)
  30.         comp_sizes[v].push_back(cnt2 - cnt1);
  31.       }
  32.     }
  33.   }
  34. }
  35.  
  36. int main()
  37. {
  38.   std::ios_base::sync_with_stdio(0);
  39.   std::cin.tie(0);
  40.   std::cout.tie(0);
  41. #ifdef AZHUKOV
  42.   freopen("input.txt", "r", stdin);
  43. #endif // AZHUKOV
  44.  
  45.   std::cin >> n >> m;
  46.   adj.resize(n + 1, std::vector<int>() );
  47.   comp_sizes.resize(n + 1, std::vector<int>() );
  48.   tin.resize(n + 1, 0); // time input
  49.   fup.resize(n + 1, 0); // min time out to up in subtree
  50.   visited.resize(n + 1, false);
  51.  
  52.   for(int i = 0; i < m; ++i)
  53.   {
  54.     int v_from, v_to;
  55.     std::cin >> v_from >> v_to;
  56.     adj[v_from].push_back(v_to);
  57.     adj[v_to].push_back(v_from);
  58.   }
  59.   timer = 1;
  60.   dfs(1, 0); // parent = 0 - without parent
  61.  
  62.   // calc answer
  63.   for(int v = 1; v<= n; ++v)
  64.   {
  65.     int ans = n - 1; // for each vertex V: V - U
  66.     int t = n - 1;
  67.     for(const int & x: comp_sizes[v])
  68.     {
  69.       t -= x;
  70.       assert(t >= 0);
  71.       ans += t * x;
  72.     }
  73.     std::cout << ans << std::endl;
  74.   }
  75.   return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement