Dang_Quan_10_Tin

egalitarianism3

Aug 8th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <set>
  4. #include <vector>
  5. #include <cstring>
  6. #define task "egalitarianism3"
  7. using namespace std;
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. const int N = 50 + 2;
  12. vector<pair<int, int>> adj[N];
  13. int n, chosen, val, par[N], dp[N];
  14. const int lim = 49000;
  15. int ans = 0, cnt[N][lim + 2], tmp[lim + 2];
  16.  
  17. void Read(){
  18.     cin >> n;
  19.     for(int i = 1; i < n; ++i){
  20.         int u, v, w;
  21.         cin >> u >> v >> w;
  22.         adj[u].push_back({v, w});
  23.         adj[v].push_back({u, w});
  24.     }
  25. }
  26.  
  27. void dfs(int v){
  28.     //cout << v << ": ";
  29.     for(auto i : adj[v])
  30.         if(i.first != par[v]){
  31.             par[i.first] = v;
  32.             dp[i.first] = i.second;
  33.             dfs(i.first);
  34.             for(int j = i.second; j <= lim; ++j)
  35.                 if(cnt[i.first][j - i.second])
  36.                     ++cnt[v][j];
  37.         }
  38.     memset(tmp, 0, sizeof tmp);
  39.     int sum = 0, piv = v;
  40.     while(piv != 0){
  41.         ++tmp[sum];
  42.         sum += dp[piv];
  43.         piv = par[piv];
  44.     }
  45.     for(int j = 0; j <= lim; ++j)
  46.         if(ans < cnt[v][j] + tmp[j]){
  47.             ans = cnt[v][j] + tmp[j];
  48.             chosen = v;
  49.             val = j;
  50.         }
  51.     cnt[v][0] = 1;
  52.     //cout << ans << ' ' << chosen << " " << val << "\n";
  53. }
  54.  
  55. set<int> res;
  56.  
  57. bool dfs_(int v, int sum){
  58.     if(sum == 0){
  59.         res.insert(v);
  60.         return true;
  61.     }
  62.     if(sum < 0)
  63.         return false;
  64.     for(auto i : adj[v])
  65.         if(i.first != par[v])
  66.             if(dfs_(i.first, sum - i.second))
  67.                 return true;
  68.     return false;
  69. }
  70.  
  71. void Solve(){
  72.     dfs(1);
  73.     if(ans > 1 || n == 1){
  74.         cout << ans << "\n";
  75.         //cout << chosen << " " << val << "\n";
  76.         int sum = 0, piv = chosen;
  77.         while(piv != 0 && sum <= val){
  78.             if(sum == val)
  79.                 res.insert(piv);
  80.             sum += dp[piv];
  81.             piv = par[piv];
  82.         }
  83.         for(auto i : adj[chosen])
  84.             if(i.first != par[chosen])
  85.                 dfs_(i.first, val - i.second);
  86.         for(auto i : res)
  87.             cout << i << " ";
  88.     }
  89.     else{
  90.         cout << "2\n1 2";
  91.     }
  92. }
  93.  
  94. int32_t main(){
  95.     ios::sync_with_stdio(0);
  96.     cin.tie(0);
  97.     cout.tie(0); if(fopen(task".INP", "r"))
  98.     freopen(task".INP", "r", stdin),
  99.     freopen(task".OUT", "w", stdout);
  100.     Read();
  101.     Solve();
  102. }
  103. /*
  104. 8
  105. 1 2 1
  106. 2 3 2
  107. 3 4 2
  108. 4 5 3
  109. 5 6 3
  110. 6 7 2
  111. 7 8 1
  112. */
Advertisement
Add Comment
Please, Sign In to add comment