15972022-11-28 20:32:39kicsiboglarLegtávolabbi leszármazottcpp11Accepted 50/5093ms26040 KiB
// legtavolabbi leszarmazott.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#define ll long long 

using namespace std;

ll n, a, b, i, maxi=-1,pos=0;
struct element
{
    bool seen,f=false;
    vector <ll> sz;
    ll step=0;
};
vector <element> x;

void dfs(ll curr)
{
    x[curr].seen = true;
    for (auto e : x[curr].sz)
    {
        if (!x[e].seen)
        {
            x[e].step = x[curr].step + a;
            dfs(e);
        }
    }
}

int main()
{
    cin >> n;
    x.resize(n + 1);
    for (i = 1; i < n; ++i)
    {
        cin >> a >> b;
        x[a].sz.push_back(b);
        x[b].f = true;
    }
    for (i = 1; i <= n; ++i)
    {
        if (!x[i].f)
        {
            dfs(i);
            break;
        }
    }

    for (i = 1; i <= n; ++i)
    {
        if (x[i].step > maxi)
        {
            maxi = x[i].step;
            pos = i;
        }
    }
    cout << pos;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1872 KiB
2Accepted0/075ms12380 KiB
3Accepted1/12ms2260 KiB
4Accepted3/32ms2460 KiB
5Accepted3/32ms2664 KiB
6Accepted1/12ms2780 KiB
7Accepted1/12ms2872 KiB
8Accepted1/13ms3256 KiB
9Accepted2/283ms14632 KiB
10Accepted3/382ms14744 KiB
11Accepted3/32ms3496 KiB
12Accepted4/486ms20148 KiB
13Accepted4/486ms20256 KiB
14Accepted3/38ms4876 KiB
15Accepted3/383ms19740 KiB
16Accepted3/378ms20760 KiB
17Accepted3/383ms20224 KiB
18Accepted4/461ms15568 KiB
19Accepted4/474ms18960 KiB
20Accepted4/493ms26040 KiB