253232026-02-19 10:19:20PappMatyasLegtávolabbi leszármazottcpp17Accepted 50/5090ms8756 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> above;
vector<vector<int>> below;
int maxDistance = -1;
int maxSave = -1;

static void GoDown(int index, int distance)
{
    if(distance > maxDistance)
    {
        maxDistance = distance;
        maxSave = index + 1;
    }
    int sz = below[index].size();

    for(int i = 0; i < sz; i++)
    {
        GoDown(below[index][i], distance + 1);
    }
}

int main()
{
    int n;
    cin >> n;

    above.resize(n);
    fill(above.begin(), above.end(), -1);
    below.resize(n);
    for(int i = 0; i < n - 1; i++)
    {
        int from, to;
        cin >> from >> to;
        from--;
        to--;
        above[to] = from;
        below[from].push_back(to);
    }

    int start;
    for(int i = 0; i < n - 1; i++)
    {
        if(above[i] == -1)
        {
            start = i;
            break;
        }
    }

    GoDown(start, 0);

    cout << maxSave;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/082ms4144 KiB
3Accepted1/11ms500 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted1/11ms316 KiB
7Accepted1/11ms316 KiB
8Accepted1/12ms428 KiB
9Accepted2/286ms4548 KiB
10Accepted3/383ms4580 KiB
11Accepted3/31ms316 KiB
12Accepted4/490ms6648 KiB
13Accepted4/489ms6448 KiB
14Accepted3/38ms832 KiB
15Accepted3/390ms6376 KiB
16Accepted3/379ms6708 KiB
17Accepted3/390ms6244 KiB
18Accepted4/463ms4764 KiB
19Accepted4/475ms5940 KiB
20Accepted4/490ms8756 KiB