Submission #1117833

#TimeUsernameProblemLanguageResultExecution timeMemory
1117833vjudge1Paprike (COI18_paprike)C++17
0 / 100
210 ms22088 KiB
/*
#ifndef ONLINE_JUDGE
#include "AkbarKING.h" // aska geldi akbarking.h
#else
#define debug(...)
#define debugArr(...)
#define debugG(...)
#endif
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>

#define int long long
#define $AzH_TxdmN$ ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")

#define ep emplace_back
#define pb push_back
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()

#define heap pii,vector<pii>,less<pii>
using namespace std;
using namespace __gnu_pbds;

template <typename T>
using __indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using __indexed_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int sz = 3e5+9;
const int LOG = 63;
const int MOD = 1e9+7;
const int INF = 1e18;
vector<int>v[sz];
vector<int>leaves;
bool used[sz];
int a[sz],dp[sz];

int n,k;

void dfs(int node)
{
    if (used[node])         return;
    used[node] = 1;
    if (v[node].size() == 1)
    {
        leaves.ep(node);
    }
    for (int to : v[node])
    {
        if (!used[to])
        {
            dfs(to);
        }
    }
}

int res;
void djk()
{
    priority_queue<heap>pq;
    for (int i = 0; i <= n; ++i)
    {
        dp[i] = used[i] = 0;
    }
    for (const int &leaf : leaves)
    {
        pq.push({a[leaf],leaf});
        used[leaf] = 1;
    }
    int id,w;
    while(!pq.empty())
    {
        id = pq.top().second;
        w = pq.top().first;
        pq.pop();
        bool flag = 1;
        for(auto to : v[id])
        {
            if (!used[to])
            {
                flag = 0;
                used[to] = 1;
                if (w+a[to] > k)
                {
                    pq.push({a[to],to});
                    ++res;
                    cerr << id << '\n';
                    break;
                }
                else
                {
                    pq.push({w+a[to],to});
                }
            }
        }
        if (flag)
        {
            ++res;
            // cerr << id << endl;
        }
    }
}

void solve()
{
    cin>>n>>k;
    for (int i = 1; i <= n; ++i)
    {
        cin>>a[i];
    }
    for (int i = 1,x,y; i < n; ++i)
    {
        cin>>x>>y;
        v[x].ep(y);
        v[y].ep(x);
    }
    for (int i = 1; i <= n; ++i)
    {
        if (!used[i])
        {
            dfs(i);
        }
    }
    djk();
    cout<<res<<'\n';
}

signed main()
{
    $AzH_TxdmN$
    //freopen("kangaroo.in", "r", stdin);
    //freopen("kangaroo.out", "w", stdout);
    int t = 1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...