44662023-03-28 12:10:11UnluckYLegtávolabbi leszármazottcpp11Accepted 50/50104ms21532 KiB
#include <bits/stdc++.h>

using namespace std;


vector<vector<int>> v;
vector<int> d;

void dfs(int x, int a){
    if (d[x] != -1) return;
    d[x] = a + 1;
    for (int i : v[x]){
        dfs(i, d[x]);
    }
}

int main(){

    int n; cin >> n;
    v.assign(n+1, {});
    d.assign(n+1, -1);

    vector<bool> dad(n+1, false);

    for (int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
        dad[b] = true;
    }

    int grand = 0;

    for (int i = 1; i <= n; i++){
        if (!dad[i]){
            grand = i;
            break;
        }
    }


    dfs(grand, -1);

    int m = 0, mi = 0;

    for (int i = 1; i <= n; i++){
        if (d[i] > m){
            m = d[i];
            mi = i;
        }
    }

    cout << mi;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/085ms12756 KiB
3Accepted1/13ms2408 KiB
4Accepted3/33ms2536 KiB
5Accepted3/33ms2556 KiB
6Accepted1/13ms2948 KiB
7Accepted1/13ms2904 KiB
8Accepted1/13ms3140 KiB
9Accepted2/290ms14784 KiB
10Accepted3/389ms14788 KiB
11Accepted3/33ms3312 KiB
12Accepted4/4104ms17844 KiB
13Accepted4/493ms18076 KiB
14Accepted3/39ms4400 KiB
15Accepted3/393ms16596 KiB
16Accepted3/383ms17608 KiB
17Accepted3/390ms17948 KiB
18Accepted4/467ms13876 KiB
19Accepted4/479ms16988 KiB
20Accepted4/496ms21532 KiB