tien_noob

Construct - ĐT

Jul 24th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. //Make CSP great again
  2. #include <bits/stdc++.h>
  3. #define TASK "TESTCODE"
  4. using namespace std;
  5. const int N = 1e5;
  6. multiset<int> Adj1[N + 1];
  7. int n, m, Low[N + 1], Num[N + 1], Count(0), Component[N + 1], d[N + 1], ComponentCount(0), res = 0, p, check;
  8. bool Free[N + 1], Visited[N + 1];
  9. stack<int> Stack;
  10. vector<int> Adj[N + 1], tmp;
  11. void read()
  12. {
  13.     cin >> n >> m;
  14.     for (int i = 1; i <= m; ++ i)
  15.     {
  16.         int u, v;
  17.         cin >> u >> v;
  18.         Adj1[u].insert(v);
  19.         Adj1[v].insert(u);
  20.         Adj[u].push_back(v);
  21.         Adj[v].push_back(u);
  22.     }
  23.     fill(Free + 1, Free + n + 1, true);
  24.     fill(Visited + 1, Visited + n + 1, false);
  25. }
  26. void Visit(int u)
  27. {
  28.     ++Count;
  29.     Low[u] = Num[u] = Count;
  30.     Stack.push(u);
  31.     for (int v : Adj1[u])
  32.     {
  33.         if (Free[v])
  34.         {
  35.             if (Num[v] != 0)
  36.             {
  37.                 Low[u] = min(Low[u], Num[v]);
  38.             }
  39.             else
  40.             {
  41.                 auto it = Adj1[v].find(u);
  42.                 Adj1[v].erase(it);
  43.                 Visit(v);
  44.                 Low[u] = min(Low[u], Low[v]);
  45.             }
  46.         }
  47.     }
  48.     if (Low[u] == Num[u])
  49.     {
  50.         ++ComponentCount;
  51.         int v;
  52.         do
  53.         {
  54.             v = Stack.top();
  55.             Stack.pop();
  56.             Component[v] = ComponentCount;
  57.             Free[v] = false;
  58.         }
  59.         while (v != u);
  60.     }
  61. }
  62. void DFS(int u)
  63. {
  64.     if (d[u] > check)
  65.     {
  66.         check = d[u];
  67.         p = u;
  68.     }
  69.     tmp.push_back(u);
  70.     for (int v : Adj[u])
  71.     {
  72.         if (!Visited[v])
  73.         {
  74.             int add = 0;
  75.             if (Component[v] != Component[u])
  76.             {
  77.                 add = 1;
  78.             }
  79.             d[v] = d[u] + add;
  80.             Visited[v] = true;
  81.             DFS(v);
  82.         }
  83.     }
  84. }
  85. void Get(int u)
  86. {
  87.     tmp.clear();
  88.     check = 0;
  89.     p = -1;
  90.     Visited[u] = true;
  91.     DFS(u);
  92.     if (p != -1)
  93.     {
  94.         check = 0;
  95.         for (int i : tmp)
  96.         {
  97.             d[i] = 0;
  98.             Visited[i] = false;
  99.         }
  100.         Visited[p] = true;
  101.         DFS(p);
  102.         res = max(res, check);
  103.     }
  104. }
  105. void solve()
  106. {
  107.     for (int i = 1; i <= n; ++ i)
  108.     {
  109.         if (Num[i] == 0)
  110.         {
  111.             Visit(i);
  112.         }
  113.     }
  114.     for (int i = 1; i <= n; ++ i)
  115.     {
  116.         if (!Visited[i])
  117.         {
  118.             Get(i);
  119.         }
  120.     }
  121.     cout << res;
  122. }
  123. int main()
  124. {
  125.     ios_base::sync_with_stdio(false);
  126.     cin.tie(nullptr);
  127.     cout.tie(nullptr);
  128.     //freopen(TASK".INP", "r", stdin);
  129.     //freopen(TASK".OUT", "w", stdout);
  130.     int t = 1;
  131.     bool typetest = false;
  132.     if (typetest)
  133.     {
  134.         cin >> t;
  135.     }
  136.     for (int __ = 1; __ <= t; ++ __)
  137.     {
  138.         read();
  139.         solve();
  140.     }
  141. }
  142.  
Add Comment
Please, Sign In to add comment