88632024-02-02 16:32:29zeytonxLegtávolabbi leszármazottcpp17Runtime error 21/50108ms63284 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl "\n"
#define pll pair<ll,ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define fs first
#define sc second

const ll MOD = 1e9+7;

void dfs(ll p, vector<vector<ll>>& g, vector<bool> used, ll& ans, ll& ans_v, ll k)
{
	used[p] = true;
	if(k > ans)
	{
		ans_v = p+1;
		ans = k;
	}
	k++;
	for(ll i : g[p])
		if(!used[i]) dfs(i, g, used, ans, ans_v, k);
	k--;
}

void solve()
{
	ll n;
	cin >> n;
	vector<vector<ll>> g(n);
	vector<ll> top(n);
	for(ll i = 0; i < n-1; i++)
	{
		ll a, b;
		cin >> a >> b;
		a--; b--;
		g[a].push_back(b);
		top[b] = 1;
	}
	ll p = 0;
	for(ll i = 0; i < n; i++)
	{
		if(top[i] == 0)
		{
			p = i;
			break;
		}
	}
	vector<bool> used(n, false);
	ll ans = 0;
	ll ans_v = 0;
	ll k = 0;
	dfs(p, g, used, ans, ans_v, k);
	cout << ans_v << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    //cin >> t;
    while(t--)
        solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base21/50
1Accepted0/03ms1828 KiB
2Accepted0/078ms12716 KiB
3Accepted1/13ms3296 KiB
4Accepted3/33ms3500 KiB
5Accepted3/33ms3844 KiB
6Accepted1/13ms3788 KiB
7Accepted1/13ms4012 KiB
8Accepted1/13ms4428 KiB
9Accepted2/2108ms16348 KiB
10Accepted3/3108ms17416 KiB
11Accepted3/33ms7148 KiB
12Runtime error0/471ms63284 KiB
13Runtime error0/475ms63252 KiB
14Accepted3/37ms4660 KiB
15Runtime error0/357ms63028 KiB
16Runtime error0/357ms62800 KiB
17Runtime error0/364ms62772 KiB
18Runtime error0/456ms62568 KiB
19Runtime error0/461ms62556 KiB
20Runtime error0/464ms62532 KiB