제출 #1329344

#제출 시각아이디문제언어결과실행 시간메모리
1329344vahagngPaprike (COI18_paprike)C++20
13 / 100
50 ms27904 KiB
//----------vahagng----------//
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;

// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

void print(long long t) { cerr << t; }
void print(int t) { cerr << t; }
void print(string t) { cerr << t; }
void print(char t) { cerr << t; }
void print(double t) { cerr << t; }
void print(long double t) { cerr << t; }
void print(unsigned long long t) { cerr << t; }

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[], V n) { cerr << "["; for (int i = 0; i < n; i++) { cerr << v[i] << " "; } cerr << "]"; }
template <class T, class V> void print(pair <T, V> p) { cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}"; }
template <class T> void print(vector <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(unordered_set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(multiset <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(map <T, V> v) { cerr << "[ "; for (auto i : v) { print(i); cerr << " "; } cerr << "]"; }

#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ld long double
#define sz(v) v.size()
#define endl '\n'

const ll inf = 2e18, mod = 1e9 + 7, mod2 = 998244353;

void SetIO(string str = "") {
    if (str != "") {
        freopen((str + ".in").c_str(), "r", stdin);
        freopen((str + ".out").c_str(), "w", stdout);
    }
    else {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
}

void FastIO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int N = 1e6 + 10, M = 2e7 + 10;

mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());

ll binpow(ll a, ll b, ll m) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res = (res * a) % m;
        }
        b >>= 1;
        a = (a * a) % m;
    }
    return res;
}

__int128_t gcd(__int128_t a, __int128_t b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

__int128_t lcm(__int128_t a, __int128_t b) {
    return a / gcd(a, b) * b;
}

ll n, k, dp[N], ans, a[N], sum[N], sum_dp[N];

vector<int>adj[N];

void dfs0(int node, int parent) {
    sum[node] = a[node];
    for(auto i : adj[node]){
        if (i == parent) continue;
        dfs0(i, node);
        sum[node] += sum[i];
    }
}

void dfs(int node, int parent) {
    sum_dp[node] = a[node];
    vector<ll> V;
    for (auto i : adj[node]) {
        if (i == parent) continue;
        dfs(i, node);
        dp[node] += dp[i];
        V.push_back(sum_dp[i]);
    }
    sort(all(V));
    int c = 0;
    for (auto i : V) {
        if (sum_dp[node] + i <= k) {
            sum_dp[node] += i;
            c++;
        }
    }
    dp[node] += V.size() - c;
}

void reroot(int node, int parent, int grand_parent) {
    if (node == 1) {
        for (auto i : adj[node]) {
            reroot(i, node, parent);
        }
        return;
    }
    dp[parent] -= dp[node];
    dp[node] += dp[parent];
    vector<ll> V;
    for (auto i : adj[parent]) {
        if (i == grand_parent) continue;
        V.push_back(sum_dp[i]);
    }
    sort(all(V));
    ll S = sum_dp[parent];
    int it = 0;
    while (it < V.size() && S != a[parent]) {
        S -= sum_dp[it];
        if (sum_dp[it] == node) {
            sum_dp[parent] -= sum_dp[node];
            dp[parent]--;
            break;
        }
        it++;
    }
    for (auto i : adj[node]) {
        if (i == parent) continue;
        V.push_back(sum_dp[i]);
    }
    sort(all(V));
    int itr = 0;
    while (itr < V.size() && sum_dp[node] != a[node]) {
        sum_dp[node] -= V[itr++];
    }
    V.clear();
    for (auto i : adj[node]) {
        V.push_back(sum_dp[i]);
    }
    sort(all(V));
    int c = 0;
    for (auto i : V) {
        if (sum_dp[node] + i <= k) {
            sum_dp[node] += i;
            c++;
        }
    }
    dp[node] += V.size() - c;
    ans = min(ans, dp[node]);
    for (auto i : adj[node]) {
        if (i == parent) continue;
        reroot(i, node, parent);
    }
}

void solve(int tc) {
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs0(1, 0);
    dfs(1, 0);
    ans = dp[1];
    reroot(1, 0, 0);
    cout << ans << endl;
}


void precalc() {

}

int main() {
    // SetIO("teamwork");
    FastIO();
    int test_case = 1;
    // cin >> test_case;
    precalc();
    int cnt = 1;
    while (test_case--) {
        solve(cnt++);
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

paprike.cpp: In function 'void SetIO(std::string)':
paprike.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen((str + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
paprike.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen((str + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
paprike.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
paprike.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...