13142022-05-11 18:18:14nkdorka1212Legtávolabbi leszármazottcpp11Accepted 50/5061ms26020 KiB
#include <bits/stdc++.h>

using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")



int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    vector<vector<int>>g(n+1);
    vector<int>befok(n+1,0);
    vector<int>vis(n+1);
    vector<int>dist(n+1,-1);
    for(int i=1;i<=n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        befok[v]++;
    }
    int os=0;
    for(int i=1;i<=n;i++)
    {
        if(befok[i]==0)
        {
            os=i;
        }
    }
    queue<int>q;
    dist[os]=0;
    vis[os]=true;
    q.push(os);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int x:g[v])
        {
            if(!vis[x])
            {
                q.push(x);
                dist[x]=dist[v]+1;
                vis[x]=true;
            }
        }
    }
    int maxi=0;
    int h=0;
    for(int i=1;i<=n;i++)
    {
        if(maxi<dist[i])
        {
            maxi=dist[i];
            h=i;
        }
    }
    cout<<h<<'\n';

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1820 KiB
2Accepted0/039ms12104 KiB
3Accepted1/12ms2892 KiB
4Accepted3/31ms2904 KiB
5Accepted3/31ms2904 KiB
6Accepted1/11ms2912 KiB
7Accepted1/11ms2940 KiB
8Accepted1/12ms2972 KiB
9Accepted2/241ms14272 KiB
10Accepted3/341ms15396 KiB
11Accepted3/31ms5216 KiB
12Accepted4/454ms17220 KiB
13Accepted4/448ms18300 KiB
14Accepted3/37ms8428 KiB
15Accepted3/346ms19556 KiB
16Accepted3/339ms19440 KiB
17Accepted3/354ms21000 KiB
18Accepted4/434ms19576 KiB
19Accepted4/437ms21672 KiB
20Accepted4/461ms26020 KiB