Advertisement
welleyth

M. SEERC 2022

Dec 10th, 2022
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8. #define int long long
  9. #define mp make_pair
  10. #define pb push_back
  11.  
  12. #pragma GCC optimize("Ofast")
  13. #pragma GCC optimize("unroll-loops")
  14. #pragma GCC target("avx2")
  15.  
  16. typedef long long ll;
  17.  
  18. constexpr int N = (int)2e5+11;
  19. constexpr int INF = (int)1e18;
  20. constexpr int md = (int)1e9+7;
  21.  
  22. void add(int& a,int b){
  23.     a = (a+b >= md ? a+b-md : a+b);
  24. }
  25. void sub(ll& a,ll b){
  26.     a = (a-b < 0 ? a-b+md : a-b);
  27. }
  28.  
  29. mt19937 rnd(time(nullptr));
  30.  
  31. int n,x;
  32. int c[N];
  33. vector<int> g[N];
  34. bool is[N];
  35. int sum[N];
  36. int go[N];
  37.  
  38. void dfs1(int v,int pr = -1){
  39.     is[v] = (v == n);
  40.     for(auto& to : g[v]){
  41.         if(to == pr)
  42.             continue;
  43.         sum[v] += c[to];
  44.         dfs1(to,v);
  45.         is[v] |= is[to];
  46.         if(is[to])
  47.             go[v] = to;
  48.     }
  49.     return;
  50. }
  51.  
  52. bool operator < (const pair<pair<int,int>,int>& A,const pair<pair<int,int>,int>& B){
  53.     __int128 a1 = A.first.first, b1 = A.first.second;
  54.     __int128 a2 = B.first.first, b2 = B.first.second;
  55.     return b1 * (a1 + 1) * a2 * (b2 + 1) > b2 * (a2 + 1) * a1 * (b1 + 1);
  56. }
  57.  
  58. void solve(){
  59.     cin >> n >> x;
  60.  
  61.     for(int i = 1; i <= n; i++){
  62.         cin >> c[i];
  63.     }
  64.  
  65.     for(int i = 1; i < n; i++){
  66.         int a,b;
  67.         cin >> a >> b;
  68.         g[a].pb(b);
  69.         g[b].pb(a);
  70.     }
  71.  
  72.     dfs1(1);
  73.     int ans[n+1];
  74.     memset(ans,0,sizeof ans);
  75.  
  76.     set<pair<pair<int,int>,int>> q;
  77.     for(int i = 1; i < n; i++){
  78.         if(is[i]){
  79.             q.insert(mp(mp(c[go[i]],sum[i]),go[i]));
  80.         }
  81.     }
  82.     for(int i = 0; i < x; i++){
  83.         auto cur = *q.begin();
  84.         q.erase(q.begin());
  85.         auto[A,B] = cur.first;
  86.         __int128 a = A, b = B;
  87.         int id = cur.second;
  88. //        if(b * (a+1) < a*(b+1))
  89. //            break;
  90.         q.insert(mp(mp(a+1,b+1),id));
  91.         ans[id]++;
  92.     }
  93.  
  94.     for(int i = 1; i <= n; i++)
  95.         cout << ans[i] << " ";
  96.  
  97.     return;
  98. }
  99.  
  100. signed main(){
  101.     ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
  102. //    freopen("input.txt","r",stdin);
  103. //    freopen("output.txt","w",stdout);
  104.     int tests = 1;
  105. //    cin >> tests;
  106.     for(int test = 1; test <= tests; test++){
  107.         solve();
  108.     }
  109.     return 0;
  110. }
  111. /**
  112. **/
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement