253232026-02-19 10:19:20PappMatyasLegtávolabbi leszármazottcpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/082ms4144 KiB
3Elfogadva1/11ms500 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva1/11ms316 KiB
7Elfogadva1/11ms316 KiB
8Elfogadva1/12ms428 KiB
9Elfogadva2/286ms4548 KiB
10Elfogadva3/383ms4580 KiB
11Elfogadva3/31ms316 KiB
12Elfogadva4/490ms6648 KiB
13Elfogadva4/489ms6448 KiB
14Elfogadva3/38ms832 KiB
15Elfogadva3/390ms6376 KiB
16Elfogadva3/379ms6708 KiB
17Elfogadva3/390ms6244 KiB
18Elfogadva4/463ms4764 KiB
19Elfogadva4/475ms5940 KiB
20Elfogadva4/490ms8756 KiB