답안 #135048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
135048 2019-07-23T14:59:50 Z fredbr 구슬과 끈 (APIO14_beads) C++17
28 / 100
1000 ms 888 KB
#include <bits/stdc++.h>

using namespace std;

int const maxn = 10101;

struct Edge {
    int u, w;
};

vector<Edge> v[maxn];

int h[maxn];

int tam(int x, int p = -1) {
    h[x] = 1;

    for (Edge e : v[x]) {
        if (e.u == p) continue;

        h[x] = max(h[x], tam(e.u, x) + 1);
    }

    return h[x];
}

int dp[maxn][2];

int solve(int x, int p = -1, int t = 0) {
    if (dp[x][t] >= 0) return dp[x][t];

    if (h[x] < 2) return 0;

    int d = 0;

    if (t == 0) {
        for (Edge e : v[x]) {
            if (e.u == p) continue;

            if (h[e.u] > 1) {
                d += max(solve(e.u, x, 1) + e.w,
                         solve(e.u, x, 0));
            }
        }
    }
    else {
        int sum = 0;
        for (Edge e : v[x]) {
            if (e.u == p) continue;

            if (h[e.u] > 1)
                sum += max(solve(e.u, x, 1) + e.w,
                           solve(e.u, x, 0));
        }

        for (Edge e : v[x]) {
            if (e.u == p) continue;

            if (h[e.u] > 1)
                d = max(d, sum - max(solve(e.u, x, 1) + e.w, solve(e.u, x, 0)) +
                           e.w + solve(e.u, x, 0));
            else
                d = max(d, sum + e.w);
        }
    }

    return dp[x][t] = d;
}

int test(int x) {
    memset(dp, -1, sizeof(dp));
    tam(x);

    return solve(x);
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int n;
    cin >> n;

    for (int i = 0; i < n-1; i++) {
        int a, b, w;
        cin >> a >> b >> w;

        v[a].push_back({b, w});
        v[b].push_back({a, w});
    }

    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans = max(ans, test(i));

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 3 ms 632 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 4 ms 632 KB Output is correct
17 Correct 4 ms 632 KB Output is correct
18 Correct 4 ms 632 KB Output is correct
19 Correct 4 ms 632 KB Output is correct
20 Correct 4 ms 632 KB Output is correct
21 Correct 4 ms 632 KB Output is correct
22 Correct 5 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 3 ms 632 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 4 ms 632 KB Output is correct
17 Correct 4 ms 632 KB Output is correct
18 Correct 4 ms 632 KB Output is correct
19 Correct 4 ms 632 KB Output is correct
20 Correct 4 ms 632 KB Output is correct
21 Correct 4 ms 632 KB Output is correct
22 Correct 5 ms 632 KB Output is correct
23 Execution timed out 1062 ms 888 KB Time limit exceeded
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 3 ms 632 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 4 ms 632 KB Output is correct
17 Correct 4 ms 632 KB Output is correct
18 Correct 4 ms 632 KB Output is correct
19 Correct 4 ms 632 KB Output is correct
20 Correct 4 ms 632 KB Output is correct
21 Correct 4 ms 632 KB Output is correct
22 Correct 5 ms 632 KB Output is correct
23 Execution timed out 1062 ms 888 KB Time limit exceeded
24 Halted 0 ms 0 KB -