Submission #163146

# Submission time Handle Problem Language Result Execution time Memory
163146 2019-11-11T14:01:55 Z arnold518 Chase (CEOI17_chase) C++14
100 / 100
2945 ms 347088 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int MAXK = 100;

int N, K;
ll P[MAXN+10], S[MAXN+10], dp1[MAXN+10][MAXK+10], dp2[MAXN+10][MAXK+10], ans;
vector<int> adj[MAXN+10];

void dfs1(int now, int bef)
{
    int i, j;
    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        dfs1(nxt, now);
    }
    for(int nxt : adj[now]) S[now]+=P[nxt];

    for(i=1; i<=K; i++)
    {
        ll val=0;
        for(int nxt : adj[now])
        {
            if(nxt==bef) continue;
            val=max(val, dp1[nxt][i-1]);
            dp1[now][i]=max(dp1[now][i], dp1[nxt][i]);
        }
        dp1[now][i]=max(dp1[now][i], S[now]-P[bef]+val);
    }
}

vector<pll> V[MAXK+10];
void dfs2(int now, int bef)
{
    int i, j;

    for(i=0; i<=K; i++) V[i].clear();
    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        for(i=0; i<=K; i++) V[i].push_back({dp1[nxt][i], nxt});
    }
    for(i=0; i<=K; i++) V[i].push_back({dp2[now][i], now});
    for(i=0; i<=K; i++) sort(V[i].begin(), V[i].end(), greater<pll>());

    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        for(i=1; i<=K; i++)
        {
            for(j=0; j<V[i].size(); j++)
            {
                if(V[i][j].second==nxt) continue;
                dp2[nxt][i]=max(dp2[nxt][i], V[i][j].first);
                break;
            }
            for(j=0; j<V[i-1].size(); j++)
            {
                if(V[i-1][j].second==nxt) continue;
                dp2[nxt][i]=max(dp2[nxt][i], V[i-1][j].first+S[now]-P[nxt]);
                break;
            }
        }
    }

    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        dfs2(nxt, now);
    }
}

void dfs3(int now, int bef)
{
    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        dfs3(nxt, now);
    }

    if(bef==0)
    {
        ans=max(ans, dp1[now][K]);
    }
    else
    {
        ll val=0;
        for(int nxt : adj[now])
        {
            if(nxt==bef) continue;
            val=max(val, dp1[nxt][K]);
        }
        val=max(val, dp2[now][K]); ans=max(ans, val);

        val=0;
        for(int nxt : adj[now])
        {
            if(nxt==bef) continue;
            val=max(val, dp1[nxt][K-1]);
        }
        val=max(val, dp2[now][K-1]);
        val+=S[now];
        ans=max(ans, val);
    }
}

int main()
{
    int i, j, k;

    scanf("%d%d", &N, &K);
    for(i=1; i<=N; i++) scanf("%lld", &P[i]);
    for(i=1; i<N; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    if(K==0) return !printf("0");

    for(i=0; i<=K; i++) dp2[1][i]=-1e17;

    dfs1(1, 0);
    dfs2(1, 0);
    dfs3(1, 0);
    printf("%lld", ans);
}

Compilation message

chase.cpp: In function 'void dfs1(int, int)':
chase.cpp:17:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
chase.cpp: In function 'void dfs2(int, int)':
chase.cpp:57:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(j=0; j<V[i].size(); j++)
                      ~^~~~~~~~~~~~
chase.cpp:63:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(j=0; j<V[i-1].size(); j++)
                      ~^~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:115:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j, k;
            ^
chase.cpp:115:15: warning: unused variable 'k' [-Wunused-variable]
     int i, j, k;
               ^
chase.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:118:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=N; i++) scanf("%lld", &P[i]);
                         ~~~~~^~~~~~~~~~~~~~~
chase.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2684 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2652 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2684 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2652 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 11 ms 4472 KB Output is correct
8 Correct 7 ms 4600 KB Output is correct
9 Correct 8 ms 4600 KB Output is correct
10 Correct 11 ms 4472 KB Output is correct
11 Correct 9 ms 4500 KB Output is correct
12 Correct 7 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 832 ms 185932 KB Output is correct
2 Correct 853 ms 185812 KB Output is correct
3 Correct 2667 ms 345580 KB Output is correct
4 Correct 389 ms 179852 KB Output is correct
5 Correct 929 ms 179976 KB Output is correct
6 Correct 866 ms 180088 KB Output is correct
7 Correct 898 ms 180032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2684 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2652 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 11 ms 4472 KB Output is correct
8 Correct 7 ms 4600 KB Output is correct
9 Correct 8 ms 4600 KB Output is correct
10 Correct 11 ms 4472 KB Output is correct
11 Correct 9 ms 4500 KB Output is correct
12 Correct 7 ms 4472 KB Output is correct
13 Correct 832 ms 185932 KB Output is correct
14 Correct 853 ms 185812 KB Output is correct
15 Correct 2667 ms 345580 KB Output is correct
16 Correct 389 ms 179852 KB Output is correct
17 Correct 929 ms 179976 KB Output is correct
18 Correct 866 ms 180088 KB Output is correct
19 Correct 898 ms 180032 KB Output is correct
20 Correct 850 ms 181832 KB Output is correct
21 Correct 70 ms 8696 KB Output is correct
22 Correct 878 ms 181980 KB Output is correct
23 Correct 363 ms 181752 KB Output is correct
24 Correct 852 ms 182008 KB Output is correct
25 Correct 2945 ms 347088 KB Output is correct
26 Correct 819 ms 188384 KB Output is correct
27 Correct 833 ms 188152 KB Output is correct