44092023-03-27 18:25:291478Legtávolabbi leszármazottcpp17Accepted 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;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/043ms13524 KiB
3Accepted1/13ms2380 KiB
4Accepted3/33ms2560 KiB
5Accepted3/33ms2784 KiB
6Accepted1/13ms3256 KiB
7Accepted1/13ms3236 KiB
8Accepted1/13ms3512 KiB
9Accepted2/243ms16324 KiB
10Accepted3/341ms16260 KiB
11Accepted3/33ms3524 KiB
12Accepted4/446ms16188 KiB
13Accepted4/446ms16256 KiB
14Accepted3/37ms5144 KiB
15Accepted3/345ms15400 KiB
16Accepted3/343ms14752 KiB
17Accepted3/346ms15792 KiB
18Accepted4/434ms12924 KiB
19Accepted4/439ms14436 KiB
20Accepted4/450ms16512 KiB