39532023-03-06 15:18:01kdbElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5014ms5984 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <stack>
#include <deque>
#include <map>
#define ll long long
using namespace std;

ostream& operator<<(ostream& os, const vector<int>& input)
{
    for (auto const& i : input) {
        os << i << " ";
    }
    return os;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<vector<int>>vec(n + 1);
    vector<int>indul(n + 1);

    for (size_t i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        vec[a].push_back(b);
        vec[b].push_back(a);
        indul[a]++;
        indul[b]++;
    }

    vector<int>zsak;
    for (size_t i = 1; i < n + 1; i++)
    {
        if (indul[i] == 1) zsak.push_back(i);
    }

    map<int, int>ki;
    for (int x : zsak)
    {
        vector<bool>visited(n + 1);
        queue<int>q;
        q.push(x);
        visited[x] = true;
        while (!q.empty())
        {
            int i = q.front();
            q.pop();
            for (int j : vec[i])
            {
                if (visited[j] == false)
                {
                    visited[j] = true;

                    if (ki.find(j) == ki.end()) ki[j] = 1;
                    if (vec[j].size() > 2) break;
                    q.push(j);
                }
            }

        }
    }
    cout << ki.size() << endl;
    for (auto i : ki)
    {
        cout << i.first << " ";
    }
    cout << endl;
    return 0;
}
/*
8 9
1 2
1 6
2 7
1 5
3 4
4 5
5 6
6 7
8 7

*/
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1824 KiB
2Accepted0/013ms3908 KiB
3Accepted2/22ms2276 KiB
4Accepted2/22ms2492 KiB
5Accepted2/23ms2700 KiB
6Accepted2/23ms2912 KiB
7Accepted2/23ms3084 KiB
8Accepted2/23ms3404 KiB
9Accepted2/24ms3612 KiB
10Accepted2/24ms3964 KiB
11Accepted2/28ms4616 KiB
12Accepted2/28ms4972 KiB
13Accepted3/33ms4104 KiB
14Accepted3/34ms4500 KiB
15Accepted3/34ms4496 KiB
16Accepted3/34ms4896 KiB
17Accepted3/37ms4980 KiB
18Accepted3/38ms5020 KiB
19Accepted3/38ms5336 KiB
20Accepted3/313ms5876 KiB
21Accepted3/314ms5984 KiB
22Accepted3/313ms5944 KiB