65312023-12-07 10:34:17tamasmarkElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5029ms6428 KiB
// zsakfalvak.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>

using namespace std;

struct adat
{
    int lep, lat;
    vector<int>sz;
};
deque<adat>x;
int akt;
deque<int>v,y,megold,megoldb;

int n, a, b, i, kezd,m;

int main()
{
    cin >> n>>m;
    x.resize(n + 1);
    megoldb.resize(n + 1);
    for (i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        x[a].sz.push_back(b);
        x[b].sz.push_back(a);
    }
    for (i = 1; i <= n; ++i)
        if (x[i].sz.size() == 1)
        {
            y.push_back(i);
        }
    i = 0;
    while (!y.empty())
    {
        i++;
        v.push_back(y.front());
        //y.pop_front();
        x[v.front()].lat = i;
        while (!v.empty())
        {
           akt = v.front();
           v.pop_front();
           for (auto& e : x[akt].sz)
           {
               if (x[e].sz.size() <= 2 &&x[e].lat<x[akt].lat)
               {
                   x[e].lat = x[akt].lat;
                   v.push_back(e);
                   if (!megoldb[e]) megold.push_back(e);
                   megoldb[e] = 1;
               }
               else if (x[akt].sz.size() <= 2 && x[e].lat < x[akt].lat)
               {
                   if (!megoldb[e]) megold.push_back(e);
                   megoldb[e]=1;
                   x[e].lat = x[akt].lat;
               }
           }
        }
        y.pop_front();
    }
    if (megold.size())
    {
        cout << megold.size() << "\n";
        sort(megold.begin(), megold.end());
        for (i = 0; i < megold.size(); ++i) cout << megold[i] << " ";
    }
    else cout << 0 << "\n";
    
    return 0;
}
/*
8 9
1 2
1 6
2 7
1 5
3 4
4 5
5 6
6 7
8 7

4 3
1 2
2 3
3 4

*/
// 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
base50/50
1Accepted0/03ms1880 KiB
2Accepted0/029ms4332 KiB
3Accepted2/23ms2572 KiB
4Accepted2/23ms2396 KiB
5Accepted2/23ms2592 KiB
6Accepted2/23ms2876 KiB
7Accepted2/23ms2844 KiB
8Accepted2/24ms3408 KiB
9Accepted2/27ms3584 KiB
10Accepted2/28ms3892 KiB
11Accepted2/216ms4468 KiB
12Accepted2/216ms4560 KiB
13Accepted3/34ms3792 KiB
14Accepted3/34ms4032 KiB
15Accepted3/36ms4060 KiB
16Accepted3/37ms4448 KiB
17Accepted3/314ms4864 KiB
18Accepted3/314ms5132 KiB
19Accepted3/317ms5476 KiB
20Accepted3/328ms6092 KiB
21Accepted3/329ms6288 KiB
22Accepted3/329ms6428 KiB