Advertisement
vahook

Mester / "IOI Válogató 2019" / 5. Banda szétválasztása

May 3rd, 2020
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #define MAXN 10000
  5. #define MAXM 500000
  6.  
  7. using namespace std;
  8.  
  9. vector<int> pontok[MAXN+1];
  10.  
  11. int N, M;
  12.  
  13. int tin[MAXN+1];
  14. int low[MAXN+1];
  15. int cnt = 1;
  16.  
  17. int valasz_komponens = 0, valasz_maxelottunk = 0, valasz = 1;
  18.  
  19. /* messze nem optimális */
  20.  
  21. int
  22. tarjan(int current, int parent)
  23. {
  24.     low[current] = tin[current] = cnt++;
  25.     int komponensek = 1;
  26.     int mi = 1;
  27.     int maxelottunk = 0;
  28.     int mogottunk = N - 1;
  29.  
  30.     bool ap = false;
  31.     for(const auto next : pontok[current]) {
  32.         if(next == parent) continue;
  33.  
  34.         if(!tin[next]) {
  35.             int gyerekek = tarjan(next, current);
  36.  
  37.             // hogyha a szülőnkkel egy komponensben vannak
  38.             if(low[next] >= tin[current]){
  39.                 maxelottunk = max(maxelottunk, gyerekek);
  40.                 mogottunk -= gyerekek;
  41.                 komponensek++;
  42.             }
  43.             mi += gyerekek;
  44.  
  45.             if(tin[current] <= low[next]) {
  46.                 ap = true;
  47.             }
  48.             low[current] = min(low[current], low[next]);
  49.         }else{
  50.             low[current] = min(low[current], tin[next]);
  51.         }
  52.     }
  53.    
  54.     if(ap && (current != 1 || (--komponensek >= 2))) {
  55.         maxelottunk = max(maxelottunk, mogottunk);
  56.  
  57.         if(komponensek > valasz_komponens || (komponensek == valasz_komponens && maxelottunk <= valasz_maxelottunk)){
  58.             valasz_komponens = komponensek;
  59.             valasz_maxelottunk = maxelottunk;
  60.             valasz = current;
  61.         }
  62.     }
  63.     return mi;
  64. }
  65.  
  66. int
  67. main()
  68. {
  69.     ios_base::sync_with_stdio(0);
  70.  
  71.     /* be */
  72.     cin >> N >> M;
  73.     for(int i = 0; i < M; i++) {
  74.         int a, b;
  75.         cin >> a >> b;
  76.         pontok[a].push_back(b);
  77.         pontok[b].push_back(a);
  78.     }
  79.  
  80.     /* do */
  81.     tarjan(1, 1);
  82.    
  83.     /* ki */
  84.     cout << valasz << endl;
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement