orazma

f.cpp

Apr 18th, 2026
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MAXN = 4e5 + 10;
  13. int fup[MAXN], tin[MAXN], col[MAXN], cnt_col[MAXN];
  14. bool was[MAXN], is_bridge[MAXN];
  15. vector <pair <int, int> > g[MAXN], g1[MAXN];
  16. int timer;
  17. vector <pair <int, int> > ans(MAXN);
  18. vector <pair <pair <int, int>, int > > vec;
  19. map <pair <int, int>, bool > ma;
  20. void dfs(int v, int pr = -1){
  21.     was[v] = 1;
  22.     tin[v] = ++timer;
  23.     fup[v] = tin[v];
  24.  
  25.     for (auto [to, num] : g[v]){
  26.         if (!was[to]){
  27.             dfs(to, v);
  28.             fup[v] = min(fup[v], fup[to]);
  29.  
  30.             if (fup[to] > tin[v]){
  31.                 is_bridge[num] = 1;/*
  32.                 g1[v].pb({to, num});
  33.                 g1[to].pb({v, num});
  34.                 */
  35.                 vec.pb({{v, to}, num});
  36.             }
  37.             ans[num] = {v, to};
  38.             ma[{v, to}] = 1;
  39.  
  40.         }
  41.         else if (to != pr){
  42.             fup[v] = min(fup[v], tin[to]);
  43.             if (ma.count(make_pair(v, to)) == 0 && ma.count(make_pair(to, v)) == 0){
  44.                 ans[num] = {v, to};
  45.                 ma[{v, to}] = 1;
  46.             }
  47.         }
  48.     }
  49. }
  50.  
  51. void dfs2(int v, int comp){
  52.     was[v] = 1;
  53.     col[v] = comp;
  54.     cnt_col[comp]++;
  55.  
  56.     for (auto [to, num] : g[v]){
  57.         if (!was[to] && is_bridge[num] == 0){
  58.             dfs2(to, comp);
  59.         }
  60.     }
  61. }
  62.  
  63. void dfs3(int v){
  64.     was[v] = 1;
  65.     for (auto [to, num] : g1[v]){
  66.         if (!was[to]){
  67.             dfs3(to);
  68.             if (col[ans[num].f] != to)
  69.                 swap(ans[num].f, ans[num].s);
  70.         }
  71.     }
  72. }
  73.  
  74. void solve(){
  75.  
  76.     int n, m;
  77.     cin >> n >> m;
  78.  
  79.     for (int i = 1; i <= m; i++){
  80.         int x, y;
  81.         cin >> x >> y;
  82.         g[x].pb({y, i});
  83.         g[y].pb({x, i});
  84.     }
  85.  
  86.     dfs(1, -1);
  87.  
  88.     for (int i = 1; i <= n; i++){
  89.         was[i] = 0;
  90.     }
  91.     int cnt = 0;
  92.     int finish = 1;
  93.     int maxx = -1;
  94.     for (int i = 1; i <= n; i++){
  95.         if (!was[i]){
  96.             cnt++;
  97.             dfs2(i, cnt);
  98.             if (cnt_col[col[i]] > maxx){
  99.                 maxx = cnt_col[col[i]];
  100.                 finish = col[i];
  101.             }
  102.         }
  103.     }
  104.  
  105.     for (int i = 0; i < vec.size(); i++){
  106.         g1[col[vec[i].f.f]].pb({col[vec[i].f.s], vec[i].s});
  107.         g1[col[vec[i].f.s]].pb({col[vec[i].f.f], vec[i].s});
  108.     }
  109.  
  110.     for (int i = 1; i <= n; i++){
  111.         was[i] = 0;
  112.     }
  113.     cout << maxx << "\n";
  114.     dfs3(finish);
  115.  
  116.     for (int i = 1; i <= m; i++){
  117.         cout << ans[i].f << ' ' << ans[i].s << "\n";
  118.     }
  119. }
  120. int main() {
  121.     IOS;
  122.     int t = 1;
  123.    // cin >> t;
  124.     while(t--){
  125.         solve();
  126.     }
  127.     return 0;
  128. }
  129.  
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment