/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 5ms 5.023 MiB
#3 Accepted 6ms 5.02 MiB
#4 Accepted 7ms 5.066 MiB
#5 Accepted 252ms 63.18 MiB
#6 Accepted 232ms 60.895 MiB
#7 Accepted 179ms 28.945 MiB
#8 Accepted 287ms 45.656 MiB
#9 Accepted 195ms 21.102 MiB
#10 Accepted 524ms 67.84 MiB
#11 Accepted 581ms 75.504 MiB
#12 Accepted 685ms 90.043 MiB
#13 Accepted 611ms 77.188 MiB
#14 Accepted 196ms 12.867 MiB
#15 Accepted 275ms 25.969 MiB
#16 Accepted 533ms 71.066 MiB
#17 Accepted 234ms 15.215 MiB
#18 Accepted 514ms 67.254 MiB
#19 Accepted 302ms 82.926 MiB
#20 Accepted 317ms 90.59 MiB
#21 Accepted 256ms 69.906 MiB
#22 Accepted 5ms 5.336 MiB
#23 Accepted 6ms 5.52 MiB
#24 Accepted 8ms 5.039 MiB

Code

#include<bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int height[200005] , dp[101][200005];
int n,k;

void dfs(int node,int par)
{
    height[node] = 0;
    for(int e:g[node])
    {
        if(e==par) continue;
        dfs(e,node);
    }
    int mx = 0;
    for(int e:g[node])
    {
        if(e==par) continue;
        mx = max(mx , height[e]);
    }
    height[node] = mx+1;
}

void dpontree(int node,int par)
{
    int mxh=0,mxh2=0;
    for(auto e:g[node])
    {
        if(e==par) continue;
        if(height[e] >= mxh)
        {
            mxh2 = mxh;
            mxh = height[e];
        }
        else if(height[e] > mxh2) mxh2 = height[e];
    }
    
    for(auto e:g[node])
    {
        if(e==par) continue;
        dpontree(e,node);
    }

    for(int i=0;i<=k;i++)
    {
        dp[i][node] = 1;
        for(auto e:g[node])
        {
            if(e==par) continue;
            if(height[e] == mxh)
            {
                dp[i][node] = max(dp[i][node] , min(mxh2,i) + dp[max(0,i-mxh2)][e] + 1);
            }
            else 
            {
                dp[i][node] = max(dp[i][node] , min(mxh,i) + dp[max(0,i-mxh)][e] + 1);
            }
        }
    }
}


int main()
{
    cin >> n >> k;
    for(int i=0;i<n-1;i++)
    {
        int u,v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1,0);
    dpontree(1,0);
    cout << dp[k][1] << endl;
}


Information

Submit By
Type
Submission
Problem
P1111 G. Thakurs tree game
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 22:01:41
Judged At
2025-07-15 22:01:41
Judged By
Score
100
Total Time
685ms
Peak Memory
90.59 MiB