180122025-09-25 10:43:46ubormaciLegtávolabbi leszármazottcpp17Elfogadva 50/5050ms8244 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*
 *
 * notes:
 *
 * int64_t
 * stoi(string s) -> string to int
 * to_string() -> int (or else) to string
 *
 * vector declaration:
 * vector<ll> v(n, 0)
 * vector<vector<ll>> v(n, vector<ll>(n, 0));
 *
 * {if statement} ? {truth value} : {false value}
 *
 * set lower bound/upper bound:
 * 	// . . . m1 . . . d . . . . m2
 *    auto m1_it = b.lower_bound(d);
 *    advance(m1_it, -1);
 *    m1 = *m1_it;
 *	m2 = *b.upper_bound(d);
 *
 * #ifdef LOCAL
 *    freopen("in.txt","r",stdin);
 *    freopen("out.txt","w",stdout);
 * #endif
 *
 * constexpr auto lcm(auto x, auto... xs)
 * {
 *	return ((x = std::lcm(x, xs)), ...);
 * }
 *
 * std::gcd(int a, int b)
 *
 * cout << setprecision(n);
 *
 * set with custom ordering
 * set<ll, decltype(&cmp)> qu(cmp);
 *
 */

typedef int64_t ll;

void solve() {

    ll n;
    cin >> n;

    vector<vector<ll>> ch(n+1);
    vector<ll> p(n+1, -1);
    for(ll i = 0; i < n - 1; i++) {
        ll father, son;
        cin >> father >> son;
        ch[father].push_back(son);
        p[son] = father;
    }

    queue<ll> q;
    vector<ll> d(n+1, 0);

    ll mx = 0;
    for(ll i = 1; i <= n; i++) {
        if(p[i] == -1) {
            q.push(i);
        }
    }

    while(!q.empty()) {
        ll curr = q.front();
        q.pop();

        mx = max(mx, d[curr]);
        
        for(const auto & c : ch[curr]) {
            d[c] = d[curr] + 1;
            q.push(c);
        }
    }

    for(ll i = 1; i <= n; i++) {
        if(d[i] == mx) {
            cout << i;
            return;
        }
    }

}


int main()
{
    std::ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);

    solve();

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms508 KiB
2Elfogadva0/043ms6704 KiB
3Elfogadva1/11ms316 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva1/11ms500 KiB
7Elfogadva1/11ms316 KiB
8Elfogadva1/12ms496 KiB
9Elfogadva2/250ms7440 KiB
10Elfogadva3/350ms7220 KiB
11Elfogadva3/31ms316 KiB
12Elfogadva4/450ms7632 KiB
13Elfogadva4/448ms7476 KiB
14Elfogadva3/34ms1076 KiB
15Elfogadva3/345ms7272 KiB
16Elfogadva3/343ms6708 KiB
17Elfogadva3/348ms7184 KiB
18Elfogadva4/432ms5684 KiB
19Elfogadva4/437ms6276 KiB
20Elfogadva4/448ms8244 KiB