Advertisement
_rashed

UVA 10459

Jan 27th, 2023
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. int head[5000], nxt[10000], edge[10000], deg[5000], rem[5000];
  20. vector<int> worst,best;
  21. int best_h = 1e9;
  22. int worst_h = 0;
  23. int g_sz = 0;
  24.  
  25. void add_edge(int a, int b) {
  26.     edge[g_sz] = b;
  27.     nxt[g_sz] = head[a];
  28.     head[a] = g_sz;
  29.     deg[a]++;
  30.     g_sz++;
  31. }
  32.  
  33. void dfs(int x, int parent, int dist, int t_dist) {
  34.     int e_idx = head[x];
  35.     bool is_leaf = true;
  36.     while(e_idx != -1) {
  37.         int e = edge[e_idx];
  38.         if(e != parent) {
  39.             dfs(e,x,dist+1,t_dist);
  40.             is_leaf = false;
  41.         }
  42.         e_idx = nxt[e_idx];
  43.     }
  44.     if(is_leaf && dist == t_dist) {
  45.         worst.push_back(x);
  46.     }
  47. }
  48.  
  49.  
  50. int get_h(int x, int parent) {
  51.     int ret = 0;
  52.     int e_idx = head[x];
  53.     while(e_idx != -1) {
  54.         int e = edge[e_idx];
  55.         if(e != parent) {
  56.             ret = max(ret,get_h(e,x));
  57.         }
  58.         e_idx = nxt[e_idx];
  59.     }
  60.     return ret+1;
  61. }
  62.  
  63. void rw_add(int r) {
  64.     vector<pair<int,int>> v;
  65.     int e_idx = head[r];
  66.     while(e_idx != -1) {
  67.         int e = edge[e_idx];
  68.         v.push_back({get_h(e,r),e});
  69.         e_idx = nxt[e_idx];
  70.     }
  71.     sort(v.begin(), v.end());
  72.     for(int i = ((int)v.size())-1; i >= 0; i--) {
  73.         if(v.size() < 2 || v[i].first >= v[v.size()-2].first) {
  74.             //cout << "here at v[i].second = " << v[i].second << "\n";
  75.             dfs(v[i].second,r,1,v[i].first);
  76.         }
  77.  
  78.     }
  79. }
  80.  
  81. int main()
  82. {
  83.     ios_base::sync_with_stdio(NULL);
  84.     cin.tie(0);
  85.     //freopen("out.txt","w",stdout);
  86.     int n;
  87.     while(cin >> n) {
  88.         g_sz = 0;
  89.         worst.clear();
  90.         best.clear();
  91.         //best_h = 1e9;
  92.         //worst_h = 0;
  93.         for(int i = 0; i < n; i++) {
  94.             head[i] = -1;
  95.             deg[i] = 0;
  96.             rem[i] = 0;
  97.         }
  98.         for(int i = 0; i < n; i++) {
  99.             int k;
  100.             cin >> k;
  101.             for(int j = 0; j < k; j++) {
  102.                 int b;
  103.                 cin >> b;
  104.                 b--;
  105.                 add_edge(i,b);
  106.             }
  107.         }
  108.         /*for(int i = 0; i < n; i++) {
  109.             int h = get_h(i,-1);
  110.             if(h < best_h) {
  111.                 best_h = h;
  112.                 best.clear();
  113.             }
  114.             if(h == best_h) {
  115.                 best.push_back(i);
  116.             }
  117.             if(h > worst_h) {
  118.                 worst_h = h;
  119.                 worst.clear();
  120.             }
  121.             if(h == worst_h) {
  122.                 worst.push_back(i);
  123.             }
  124.         }*/
  125.         queue<int> curr;
  126.         queue<int> prv;
  127.         for(int i = 0; i < n; i++) {
  128.             if(deg[i] == 1) {
  129.                 curr.push(i);
  130.             }
  131.         }
  132.         while(!curr.empty()) {
  133.             int c_sz = curr.size();
  134.             while(!prv.empty()) {
  135.                 prv.pop();
  136.             }
  137.             for(int i = 0; i < c_sz; i++) {
  138.                 int x = curr.front();
  139.                 //cout << "removing " << x+1 << " from curr " << "\n";
  140.                 curr.pop();
  141.                 prv.push(x);
  142.                 rem[x] = 1;
  143.                 int c_idx = head[x];
  144.                 while(c_idx != -1) {
  145.                     int c = edge[c_idx];
  146.                     deg[c]--;
  147.                     if(!rem[c] && deg[c] == 1) {
  148.                         curr.push(c);
  149.                     }
  150.                     c_idx = nxt[c_idx];
  151.                 }
  152.             }
  153.         }
  154.         //vector<int> best;
  155.         while(!prv.empty()) {
  156.             best.push_back(prv.front());
  157.             prv.pop();
  158.         }
  159.         //dfs(best[0],-1);
  160.         for(int r : best) {
  161.             rw_add(r);
  162.         }
  163.         sort(best.begin(),best.end());
  164.         sort(worst.begin(),worst.end());
  165.         cout << "Best Roots  : ";
  166.         for(int i = 0; i < best.size(); i++) {
  167.             if(i)
  168.                 cout << " ";
  169.             cout << best[i]+1;
  170.         }
  171.         cout << "\nWorst Roots : ";
  172.         for(int i = 0; i < worst.size(); i++) {
  173.             if(i && worst[i] == worst[i-1])
  174.                 continue;
  175.             if(i)
  176.                 cout << " ";
  177.             cout << worst[i]+1;
  178.         }
  179.         cout << "\n";
  180.     }
  181.     return 0;
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement