202302026-01-05 16:39:58szabelrLegtávolabbi leszármazottcpp17Wrong answer 49/50109ms6788 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()
{
    int n;
    cin >> n;
    vector<vector<int>> adj(n+1);
    vector<int>dist(n + 1, 0);
    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;
        }
    }
    
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (auto next : adj[v])
        {
            if(dist[next]==0)
            {
                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
SubtaskSumTestVerdictTimeMemory
base49/50
1Accepted0/01ms508 KiB
2Accepted0/086ms5940 KiB
3Wrong answer0/11ms316 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted1/11ms316 KiB
7Accepted1/11ms316 KiB
8Accepted1/12ms316 KiB
9Accepted2/290ms6716 KiB
10Accepted3/386ms6788 KiB
11Accepted3/31ms376 KiB
12Accepted4/4109ms6708 KiB
13Accepted4/4104ms6640 KiB
14Accepted3/38ms820 KiB
15Accepted3/389ms5940 KiB
16Accepted3/389ms5744 KiB
17Accepted3/390ms6248 KiB
18Accepted4/465ms4660 KiB
19Accepted4/479ms5648 KiB
20Accepted4/4101ms6452 KiB