43412023-03-25 15:58:00szilLegtávolabbi leszármazottcpp14Accepted 50/5043ms18720 KiB
#include <bits/stdc++.h>
 
using ll = long long;
using namespace std;

const int MAXN = 100001;

vector<int> g[MAXN];
bool p[MAXN];
int ans = 1, best = 0;

void dfs(int u, int p, int d) {
	if (best < d) {
		best = d;
		ans = u;
	}
	for (int v : g[u])
		if (v != p)
			dfs(v, u, d+1);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		p[b] = true;
	}
	for (int i = 1; i <= n; i++) {
		if (!p[i]) dfs(i, 0, 0);
	}
	cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms6672 KiB
2Accepted0/034ms9944 KiB
3Accepted1/14ms7076 KiB
4Accepted3/34ms7292 KiB
5Accepted3/34ms7252 KiB
6Accepted1/14ms7252 KiB
7Accepted1/14ms7372 KiB
8Accepted1/14ms7364 KiB
9Accepted2/237ms10856 KiB
10Accepted3/335ms11100 KiB
11Accepted3/34ms7612 KiB
12Accepted4/439ms14180 KiB
13Accepted4/439ms14184 KiB
14Accepted3/38ms8112 KiB
15Accepted3/337ms14860 KiB
16Accepted3/337ms14936 KiB
17Accepted3/337ms14424 KiB
18Accepted4/428ms12912 KiB
19Accepted4/434ms14592 KiB
20Accepted4/443ms18720 KiB