47442023-03-31 10:44:58Erik_GepardLegtávolabbi leszármazottcpp17Accepted 50/5041ms16280 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int> 
 
vector<int> graf[100009];

 
void solve() {
  int n;
  cin>>n;
  vector<int> mas(n+1);
  for(int i=0; i<n-1; i++){
    int a, b;
    cin>>a>>b;
    graf[a].pb(b);
    mas[b]=1;
  }
  int rajt=0;
  for(int i=1; i<=n; i++){
    if(mas[i]==0) rajt=i;
  }
  vector<int> d(n+1, INT_MAX);
  vector<int> volt(n+1, 0);
  queue<int> q;
  d[rajt]=0;
  volt[rajt]=1;
  q.push(rajt);
  while(!q.empty()){
    int cs=q.front();
    q.pop();
    for(int sz : graf[cs]){
        if(!volt[sz]){
            volt[sz]=1;
            d[sz]=d[cs]+1;
            q.push(sz);
        }
    }
  }
  int maxi=0, maxcs=0;
  for(int i=1; i<=n; i++){
    if(d[i]>maxi){
        maxi=d[i];
        maxcs=i;
    }
  }
  cout<<maxcs<<"\n";
}
 
int main() {
	ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms6600 KiB
2Accepted0/034ms11924 KiB
3Accepted1/14ms7212 KiB
4Accepted3/34ms7432 KiB
5Accepted3/34ms7388 KiB
6Accepted1/14ms7428 KiB
7Accepted1/14ms7520 KiB
8Accepted1/14ms7848 KiB
9Accepted2/235ms13428 KiB
10Accepted3/337ms13484 KiB
11Accepted3/34ms7932 KiB
12Accepted4/437ms14372 KiB
13Accepted4/439ms14608 KiB
14Accepted3/37ms8880 KiB
15Accepted3/334ms15048 KiB
16Accepted3/334ms14404 KiB
17Accepted3/335ms14140 KiB
18Accepted4/427ms13364 KiB
19Accepted4/432ms13992 KiB
20Accepted4/441ms16280 KiB