Submission #504812

#TimeUsernameProblemLanguageResultExecution timeMemory
504812neonahtTorrent (COI16_torrent)C++14
0 / 100
170 ms25360 KiB
#include <bits/stdc++.h>
#define st first
#define nd second
#define ll int64_t
using namespace std;
const int SZ=3e5+7;
vector <int> node[SZ];
int dp[SZ],a,b;

void dfs(int v,int p) {
    if(node[v].size()==1 && v!=a && v!=b) return ;

    vector <int> went;
    for(auto x:node[v]) {
        if(x==a || x==b || x==p) continue;
        dfs(x,v);
        went.emplace_back(dp[x]);
    }

    int mx(0);
    sort(went.begin(),went.end(),greater<int>());
    
    for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
    dp[v]=mx;
    return ;
}

int main(void) {
    cin.tie(0)->ios::sync_with_stdio(false);
    int n;
    cin >> n >> a >> b;
    for(int i=1,x,y;i<n;i++) {
        cin >> x >> y;
        node[x].emplace_back(y);
        node[y].emplace_back(x);
    }
    dfs(a,-1);
    dfs(b,-1);
    cout << max(dp[a],dp[b]);
}

Compilation message (stderr)

torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
      |                 ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...