211912026-01-12 16:58:24algoproLegtávolabbi leszármazottcpp17Elfogadva 50/5046ms6712 KiB
// UUID: 78510bb9-bfe4-4b3e-81c2-9fb1304e2d9e
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<vector<int>> children(N + 1);
    vector<int> hasParent(N + 1, 0);

    for (int i = 0; i < N - 1; i++) {
        int parent, child;
        cin >> parent >> child;
        children[parent].push_back(child);
        hasParent[child] = 1;
    }
    int root = 1;
    for (int i = 1; i <= N; i++) {
        if (!hasParent[i]) {
            root = i;
            break;
        }
    }
    queue<pair<int, int>> q;
    q.push({root, 0});
    int farthestNode = root;
    int maxDepth = 0;
    while (!q.empty()) {
        auto [u, depth] = q.front();
        q.pop();
        if (depth > maxDepth) {
            maxDepth = depth;
            farthestNode = u;
        }
        for (int v : children[u]) {
            q.push({v, depth + 1});
        }
    }
    cout << farthestNode << "\n";
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/034ms4404 KiB
3Elfogadva1/11ms316 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva1/11ms316 KiB
7Elfogadva1/11ms316 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva2/237ms5092 KiB
10Elfogadva3/339ms4904 KiB
11Elfogadva3/31ms316 KiB
12Elfogadva4/441ms4996 KiB
13Elfogadva4/439ms5168 KiB
14Elfogadva3/34ms820 KiB
15Elfogadva3/341ms5168 KiB
16Elfogadva3/335ms4808 KiB
17Elfogadva3/339ms4776 KiB
18Elfogadva4/430ms4660 KiB
19Elfogadva4/432ms4404 KiB
20Elfogadva4/446ms6712 KiB