198872025-12-28 23:23:40szabelrElágazás nélküli úton levő települések (50 pont)cpp17Elfogadva 50/5013ms1332 KiB
// Elágazás nélküli úton levő települések.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m,x,y;
    cin >> n >> m;
    vector<vector<int>> adj(n + 1);
    vector<int> res;
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    vector<bool> visited(n+1,false);
    for (int i = 1; i <= n; i++)
    {
        if (adj[i].size() == 1)
        {
            int before = i;
            int next = adj[i][0];
            if (!visited[next])
            
                res.push_back(next);
            
            visited[next] = true;
            while (adj[next].size() == 2)
            {
                if (adj[next][0] == before)
                {
                    before = next;
                    next = adj[next][1];
                    
                }
                else
                {
                    before = next;
                    next = adj[next][0];
                }
                if(!visited[next])
                
                   res.push_back(next);
                
                visited[next] = true;
                
            }
        }
    }
    sort(res.begin(), res.end());
    cout << res.size() << endl;
    for (auto x : res)
        cout << x << " ";

}

// 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/01ms316 KiB
2Elfogadva0/013ms1332 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva2/21ms420 KiB
6Elfogadva2/21ms316 KiB
7Elfogadva2/21ms316 KiB
8Elfogadva2/22ms316 KiB
9Elfogadva2/23ms516 KiB
10Elfogadva2/24ms580 KiB
11Elfogadva2/27ms780 KiB
12Elfogadva2/27ms820 KiB
13Elfogadva3/31ms316 KiB
14Elfogadva3/32ms508 KiB
15Elfogadva3/33ms564 KiB
16Elfogadva3/33ms564 KiB
17Elfogadva3/36ms748 KiB
18Elfogadva3/37ms848 KiB
19Elfogadva3/38ms1076 KiB
20Elfogadva3/312ms1076 KiB
21Elfogadva3/313ms1296 KiB
22Elfogadva3/313ms1260 KiB