246172026-02-13 07:43:49tamasnagyLegtávolabbi leszármazottcpp17Accepted 50/50100ms10020 KiB
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>>szl;
vector<int>gyt;
int maxi_depth=0;
int maxi_depth_index;
void dfs(int x, int depth=0){
    if(szl[x].empty()){
        if(depth>maxi_depth){
            maxi_depth=depth;
            maxi_depth_index=x;
        }
    }
    else{
        for(int i=0; i<szl[x].size();i++){
            dfs(szl[x][i],depth+1);
        }
    }

}

int main()
{
    int n;
    cin>>n;
    szl.resize(n+1);
    gyt.resize(n+1);
    for(int i=0; i<n-1; i++){
        int a,b;
        cin>>a>>b;
        gyt[b]++;
        szl[a].push_back(b);
    }
    int kcs;
    for(int i=1; i<=n; i++){
        if(gyt[i]==0){
            kcs=i;
            break;
        }
    }
    maxi_depth_index=kcs;
    dfs(kcs);
    cout<<maxi_depth_index;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/074ms4140 KiB
3Accepted1/11ms316 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted1/11ms508 KiB
7Accepted1/11ms316 KiB
8Accepted1/11ms316 KiB
9Accepted2/285ms4688 KiB
10Accepted3/385ms4660 KiB
11Accepted3/31ms316 KiB
12Accepted4/485ms7220 KiB
13Accepted4/486ms7248 KiB
14Accepted3/38ms820 KiB
15Accepted3/3100ms7220 KiB
16Accepted3/393ms7444 KiB
17Accepted3/382ms6964 KiB
18Accepted4/461ms5160 KiB
19Accepted4/472ms6700 KiB
20Accepted4/498ms10020 KiB