202312026-01-05 16:42:13szabelrLegtávolabbi leszármazottcpp17Elfogadva 50/5063ms6840 KiB
// Legtávolabbi leszármazott.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<vector<int>> adj(n+1);
    vector<int>dist(n + 1, -1);
    vector<int>indegree(n + 1, 0);
    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
        indegree[y]++;
    }
    queue<int> q;
    int maxi = 0;
    int maxip = -1;
    for (int i = 1; i <= n; i++)
    {
        if (indegree[i] == 0)
        {
            q.push(i);
            maxip = i;
            maxi = 0;
            dist[i] = 0;
        }
    }
    
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (auto next : adj[v])
        {
            if(dist[next]==-1)
            {
                dist[next] = dist[v] + 1;
                if (dist[next] > maxi)
                {
                    maxi = dist[next];
                    maxip = next;
                }
                q.push(next);
            }
        }
    }
    cout << maxip;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/02ms316 KiB
2Elfogadva0/046ms5956 KiB
3Elfogadva1/12ms316 KiB
4Elfogadva3/31ms500 KiB
5Elfogadva3/31ms528 KiB
6Elfogadva1/11ms372 KiB
7Elfogadva1/11ms316 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva2/248ms6840 KiB
10Elfogadva3/343ms6708 KiB
11Elfogadva3/31ms316 KiB
12Elfogadva4/448ms6708 KiB
13Elfogadva4/461ms6708 KiB
14Elfogadva3/34ms828 KiB
15Elfogadva3/356ms5908 KiB
16Elfogadva3/343ms5788 KiB
17Elfogadva3/348ms6200 KiB
18Elfogadva4/435ms4660 KiB
19Elfogadva4/445ms5428 KiB
20Elfogadva4/463ms6452 KiB