답안 #57889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57889 2018-07-16T13:31:54 Z gabrielsimoes Chase (CEOI17_chase) C++17
30 / 100
2101 ms 256924 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 1e5+10, MAXV = 101;

int n, vMax;
ll p[MAXN];
vector<int> g[MAXN];
int a[MAXN], b[MAXN];

ll dp[MAXN][MAXV][3];
ll f(int curEdge, int v, int dir) {
    if (v == 0) return 0;

    ll &ret = dp[curEdge][v][dir];
    if (ret != -1) return ret;
    ret = 0;

    int cur, prev;
    if (dir == 0) cur = curEdge, prev = -1;
    else if (dir == 1) prev = a[curEdge], cur = b[curEdge];
    else cur = a[curEdge], prev = b[curEdge];

    ll sumAll = 0;
    for (int edge : g[cur]) {
        int nx = a[edge] == cur ? b[edge] : a[edge];
        if (nx != prev) {
            sumAll += p[nx];
        }
    }

    for (int edge : g[cur]) {
        int nx = a[edge] == cur ? b[edge] : a[edge];
        if (nx != prev) {
            int dir = (cur == a[edge] ? 1 : 2);
            ret = max(ret, max(f(edge, v, dir),
                               f(edge, v-1, dir) + sumAll));
        }
    }

    // printf("dp %-2d %-2d %-2d == %lld\n", curEdge, v, dir, ret);
    // printf("dp: cur %-2d prev %-2d v %-2d == %lld\n", cur, prev, v, ret);
    return ret;
}

int main() {
    memset(dp, -1, sizeof(dp));

    scanf("%d%d", &n, &vMax);
    for (int i = 1; i <= n; i++) scanf("%lld", &p[i]);
    for (int i = 1; i < n; i++) {
        scanf("%d%d", &a[i], &b[i]);
        g[a[i]].push_back(i);
        g[b[i]].push_back(i);
    }

    ll ans = 0;
    // for (int i = 1; i <= n; i++) {
    //     ans = max(ans, f(i, vMax, 0));
    // }
    ans = max(ans, f(1, vMax, 0));

    printf("%lld\n", ans);
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &vMax);
     ~~~~~^~~~~~~~~~~~~~~~~~~
chase.cpp:51:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 1; i <= n; i++) scanf("%lld", &p[i]);
                                  ~~~~~^~~~~~~~~~~~~~~
chase.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a[i], &b[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 174 ms 239992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 174 ms 239992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2088 ms 252152 KB Output is correct
2 Correct 2101 ms 253676 KB Output is correct
3 Correct 252 ms 253676 KB Output is correct
4 Correct 279 ms 253676 KB Output is correct
5 Correct 936 ms 253676 KB Output is correct
6 Correct 986 ms 254872 KB Output is correct
7 Correct 978 ms 256924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 174 ms 239992 KB Output isn't correct
2 Halted 0 ms 0 KB -