43612023-03-27 08:53:33AblablablaLegtávolabbi leszármazottcpp17Accepted 50/5083ms13932 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> csucsok(n + 1, vector<int>(0, 0));
    vector<bool> kozosOs(n + 1, true);

    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        csucsok[a].push_back(b);
        kozosOs[b] = false;
    }

    int kozos;
    for(int i = 1; i <= n; i++){
        if(kozosOs[i]){
            kozos = i;
            break;
        }
    }

    queue<int> bejar;
    bejar.push(kozos);
    vector<bool> marVolt(n + 1, false);
    marVolt[0] = true;
    int aktualis = 1;
    int kovi = 0;
    int melyseg = 0;
    int valasz = 0;
    /*cout << kozos << "\n";
    for(int i = 1; i <= n; i++){
        for(int x : csucsok[i]){
            cout << x << " ";
        }
        cout << "\n";
    }*/

    while(!bejar.empty()){
        int akt = bejar.front();
        bejar.pop();

        for(int x : csucsok[akt]){
            if(!marVolt[x]){
                bejar.push(x);
                marVolt[x] = true;
                kovi++;
                //cout << x << " " << bejar.size() << " " << kovi << "\n";
            }
        }

        aktualis--;
        //cout << aktualis << "\n";

        if(aktualis == 0){
            swap(aktualis, kovi);
            melyseg++;
            if(aktualis != 0){
                valasz = bejar.front();
            }
            /*cout << "aktualis: " << aktualis << "\n";
            cout << "melyseg: " << melyseg << "\n";
            cout << "valasz: " << valasz << "\n";*/
        }
    }

    cout << valasz << "\n";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1840 KiB
2Accepted0/074ms9372 KiB
3Accepted1/13ms2156 KiB
4Accepted3/33ms2412 KiB
5Accepted3/33ms2544 KiB
6Accepted1/12ms2628 KiB
7Accepted1/13ms2760 KiB
8Accepted1/13ms3020 KiB
9Accepted2/279ms10584 KiB
10Accepted3/379ms10848 KiB
11Accepted3/33ms3220 KiB
12Accepted4/482ms11828 KiB
13Accepted4/481ms11824 KiB
14Accepted3/38ms3900 KiB
15Accepted3/375ms12168 KiB
16Accepted3/371ms11504 KiB
17Accepted3/376ms11620 KiB
18Accepted4/457ms10236 KiB
19Accepted4/468ms10900 KiB
20Accepted4/483ms13932 KiB