답안 #37051

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
37051 2017-12-20T16:43:21 Z DoanPhuDuc Chase (CEOI17_chase) C++
0 / 100
443 ms 185512 KB
#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n)  { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }

using namespace std;

typedef long long LL;
typedef pair <int, int> II;

const int N = 1e5 + 10;
const int A = 1e2 + 10;
const LL INFL = (LL)1e18;

int n, m;
int a[N], p[N];

LL S[N], C[N];
LL f[N][A], g[N][A];

vector <int> adj[N];

void DFS(int u, int par = -1) {
    S[u] = a[u];
    REP(k, 0, adj[u].size()) {
        int v = adj[u][k]; if (v == par) continue;
        S[u] += a[v];
        p[v] = u;
        DFS(v, u);
    }
}

void DP(int u, int par = -1) {
    f[u][0] = 0;
    f[u][1] = S[u];
    g[u][1] = S[u] - a[u];
    REP(k, 0, adj[u].size()) {
        int v = adj[u][k]; if (v == par) continue;
        DP(v, u);
        FOR(i, 2, m) {
            if (f[v][i - 1] != -1)
                f[u][i] = max(f[u][i], f[v][i - 1] + S[u] - a[v]);
            if (g[v][i - 1] != -1)
                g[u][i] = max(g[u][i], g[v][i - 1] + S[u] - a[v]);
        }
    }
}

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // LOCAL
    scanf("%d%d", &n, &m);
    FOR(i, 1, n) scanf("%d", &a[i]);
    FOR(i, 1, n - 1) {
        int u, v; scanf("%d%d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    memset(f, -1, sizeof f);
    memset(g, -1, sizeof g);
    DFS(1);
    DP(1);
    FOR(u, 1, n) {
        FOR(i, 1, m) if (f[u][i] == -1) f[u][i] = f[u][i - 1];
        FOR(i, 1, m) if (g[u][i] == -1) g[u][i] = g[u][i - 1];
    }
    LL ans = -INFL;
    FOR(u, 1, n) {
        ans = max(ans, g[u][m] + a[p[u]]);
        ans = max(ans, f[u][m] + a[p[u]] - a[u]);
        FOR(i, 1, m) C[i] = -INFL;
        REP(k, 0, adj[u].size()) {
            int v = adj[u][k]; if (v == p[u]) continue;
            FOR(i, 1, m - 1) {
                ans = max(ans, C[i] + f[v][m - 1 - i] + S[u] - a[v] + a[p[u]]);
            }
            FOR(i, 1, m) C[i] = max(C[i], g[v][i] - a[v]);
        }
        FOR(i, 1, m) C[i] = -INFL;
        FOD(k, (int)adj[u].size() - 1, 0) {
            int v = adj[u][k]; if (v == p[u]) continue;
            FOR(i, 1, m - 1) ans = max(ans, C[i] + f[v][m - 1 - i] + S[u] - a[v] + a[p[u]]);
            FOR(i, 1, m) C[i] = max(C[i], g[v][i] - a[v]);
        }

    }
    printf("%lld", ans);
    return 0;
}

Compilation message

chase.cpp: In function 'void DFS(int, int)':
chase.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
chase.cpp:29:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^
chase.cpp: In function 'void DP(int, int)':
chase.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
chase.cpp:41:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^
chase.cpp: In function 'int main()':
chase.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
chase.cpp:78:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
chase.cpp:58:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
                          ^
chase.cpp:59:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     FOR(i, 1, n) scanf("%d", &a[i]);
                                    ^
chase.cpp:61:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u, v; scanf("%d%d", &u, &v);
                                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 178600 KB Output is correct
2 Incorrect 9 ms 178600 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 178600 KB Output is correct
2 Incorrect 9 ms 178600 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 443 ms 185512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 178600 KB Output is correct
2 Incorrect 9 ms 178600 KB Output isn't correct
3 Halted 0 ms 0 KB -