Submission #759239

#TimeUsernameProblemLanguageResultExecution timeMemory
759239Charizard2021Paprike (COI18_paprike)C++17
0 / 100
31 ms11540 KiB
#include<bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(false);; cin.tie(NULL) template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;}; template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;} template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;} template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;}; template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;}; template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;}; void selfMax(int& a, int b){ a = max(a, b); } void selfMin(int& a, int b){ a = min(a, b); } /* Use DSU dsu(N); */ struct DSU { vector<int> e; DSU(int N) { e = vector<int>(N, -1); } // get representive component (uses path compression) int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } bool same_set(int a, int b) { return get(a) == get(b); } int size(int x) { return -e[get(x)]; } bool unite(int x, int y) { // union by size x = get(x), y = get(y); if (x == y) return false; if (e[x] > e[y]) swap(x, y); e[x] += e[y]; e[y] = x; return true; } }; /* Run with Bit<int> BIT */ template <class T> class BIT { private: int size; vector<T> bit; vector<T> arr; public: BIT(int size) : size(size), bit(size + 1), arr(size) {} void set(int ind, int val) { add(ind, val - arr[ind]); } void add(int ind, int val) { arr[ind] += val; ind++; for (; ind <= size; ind += ind & -ind) { bit[ind] += val; } } T pref_sum(int ind) { ind++; T total = 0; for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; } return total; } }; /* Change Comb for specific Seg tree probs, but run with Seg<int> Seg; */ template<class T> struct Seg { const T ID = 1e18; T comb(T a, T b) { return min(a,b); } int n; vector<T> seg; void init(int _n) { n = _n; seg.assign(2*n,ID); } void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); } void upd(int p, T val) { seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); } T query(int l, int r) { T ra = ID, rb = ID; for (l += n, r += n+1; l < r; l /= 2, r /= 2) { if (l&1) ra = comb(ra,seg[l++]); if (r&1) rb = comb(seg[--r],rb); } return comb(ra,rb); } }; const int N = 100001; vector<int> adj[N]; int parents[N]; int levels[N]; vector<int> levelsToIdx[N]; void dfs(int u, int p){ parents[u] = p; for(int v : adj[u]){ if(v != p){ levels[v] = levels[u] + 1; dfs(v, u); } } } int main(){ int n, k; cin >> n >> k; vector<int> v(n); cin >> v; vector<int> order(n); for(int i =0 ; i < n; i++){ order[i] = i; } for(int i = 0; i < n - 1; i++){ int a, b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } dfs(0, -1); for(int i = 0; i < n; i++){ levelsToIdx[levels[i]].push_back(i); } int ans =0; for(int i = n - 1; i > 0; i--){ sort(levelsToIdx[i].begin(), levelsToIdx[i].end(), [&](int a, int b){ return v[a] < v[b]; }); for(int u : levelsToIdx[i]){ if(v[u] + v[parents[u]] <= k){ v[parents[u]] += v[u]; } else{ ans++; } } } cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...