답안 #96671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96671 2019-02-10T18:48:06 Z keko37 Transport (COCI19_transport) C++14
65 / 130
1000 ms 14492 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long llint;

const int MAXN = 100005;

llint n, root, cc, sol;
int val[MAXN];
llint par[MAXN], siz[MAXN], dp1[MAXN], dp2[MAXN], curr[MAXN], moze[MAXN];
bool cen[MAXN];
vector < pair <int, int> > v[MAXN];
vector <int> q, t;

void dfs (int x, int rod) {
    par[x] = rod;
    siz[x] = 1;
    for (auto it : v[x]) {
        int sus = it.first;
        if (sus == rod || cen[sus]) continue;
        dfs(sus, x);
        siz[x] += siz[sus];
    }
}

int nadi_centroid (int x) {
    int mx = 0, ind = 0;
    if (par[x] != 0) {
        mx = siz[root] - siz[x];
        ind = par[x];
    }
    for (auto it : v[x]) {
        int sus = it.first;
        if (sus == par[x] || cen[sus]) continue;
        if (siz[sus] > mx) {
            mx = siz[sus];
            ind = sus;
        }
    }
    if (mx * 2 > n) return nadi_centroid(ind);
    return x;
}

void calc_dp (int x, int rod) {
    if (rod == 0) {
        dp1[x] = 0;
        dp2[x] = 0;
        curr[x] = val[x];
    }
    if (rod != 0 && dp1[x] == 0) sol++;
    q.push_back(dp1[x]);
    for (auto it : v[x]) {
        int sus = it.first;
        int dist = it.second;
        if (sus == rod || cen[sus]) continue;
        dp1[sus] = max(dp1[x], -(curr[x] - dist));
        curr[sus] = curr[x] - dist + val[sus];
        dp2[sus] = max(0LL, dp2[x]-(val[sus] - dist));
        calc_dp(sus, x);
    }
}

void napravi (int x, int rod) {
    t.push_back(dp1[x]);
    for (auto it : v[x]) {
        int sus = it.first;
        if (sus == rod || cen[sus]) continue;
        napravi(sus, x);
    }
}

void upit (int x, int rod) {
    if (dp2[x] == 0) {
        sol += upper_bound(q.begin(), q.end(), curr[x] - val[cc]) - q.begin() - 1;
        sol -= upper_bound(t.begin(), t.end(), curr[x] - val[cc]) - t.begin() - 1;
    }
    for (auto it : v[x]) {
        int sus = it.first;
        if (sus == rod || cen[sus]) continue;
        upit(sus, x);
    }
}

void decompose (int x) {
    root = x;
    dfs(root, 0);
    cc = nadi_centroid(root);
    cen[cc] = 1;
    q.clear();
    calc_dp(cc, 0);
    sort(q.begin(), q.end());
    for (auto it : v[cc]) {
        int sus = it.first;
        if (cen[sus]) continue;
        t.clear();
        napravi(sus, cc);
        sort(t.begin(), t.end());
        upit(sus, cc);
    }
    int cvor = cc;
    for (auto it : v[cvor]) {
        int sus = it.first;
        if (cen[sus]) continue;
        decompose(sus);
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> val[i];
    }
    for (int i=0; i<n-1; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back(make_pair(b, c));
        v[b].push_back(make_pair(a, c));
    }
    decompose(1);
    cout << sol;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 3064 KB Output is correct
2 Correct 9 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 318 ms 3320 KB Output is correct
2 Correct 402 ms 3448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 8952 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1093 ms 11440 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 14492 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 821 ms 5280 KB Output is correct
2 Correct 38 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 6972 KB Output is correct
2 Execution timed out 1096 ms 6520 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 211 ms 8052 KB Output is correct
2 Correct 171 ms 8436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 310 ms 9840 KB Output is correct
2 Correct 249 ms 9976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1081 ms 11888 KB Time limit exceeded
2 Halted 0 ms 0 KB -