Advertisement
Korotkodul

Компоненты_вершинной_двусвязности

Oct 19th, 2021 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <map>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. vector <int> tin;
  9. vector <int> up;
  10. vector <bool> was;
  11. vector <vector<int>> gr;
  12. vector <bool> cut_p;
  13. int P = 0;
  14. queue <pair <int, int> > buf;
  15. map < pair <int,int> , int> marker;
  16. int color = 1;
  17. void dfs(int v, int last, int timer) {
  18.     was[v] = true;
  19.     tin[v] = up[v] = timer;
  20.     int child = 0;
  21.     for (int u: gr[v]){
  22.  
  23.         if (u == last) {
  24.  
  25.             continue;
  26.         }
  27.         else if (was[u] == true){
  28.             up[v] = min(up[v], tin[u]);
  29.             if (tin[u] < tin[v]){
  30.                 buf.push({min(u,v), max(u,v)});
  31.             }
  32.         }
  33.         else if (was[u] == false){
  34.             dfs(u, v, timer + 1);
  35.             up[v] = min(up[v], up[u]);
  36.             if (up[u] >= tin[v]){
  37.                 if (!cut_p[v]) P++;
  38.                 cut_p[v] = true;
  39.                 while (true){
  40.                     pair <int,int> edge = buf.back();
  41.                     buf.pop();
  42.                     marker[{min(u,v), max(u,v)}] = color;
  43.                     if (edge.first == min(v,u) && edge.second ==  max(v,u) ){
  44.                         color++;
  45.                         break;
  46.                     }
  47.                 }
  48.             }
  49.             child++;
  50.         }
  51.     }
  52.     if (v == last && child > 1) {
  53.         if (!cut_p[v]) P++;
  54.         cut_p[v] = true;
  55.     }
  56. }
  57.  
  58. int main()
  59. {
  60.     int n, m;
  61.     cin >> n >> m;
  62.     gr.resize(n);
  63.     cut_p.resize(n, false);
  64.     vector <pair<int,int> > Edges;
  65.     for (int i = 0; i < m; ++i) {
  66.         int a, b;
  67.         cin >> a >> b;
  68.         a--;
  69.         b--;
  70.         gr[a].push_back(b);
  71.         gr[b].push_back(a);
  72.         marker[{min(a,b), max(a,b)}] = -1;
  73.         Edges.push_back({min(a,b), max(a,b)});
  74.     }
  75.     tin.resize(n, -4);
  76.     up.resize(n, 1e9);
  77.     was.resize(n, false);
  78.     for (int i = 0;i<n;++i){
  79.         if (!was[i]) {
  80.             dfs(i, -1, 0);
  81.         }
  82.     }
  83.     /*cout<<"\ntin\n";
  84.     for (int i = 0;i<n;++i){
  85.         cout<<i+1<<": "<<tin[i]<<'\n';
  86.     }cout<<'\n';
  87.     cout<<"up\n";
  88.     for (int i=0;i<n;++i){
  89.         cout<<i+1<<": "<<up[i]<<'\n';
  90.     }cout<<'\n';
  91.     cout <<"amount of cut_points = "<< P<< '\n';
  92.     for (int i= 0;i<n;++i){
  93.         if (cut_p[i]){
  94.             cout<<i+1<<'\n';
  95.         }
  96.     }*/
  97.     was.assign(n, false);
  98.     int cnt = -1;
  99.     for (int i = 0;i<n;++i){
  100.         if (!was[i]){
  101.             cnt++;
  102.             dfs(i, -1, cnt);
  103.         }
  104.     }
  105.     cout<<"\nmarking\n";
  106.     for (auto x: Edges){
  107.         cout<<"("<<x.first + 1<<","<<x.second + 1<<") = "<<marker[x]<<'\n';
  108.     }
  109.     cout<<"DONE\n";
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement