42582023-03-19 20:01:28nkdorka1212Legtávolabbi leszármazottcpp17Accepted 50/5086ms16076 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<int>befok(n+1,0);
    vector<vector<int>>g(n+1);
    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        befok[b]++;
    }
    int gyoker=0;
    for(int i=1;i<=n;i++)
    {
        if(befok[i]==0)
        {
            gyoker=i;
        }
    }
    vector<int>dist(n+1,0);
    queue<int>q;
    q.push(gyoker);
    dist[gyoker]=0;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int x:g[v])
        {
            dist[x]=dist[v]+1;
            q.push(x);
        }
    }
    int maxh=1;
    for(int i=1;i<=n;i++)
    {
        if(dist[maxh]<dist[i])
        {
            maxh=i;
        }
    }
    cout<<maxh<<"\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1808 KiB
2Accepted0/071ms10368 KiB
3Accepted1/13ms2220 KiB
4Accepted3/33ms2452 KiB
5Accepted3/33ms2704 KiB
6Accepted1/13ms2876 KiB
7Accepted1/13ms3072 KiB
8Accepted1/13ms3148 KiB
9Accepted2/279ms12284 KiB
10Accepted3/378ms12284 KiB
11Accepted3/33ms3184 KiB
12Accepted4/481ms13692 KiB
13Accepted4/479ms13620 KiB
14Accepted3/38ms4388 KiB
15Accepted3/374ms14212 KiB
16Accepted3/371ms13084 KiB
17Accepted3/376ms13196 KiB
18Accepted4/457ms11504 KiB
19Accepted4/468ms12748 KiB
20Accepted4/486ms16076 KiB