제출 #1363981

#제출 시각아이디문제언어결과실행 시간메모리
1363981madamadam3Petrol stations (CEOI24_stations)C++20
18 / 100
3593 ms9940 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long int

const int INF = 4e18;
template<class T> bool chmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
template<class T> bool chmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using vvpi = vector<vector<pi>>;

#define bg(x) (x).begin()
#define en(x) (x).end()
#define all(x) bg(x), en(x)
#define sz(x) (int)((x).size())
#define rep(i,a,b) for (int i = a; i < b; i++)
#define rev(i,a,b) for (int i = a; i >= b; i--)

int n, k;
vvpi G; vi ans;

signed main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> k;
    G.resize(n); for (int i = 0; i < n-1; i++) {
        int u, v, w; cin >> u >> v >> w;
        G[u].push_back({v,w}); G[v].push_back({u,w});
    }
    ans.resize(n);

    auto dfs = [&](auto &&self, int u, int p, vi &s)->int {
        s[u] = 1;
        for (auto [v,w] : G[u]) {
            if (v == p) continue;
            s[u] += self(self, v, u, s);
        }
        return s[u];
    };

    rep(root, 0, n) {
        queue<pi> q; q.push({root, k});
        vi par(n, -1); vi s(n);
        dfs(dfs, root, root, s);
        while (!q.empty()) {
            auto [u,c] = q.front(); q.pop();

            for (auto [v,w] : G[u]) {
                if (v == par[u]) continue;
                int d = c-w;
                if (d < 0) {
                    ans[u] += s[v], d = k-w;
                }
                par[v] = u;
                q.push({v, d});
            }
        }
    }

    for (auto &x : ans) cout << x << "\n";
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…