Advertisement
El_GEMMY

Tree Diameter #2

Jan 23rd, 2023
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI acos(-1)
  18. #define ceil(a, b) (((a) / (b)) + ((a) % (b) ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define llmax LLONG_MAX
  22. #define llmin LLONG_MIN
  23. #define inf 2000000000
  24. #define nl '\n'
  25. #define ppcnt __builtin_popcount
  26. #define ppcntll __builtin_popcountll
  27. #define clz __builtin_clz
  28. #define clzll __builtin_clzll
  29. #define ctz __builtin_ctz
  30. #define ctzll __builtin_ctzll
  31. #define modulo(a, b, mod) ((((a) % (mod)) + ((b) % (mod))) % (mod))
  32. #define cnte(v, x) count(all(v), (x))
  33. #define mine(v) min_element(all(v))
  34. #define maxe(v) max_element(all(v))
  35. #define updmin(a, b) a = min(a, b)
  36. #define updmax(a, b) a = max(a, b)
  37. #define findmod(x, m) x = ((x) % (m) + (m)) % m
  38. #define getmod(x, m) ((x) % (m) + (m)) % (m)
  39. #define debug(x) cout << "x: " << (x) << nl;
  40. #define debug2(x, y) cout << "x: " << (x) << " y: " << y << nl;
  41. #define ordered_set tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  42. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  43.  
  44. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  45. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  46.  
  47. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  48.     for (auto& [x, y] : v) in >> x >> y;
  49.     return in;
  50. }
  51.  
  52. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  53.     for (T& i : v) in >> i;
  54.     return in;
  55. }
  56.  
  57. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  58.     for (const T& x : v)
  59.         out << x << ' ';
  60.     return out;
  61. }
  62.  
  63. template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
  64.     for(auto& [x, y] : v){
  65.         out << x << ' ' << y << nl;
  66.     }
  67.     return out;
  68. }
  69.  
  70. void Start_Crushing() {
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(nullptr);
  73.     cout.tie(nullptr);
  74. #ifndef ONLINE_JUDGE
  75.     freopen("input.txt", "r", stdin);
  76.     freopen("output.txt", "w", stdout);
  77. #endif
  78. }
  79. int n, maxi;
  80. vector<vector<int>> adj;
  81.  
  82. vector<int> bfs(vector<int>& deg){
  83.     queue<int> q;
  84.     for(int i = 1; i <= n; i++){
  85.         if(deg[i] == 1) {
  86.             q.push(i);
  87.         }
  88.     }
  89.  
  90.     int processed = int(q.size());
  91.     while(processed < n){
  92.         queue<int> nxt_leaves;
  93.         while(not q.empty()){
  94.             int curr = q.front();
  95.             q.pop();
  96.  
  97.             for(auto& nxt : adj[curr]){
  98.                 deg[nxt]--;
  99.                 if(deg[nxt] == 1){
  100.                     nxt_leaves.push(nxt);
  101.                 }
  102.             }
  103.         }
  104.         processed += (int)nxt_leaves.size();
  105.         q = nxt_leaves;
  106.     }
  107.  
  108.     vector<int> ret;
  109.     while(not q.empty())
  110.         ret.emplace_back(q.front()), q.pop();
  111.  
  112.     return ret;
  113. }
  114.  
  115. void dfs_for_worst(int src, int par, int depth, vector<int>& worst){
  116.     if(depth == maxi){
  117.         worst.emplace_back(src);
  118.     }else if(depth > maxi){
  119.         worst.clear();
  120.         worst.emplace_back(src);
  121.         maxi = depth;
  122.     }
  123.  
  124.     for(auto& nxt : adj[src]){
  125.         if(nxt == par)
  126.             continue;
  127.         dfs_for_worst(nxt, src, depth + 1, worst);
  128.     }
  129. }
  130.  
  131. void solve(){
  132.     while(cin >> n){
  133.         adj.assign(n + 5, vector<int>());
  134.         vector<int> deg(n + 5);
  135.         vector<int> from_a(n + 5, inf), from_b(n + 5, inf);
  136.  
  137.         for(int i = 1; i <= n; i++){
  138.             int k; cin >> k;
  139.             deg[i] = k;
  140.             while(k--){
  141.                 int v; cin >> v;
  142.                 adj[i].emplace_back(v);
  143.             }
  144.         }
  145.  
  146.         vector<int> best = bfs(deg);
  147.  
  148.         vector<int> worst;
  149.         for(auto& center : best){
  150.             vector<int> tmp;
  151.             maxi = imin;
  152.             dfs_for_worst(center, -1, 0, tmp);
  153.  
  154.             for(auto& end : tmp){
  155.                 worst.emplace_back(end);
  156.             }
  157.         }
  158.  
  159.         sort(all(best)), sort(all(worst));
  160.  
  161.         cout << "Best Roots  : ";
  162.         for(int i = 0; i < best.size(); i++){
  163.             if(i)
  164.                 cout << ' ';
  165.             cout << best[i];
  166.         }
  167.         cout << nl << "Worst Roots : ";
  168.         for(int i = 0; i < worst.size(); i++){
  169.             if(i)
  170.                 cout << ' ';
  171.             cout << worst[i];
  172.         }
  173.         cout << nl;
  174.     }
  175. }
  176.  
  177. void set_file(string& file_name){
  178.     freopen((file_name + ".in").c_str(), "r", stdin);
  179.     freopen((file_name + ".out").c_str(), "w", stdout);
  180. }
  181.  
  182. int main(){
  183.     Start_Crushing();
  184.  
  185. //    string file_name = "family";
  186. //    set_file(file_name);
  187.  
  188.     int t = 1;
  189. //    /*Multiple test cases?*/ cin >> t;
  190.     while (t--) {
  191.         solve();
  192.         if(!t)
  193.             break;
  194.         cout << nl;
  195.     }
  196.  
  197. //    for(int tc = 1; tc <= t; tc++){
  198. //        cout << "Case #" << tc << ": ";
  199. //        solve();
  200. //        if(tc != t)
  201. //            cout << nl;
  202. //    }
  203.  
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement