Advertisement
i_love_rao_khushboo

D. Choosing Capital for Treeland

Aug 14th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. // Problem: D. Choosing Capital for Treeland
  2. // Contest: Codeforces - Codeforces Round #135 (Div. 2)
  3. // URL: https://codeforces.com/contest/219/problem/D
  4. // Memory Limit: 256 MB
  5. // Time Limit: 3000 ms
  6. // Parsed on: 14-08-2021 16:23:09 IST (UTC+05:30)
  7. // Author: Kapil Choudhary
  8. // ********************************************************************
  9. // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
  10. // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
  11. // ********************************************************************
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define ull unsigned long long
  19. #define pb push_back
  20. #define ppb pop_back
  21. #define mp make_pair
  22. #define F first
  23. #define S second
  24. #define PI 3.1415926535897932384626
  25. #define sz(x) ((int)(x).size())
  26.  
  27. typedef pair<int, int> pii;
  28. typedef pair<ll, ll> pll;
  29. typedef vector<int> vi;
  30. typedef vector<ll> vll;
  31. typedef vector<ull> vull;
  32. typedef vector<bool> vb;
  33. typedef vector<char> vc;
  34. typedef vector<string> vs;
  35. typedef vector<pii> vpii;
  36. typedef vector<pll> vpll;
  37. typedef vector<vi> vvi;
  38. typedef vector<vll> vvll;
  39. typedef vector<vull> vvull;
  40. typedef vector<vb> vvb;
  41. typedef vector<vc> vvc;
  42. typedef vector<vs> vvs;
  43.  
  44. /************************************************** DEBUGGER *******************************************************************************************************/
  45.  
  46. #ifndef ONLINE_JUDGE
  47. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  48. #else
  49. #define debug(x)
  50. #endif
  51.  
  52. void _print(ll t) { cerr << t; }
  53. void _print(int t) { cerr << t; }
  54. void _print(string t) { cerr << t; }
  55. void _print(char t) { cerr << t; }
  56. void _print(ld t) { cerr << t; }
  57. void _print(double t) { cerr << t; }
  58. void _print(ull t) { cerr << t; }
  59.  
  60. template <class T, class V> void _print(pair <T, V> p);
  61. template <class T> void _print(vector <T> v);
  62. template <class T> void _print(vector <vector<T>> v);
  63. template <class T> void _print(set <T> v);
  64. template <class T, class V> void _print(map <T, V> v);
  65. template <class T> void _print(multiset <T> v);
  66. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  67. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  68. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  69. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  70. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  71. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  72.  
  73. /*******************************************************************************************************************************************************************/
  74.  
  75. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  76. int rng(int lim) {
  77.     uniform_int_distribution<int> uid(0,lim-1);
  78.     return uid(rang);
  79. }
  80.  
  81. const int INF = 0x3f3f3f3f;
  82. const int mod = 1e9+7;
  83.  
  84. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  85.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  86.                          
  87. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  88. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  89.  
  90. /******************************************************************************************************************************/
  91.  
  92. vector<vpii> g;
  93.  
  94. // dp_down[i] = #edges which are directed downwards in the path from 1 to i
  95. // dp_up[i] = #edges which are directed upwards in the path from 1 to i
  96. vi dp_down, dp_up;
  97.  
  98. // n = #nodes, upwards = total #edges directed up
  99. int n, upwards;
  100.  
  101. void dfs(int cur, int par) {
  102.     for(auto x: g[cur]) {
  103.         int node = x.F, dir = x.S;
  104.         if(node == par) continue;
  105.        
  106.         if(dir == 1) dp_down[node] = dp_down[cur] + 1;
  107.         else dp_down[node] = dp_down[cur];
  108.        
  109.         if(dir == -1) dp_up[node] = dp_up[cur] + 1;
  110.         else dp_up[node] = dp_up[cur];
  111.        
  112.         if(dir == -1) upwards += 1;
  113.        
  114.         // calculate value for child nodes
  115.         dfs(node, cur);
  116.     }
  117. }
  118.  
  119. // https://www.youtube.com/watch?v=lrWshOTKaec&list=PLzVLIdIx9dQxCKaiktxELrtXtnItgAAIr&index=5
  120. // https://codeforces.com/blog/entry/5160
  121. void solve()
  122. {
  123.     cin >> n;
  124.    
  125.     g.clear(); g.resize(n + 1);
  126.     dp_down.clear(); dp_down.resize(n + 1, 0);
  127.     dp_up.clear(); dp_up.resize(n + 1, 0);
  128.     upwards = 0;
  129.    
  130.     for(int i = 1; i < n; i++) {
  131.         int x, y; cin >> x >> y;
  132.        
  133.         // 1 is for downward edge & -1 is for upward edge
  134.         g[x].pb({y, 1});
  135.         g[y].pb({x, -1});
  136.     }
  137.    
  138.     dfs(1, 0);
  139.    
  140.     vi val(n + 1, 0);
  141.     for(int i = 1; i <= n; i++) {
  142.         val[i] = dp_down[i] + (upwards - dp_up[i]);
  143.     }
  144.    
  145.     int mn = *min_element(val.begin() + 1, val.end());
  146.    
  147.     cout << mn << "\n";
  148.     for(int i = 1; i <= n; i++) {
  149.         if(val[i] == mn) cout << i << " ";
  150.     }
  151.    
  152.     cout << "\n";
  153. }
  154.  
  155. int main()
  156. {
  157.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  158.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  159.  
  160.     // #ifndef ONLINE_JUDGE
  161.     //     freopen("input.txt", "r", stdin);
  162.     //     freopen("output.txt", "w", stdout);
  163.     // #endif
  164.    
  165.     // #ifndef ONLINE_JUDGE
  166.     //      freopen("error.txt", "w", stderr);
  167.     // #endif
  168.    
  169.     int t = 1;
  170.     // int test = 1;
  171.     // cin >> t;
  172.     while(t--) {
  173.         // cout << "Case #" << test++ << ": ";
  174.         solve();
  175.     }
  176.  
  177.     return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement