44092023-03-27 18:25:291478Legtávolabbi leszármazottcpp17Elfogadva 50/5050ms16512 KiB
#include <bits/stdc++.h>  
#include <random>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
 
int main()
{
    fast_cin();
    //ifstream cin("in.txt");
    
    int n;
    cin >> n;

    vector<vector<int>> nb(n + 1);
    vector<bool> son(n + 1);
    for(int i = 1; i <= n; i++){
        int a;
        cin >> a;
        int b;
        cin >> b;
        son[b] = 1;
        nb[a].push_back(b);
        nb[b].push_back(a);
    }
    int gdad;
    for(int i = 1; i <= n; i++){
        if(!son[i]){
            gdad = i;
        }
    }

    //cout << gdad;

    queue<int> q;
    q.push(gdad);
    vector<int> seen(n + 1);
    vector<int> distance(n + 1);
    int maxD = 0, ans;
    while(!q.empty()){
        int f = q.front();
        q.pop();
        seen[f] = 1;
        for(int x : nb[f]){
            if(!seen[x]){
                distance[x] = distance[f] + 1;
                if(distance[x] > maxD){
                    maxD = distance[x];
                    ans = x;
                }
                q.push(x);
            }
        }
    }

    cout << ans;


    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1828 KiB
2Elfogadva0/043ms13524 KiB
3Elfogadva1/13ms2380 KiB
4Elfogadva3/33ms2560 KiB
5Elfogadva3/33ms2784 KiB
6Elfogadva1/13ms3256 KiB
7Elfogadva1/13ms3236 KiB
8Elfogadva1/13ms3512 KiB
9Elfogadva2/243ms16324 KiB
10Elfogadva3/341ms16260 KiB
11Elfogadva3/33ms3524 KiB
12Elfogadva4/446ms16188 KiB
13Elfogadva4/446ms16256 KiB
14Elfogadva3/37ms5144 KiB
15Elfogadva3/345ms15400 KiB
16Elfogadva3/343ms14752 KiB
17Elfogadva3/346ms15792 KiB
18Elfogadva4/434ms12924 KiB
19Elfogadva4/439ms14436 KiB
20Elfogadva4/450ms16512 KiB