Submission #578558

# Submission time Handle Problem Language Result Execution time Memory
578558 2022-06-17T09:23:27 Z andrei_boaca Chase (CEOI17_chase) C++14
100 / 100
574 ms 263236 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int need=2;
ll n,v,p[100005],par[100005],dp[100005][105];
vector<ll> muchii[100005];
ll ans=0,s[100005];
ll MAXROOT=1;
bool use[100005];
ll max1[100005][105],max2[100005][105];
void dfs(int nod)
{
    for(int i=1;i<=v;i++)
        dp[nod][i]=0;
    ll suma=0;
    int cnt=0;
    for(int i:muchii[nod])
        if(i!=par[nod])
        {
            suma+=p[i];
            par[i]=nod;
            cnt++;
            dfs(i);
            for(int j=1;j<=v;j++)
            {
                if(dp[i][j]>max1[nod][j])
                {
                    max2[nod][j]=max1[nod][j];
                    max1[nod][j]=dp[i][j];
                }
                else
                    if(dp[i][j]>max2[nod][j])
                        max2[nod][j]=dp[i][j];
            }
        }
    s[nod]=suma;
    for(int j=1;j<=v;j++)
    {
        dp[nod][j]=dp[nod][j-1];
        for(int i:muchii[nod])
            if(i!=par[nod])
            {
                dp[nod][j]=max(dp[nod][j],dp[i][j]);
                dp[nod][j]=max(dp[nod][j],dp[i][j-1]+s[nod]);
            }
    }
}
void calcdp(int nod,int j)
{
    dp[nod][j]=0;
    ll x=max1[nod][j];
    ll y=s[nod];
    y=max1[nod][j-1]+s[nod];
    dp[nod][j]=max(x,y);
}
void reroot(int nod,int fiu)
{
    s[nod]-=p[fiu];
    s[fiu]+=p[nod];
    par[nod]=fiu;
    par[fiu]=0;
    for(int j=1;j<=v;j++)
    {
        if(dp[fiu][j]==max1[nod][j])
        {
            max1[nod][j]=max2[nod][j];
            max2[nod][j]=0;
        }
        else
            if(dp[fiu][j]==max2[nod][j])
                max2[nod][j]=0;
    }
    for(int j=1;j<=v;j++)
        calcdp(nod,j);
    for(int j=1;j<=v;j++)
    {
        if(dp[nod][j]>max1[fiu][j])
        {
            max2[fiu][j]=max1[fiu][j];
            max1[fiu][j]=dp[nod][j];
        }
        else
            if(dp[nod][j]>max2[fiu][j])
                max2[fiu][j]=dp[nod][j];
    }
    for(int j=1;j<=v;j++)
        calcdp(fiu,j);
}
void calc(int nod)
{
    use[nod]=1;
    ans=max(ans,dp[nod][v]);
    for(int i:muchii[nod])
        if(i!=par[nod]&&!use[i])
        {
            reroot(nod,i);
            calc(i);
            reroot(i,nod);
        }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>v;
    for(int i=1;i<=n;i++)
        cin>>p[i];
    for(int i=1;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
    }
    dfs(1);
    calc(1);
    cout<<ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 4 ms 5244 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 521 ms 260796 KB Output is correct
2 Correct 474 ms 260856 KB Output is correct
3 Correct 455 ms 255684 KB Output is correct
4 Correct 210 ms 255436 KB Output is correct
5 Correct 512 ms 255420 KB Output is correct
6 Correct 546 ms 255412 KB Output is correct
7 Correct 521 ms 255540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 4 ms 5244 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 521 ms 260796 KB Output is correct
14 Correct 474 ms 260856 KB Output is correct
15 Correct 455 ms 255684 KB Output is correct
16 Correct 210 ms 255436 KB Output is correct
17 Correct 512 ms 255420 KB Output is correct
18 Correct 546 ms 255412 KB Output is correct
19 Correct 521 ms 255540 KB Output is correct
20 Correct 536 ms 255308 KB Output is correct
21 Correct 71 ms 11092 KB Output is correct
22 Correct 574 ms 257512 KB Output is correct
23 Correct 201 ms 257464 KB Output is correct
24 Correct 553 ms 257600 KB Output is correct
25 Correct 461 ms 257364 KB Output is correct
26 Correct 481 ms 263236 KB Output is correct
27 Correct 533 ms 263108 KB Output is correct