/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 7.977 MiB
#2 Accepted 4ms 9.52 MiB
#3 Accepted 6ms 13.027 MiB
#4 Accepted 8ms 16.523 MiB
#5 Accepted 214ms 65.867 MiB
#6 Accepted 221ms 61.711 MiB
#7 Accepted 108ms 29.996 MiB
#8 Accepted 258ms 48.105 MiB
#9 Accepted 120ms 21.855 MiB
#10 Accepted 494ms 69.953 MiB
#11 Accepted 564ms 78.074 MiB
#12 Accepted 669ms 90.02 MiB
#13 Accepted 471ms 79.418 MiB
#14 Accepted 116ms 15.527 MiB
#15 Accepted 179ms 27.316 MiB
#16 Accepted 424ms 73.566 MiB
#17 Accepted 146ms 17.301 MiB
#18 Accepted 408ms 69.402 MiB
#19 Accepted 226ms 85.098 MiB
#20 Accepted 272ms 94.383 MiB
#21 Accepted 180ms 71.348 MiB
#22 Accepted 17ms 55.836 MiB
#23 Accepted 23ms 80.812 MiB
#24 Accepted 6ms 14.82 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
vector<int> g[200005];
int height[200005] , dp[101][200005];
int n,k;

#define fr freopen("input19.txt","r",stdin);
ofstream file("output19.txt");

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()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //fr
    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);
    //for(int i=1;i<=n;i++) cout<<i<<" "<<height[i]<<endl;
    dpontree(1,0);
    cout << dp[k][1] << endl;
    //file<<dp[k][1]<<endl;
    //for(int i=0;i<=k;i++) cout<<i<<" "<<dp[i][1]<<endl;
}

Information

Submit By
Type
Submission
Problem
P1111 Thakurs tree game
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-09 14:49:45
Judged At
2024-11-07 09:24:38
Judged By
Score
100
Total Time
669ms
Peak Memory
94.383 MiB