18632022-12-06 09:55:13kdbLegtávolabbi leszármazottcpp11Accepted 50/5063ms23636 KiB
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin >> n;
    unordered_map<int, int> m;
    vector<vector<int>> vec(n+1, vector<int>(0, 0));
    for (size_t i = 0; i < n-1; i++)
    {
        int a, b;
        cin >> a >> b;
        vec[a].push_back(b);
        m[b]++;
    }
    int kezdet = 0;
    for (size_t i = 1; i < n+1; i++)
    {
        if (m[i] == 0)
        {
            kezdet = i;
            break;
        }
    }
    vector<bool> visited(n + 1, false);
    visited[kezdet] = true;
    queue<int> q;
    int s = kezdet;
    q.push(kezdet);
    while (!q.empty())
    {
        s = q.front();
        q.pop();
        for (int i : vec[s])
        {
            if (!visited[i])
            {
                visited[i] = true;
                q.push(i);
            }
        }
    }
    cout << s << endl;
}
/*
8
1 4
5 7
8 6
8 5
3 1
3 2
3 8
*/
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2108 KiB
2Accepted0/054ms18540 KiB
3Accepted1/12ms2416 KiB
4Accepted3/32ms2476 KiB
5Accepted3/32ms2676 KiB
6Accepted1/12ms2840 KiB
7Accepted1/12ms3056 KiB
8Accepted1/12ms3344 KiB
9Accepted2/250ms20032 KiB
10Accepted3/350ms20032 KiB
11Accepted3/32ms3656 KiB
12Accepted4/457ms21468 KiB
13Accepted4/456ms21676 KiB
14Accepted3/37ms5184 KiB
15Accepted3/352ms21948 KiB
16Accepted3/354ms21260 KiB
17Accepted3/356ms21112 KiB
18Accepted4/437ms16512 KiB
19Accepted4/446ms18164 KiB
20Accepted4/463ms23636 KiB