198852025-12-28 23:18:34szabelrElágazás nélküli úton levő települések (50 pont)cpp17Elfogadva 50/5028ms1332 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()
{
    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);
    int db = 0;
    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);
                db++;
            }
            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);
                    db++;
                }
                visited[next] = true;
                
            }
        }
    }
    sort(res.begin(), res.end());
    cout << db << 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/028ms1332 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms316 KiB
7Elfogadva2/21ms316 KiB
8Elfogadva2/23ms316 KiB
9Elfogadva2/24ms568 KiB
10Elfogadva2/28ms564 KiB
11Elfogadva2/214ms808 KiB
12Elfogadva2/214ms788 KiB
13Elfogadva3/32ms336 KiB
14Elfogadva3/33ms316 KiB
15Elfogadva3/34ms568 KiB
16Elfogadva3/34ms564 KiB
17Elfogadva3/313ms872 KiB
18Elfogadva3/313ms824 KiB
19Elfogadva3/316ms816 KiB
20Elfogadva3/328ms1140 KiB
21Elfogadva3/328ms1256 KiB
22Elfogadva3/328ms1332 KiB