85722024-01-22 11:32:54csaron71Legtávolabbi leszármazottcpp17Accepted 50/5092ms25404 KiB
#include <bits/stdc++.h>

using namespace std;

    vector<vector<int> > graph;
    int n;
    vector<bool> volt;
    vector<int> vege;

void melysegi(int x, int y) {
    if (volt[x]) return;
    volt[x]=true;
    for (int sz : graph[x]) {
        vege[sz]=y+1;
        melysegi(sz, y+1);
    }
}

int main()
{
    cin >> n;
    vector<int> apa(n, -1);
    volt.assign(n, false);
    graph.assign(n, vector<int>());
    vege.assign(n, 0);
    for (int i=0; i<n-1; i++) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        apa[y]=x;
        graph[x].push_back(y);
    }

    int os;
    for (int i=0; i<n; i++) {
        if (apa[i]==-1) {
            os=i;
        }
    }

    melysegi(os, 0);
    int nagy=0;

    for (int i=0; i<n; i++) {
        if (vege[nagy]<vege[i]) {
            nagy=i;
        }
    }

    cout << nagy+1 << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2088 KiB
2Accepted0/075ms11404 KiB
3Accepted1/13ms3184 KiB
4Accepted3/33ms3328 KiB
5Accepted3/33ms3628 KiB
6Accepted1/13ms3612 KiB
7Accepted1/13ms3840 KiB
8Accepted1/13ms4216 KiB
9Accepted2/282ms14028 KiB
10Accepted3/382ms13952 KiB
11Accepted3/33ms4592 KiB
12Accepted4/485ms19300 KiB
13Accepted4/483ms19548 KiB
14Accepted3/39ms5952 KiB
15Accepted3/379ms19552 KiB
16Accepted3/375ms20200 KiB
17Accepted3/379ms19288 KiB
18Accepted4/459ms15396 KiB
19Accepted4/471ms18620 KiB
20Accepted4/492ms25404 KiB