217172026-01-13 18:23:37algoproLegtávolabbi leszármazottcpp17Accepted 50/50104ms9996 KiB
// UUID: abcf8742-a74f-4efa-abed-1eec8d612307
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph;


void dfs(int start, vector<int> &gen, int level){
	gen[start] = level;
	for(auto u : graph[start]){
		if(gen[u] == -1){
			dfs(u, gen, level + 1);
		}
	}

}

int main() {
	int n; cin >> n;
	graph.resize(n + 1);
	int grand = 0;
	vector<bool> isgrand(n + 1, true);
	for(int i = 0; i < n; i++){
		int a, b; cin >> a >> b;
		isgrand[b] = false;
		graph[a].push_back(b);
	}
	for(int i = 1; i <= n; i++){
		if(isgrand[i]){
			grand = i;
			break;
		}
	}
	vector<int> gen(n + 1, -1);
	dfs(grand, gen, 0);
	int max = 0;
	int index = 0;
	for(int i = 1; i <= n; i++){
		if(gen[i] > max){
			index = i;
			max = gen[i];
		}
	}
	cout << index;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/085ms4204 KiB
3Accepted1/11ms316 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted1/11ms316 KiB
7Accepted1/11ms316 KiB
8Accepted1/12ms316 KiB
9Accepted2/293ms4660 KiB
10Accepted3/382ms4680 KiB
11Accepted3/31ms316 KiB
12Accepted4/489ms7348 KiB
13Accepted4/4104ms7364 KiB
14Accepted3/38ms820 KiB
15Accepted3/385ms7072 KiB
16Accepted3/382ms7444 KiB
17Accepted3/3101ms7220 KiB
18Accepted4/465ms5172 KiB
19Accepted4/479ms6732 KiB
20Accepted4/4104ms9996 KiB