39282023-03-05 19:07:36hackemonLegtávolabbi leszármazottcpp17Elfogadva 50/5037ms15652 KiB
#include <bits/stdc++.h>
using namespace std;
//using namespace std::chrono;

#define pii pair<int,int>
#define pb push_back
#define vi vector<int>
#define vb vector<bool>
#define vl vector<ll>
#define vvi vector<vi>
#define vvb vector<vb>
#define vvl vector<vl>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define fScan {ios_base::sync_with_stdio(false); cin.tie(NULL);}
#define rep(i, a, b) for(int i = a; i < (b); i++)
#define REP(i, n) for(int i = 0;i <(n); i++)
using ll = long long;
using ld = long double;
ll mod = 1000000007;

const char ny =  '\n';

bool prime(ll a) {
    if (a==1) return 0;
    for (int i=2;i*i<=a;++i)
    {
        if (a%i==0) return 0;
    }
    return 1;
}

ll gcd(ll a,ll b) {
    if (b==0) return a;
    return gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a/gcd(a,b)*b;
}

ll min(int a, ll b) {
    if(a< b) return a;
    else return b;
}

int n;
vector<vector<int>> adj(100005);
int os = 1;
vector<int> be(100005, 0), dist(100005, 0);

void bfs(int kezdo) {
    dist[kezdo] = 0;
    queue<int> most;
    most.push(kezdo);
    while(!most.empty()) {
        int x = most.front();
        most.pop();
        for(int i = 0;i< adj[x].size();i++) {
            dist[adj[x][i]] = dist[x]+1;
            most.push(adj[x][i]);
        }
    }
}


void solve() { 
    cin >> n;
    for(int i = 0;i< n-1;i++ ){ 
        int a, b;
        cin >> a >> b;
        be[b]++;
        adj[a].push_back(b);
    }
    for(int i = 1;i<=n;i++ ) {
        if(!be[i]) {
            os = i;
            break;
        }
    }
    bfs(os);
    int ma = 0, ans = 1;
    for(int i = 1;i<=n;i++ ) {
        if(dist[i] > ma) {
            ma = dist[i];
            ans = i;
        }
    }
    cout << ans << ny;
}


int main()
{
    fScan

    int t = 1;
    //comment out if necessary
    //cin>> t;


    //auto start = high_resolution_clock::now();
    while(t-- ) {
        solve();
    }
    //auto end = high_resolution_clock::now();
    //auto dur = duration_cast<milliseconds>(end-start);
    //cout<< "runtime: " << dur.count() << " milliseconds" << '\n';
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/04ms7912 KiB
2Elfogadva0/034ms11368 KiB
3Elfogadva1/14ms8400 KiB
4Elfogadva3/34ms8620 KiB
5Elfogadva3/34ms8904 KiB
6Elfogadva1/14ms8908 KiB
7Elfogadva1/14ms9196 KiB
8Elfogadva1/14ms9096 KiB
9Elfogadva2/235ms12332 KiB
10Elfogadva3/335ms12332 KiB
11Elfogadva3/34ms9276 KiB
12Elfogadva4/437ms13516 KiB
13Elfogadva4/435ms13768 KiB
14Elfogadva3/38ms10080 KiB
15Elfogadva3/332ms14572 KiB
16Elfogadva3/332ms13880 KiB
17Elfogadva3/334ms13740 KiB
18Elfogadva4/427ms13176 KiB
19Elfogadva4/430ms13568 KiB
20Elfogadva4/437ms15652 KiB