88622024-02-02 16:29:17zeytonxLegtávolabbi leszármazottcpp17Runtime error 21/50105ms63852 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;
		g[a-1].push_back(b-1);
		top[b-1] = 1;
	}
	ll p = 0;
	for(ll i = 0; i < n-1; 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/03ms1972 KiB
2Accepted0/075ms12772 KiB
3Accepted1/13ms3304 KiB
4Accepted3/33ms3532 KiB
5Accepted3/33ms3744 KiB
6Accepted1/13ms3872 KiB
7Accepted1/13ms3968 KiB
8Accepted1/13ms4188 KiB
9Accepted2/2105ms15916 KiB
10Accepted3/3104ms16884 KiB
11Accepted3/33ms6244 KiB
12Runtime error0/463ms63852 KiB
13Runtime error0/465ms63844 KiB
14Accepted3/37ms4068 KiB
15Runtime error0/357ms63796 KiB
16Runtime error0/354ms63620 KiB
17Runtime error0/359ms63552 KiB
18Runtime error0/450ms63492 KiB
19Runtime error0/454ms63496 KiB
20Runtime error0/459ms63488 KiB