Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define int long long
- #define mp make_pair
- #define pb push_back
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC target("avx2")
- typedef long long ll;
- constexpr int N = (int)2e5+11;
- constexpr int INF = (int)1e18;
- constexpr int md = (int)1e9+7;
- void add(int& a,int b){
- a = (a+b >= md ? a+b-md : a+b);
- }
- void sub(ll& a,ll b){
- a = (a-b < 0 ? a-b+md : a-b);
- }
- mt19937 rnd(time(nullptr));
- int n,x;
- int c[N];
- vector<int> g[N];
- bool is[N];
- int sum[N];
- int go[N];
- void dfs1(int v,int pr = -1){
- is[v] = (v == n);
- for(auto& to : g[v]){
- if(to == pr)
- continue;
- sum[v] += c[to];
- dfs1(to,v);
- is[v] |= is[to];
- if(is[to])
- go[v] = to;
- }
- return;
- }
- bool operator < (const pair<pair<int,int>,int>& A,const pair<pair<int,int>,int>& B){
- __int128 a1 = A.first.first, b1 = A.first.second;
- __int128 a2 = B.first.first, b2 = B.first.second;
- return b1 * (a1 + 1) * a2 * (b2 + 1) > b2 * (a2 + 1) * a1 * (b1 + 1);
- }
- void solve(){
- cin >> n >> x;
- for(int i = 1; i <= n; i++){
- cin >> c[i];
- }
- for(int i = 1; i < n; i++){
- int a,b;
- cin >> a >> b;
- g[a].pb(b);
- g[b].pb(a);
- }
- dfs1(1);
- int ans[n+1];
- memset(ans,0,sizeof ans);
- set<pair<pair<int,int>,int>> q;
- for(int i = 1; i < n; i++){
- if(is[i]){
- q.insert(mp(mp(c[go[i]],sum[i]),go[i]));
- }
- }
- for(int i = 0; i < x; i++){
- auto cur = *q.begin();
- q.erase(q.begin());
- auto[A,B] = cur.first;
- __int128 a = A, b = B;
- int id = cur.second;
- // if(b * (a+1) < a*(b+1))
- // break;
- q.insert(mp(mp(a+1,b+1),id));
- ans[id]++;
- }
- for(int i = 1; i <= n; i++)
- cout << ans[i] << " ";
- return;
- }
- signed main(){
- ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
- // freopen("input.txt","r",stdin);
- // freopen("output.txt","w",stdout);
- int tests = 1;
- // cin >> tests;
- for(int test = 1; test <= tests; test++){
- solve();
- }
- return 0;
- }
- /**
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement