82302024-01-12 22:19:18bovizdbElágazás nélküli úton levő települések (50 pont)cpp17Elfogadva 50/5014ms6648 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
#define endl "\n"
#define pll pair<ll, ll>
#define fs first
#define sc second
#define vll vector<ll>
#define v2d vector<vector<ll>>
#define vpll vector<pll>
#define qll queue<ll>
#define stll stack<ll>
#define sll set<ll>
#define mll map<ll, ll>

ll n, m;
v2d g;

void solve()
{
    cin >> n >> m;
    g.resize(n);
    for (ll i = 0; i < m; i++)
    {
        ll in1, in2;
        cin >> in1 >> in2;
        g[in1-1].push_back(in2-1);
        g[in2-1].push_back(in1-1);
    }
    vll v;
    for (ll i = 0; i < n; i++)
    {
        if (g[i].size() == 1)
        {
            v.push_back(i);
        }
    }
    vector<bool>out(n);
    for (ll i : v)
    {
        ll p = i, last = -1;
        while(true)
        {
            if (g[p].size() > 2 || g[p].size() == 0) break;
            if (g[p].size() == 1 && p != i) break;
            if (g[p][0] == last)
            {
                out[g[p][1]] = 1;
                last = p;
                p = g[p][1];
            }
            else
            {
                out[g[p][0]] = 1;
                last = p;
                p = g[p][0];
            }
        }
    }
    cout << count(out.begin(), out.end(), 1) << endl;
    for (ll i = 0; i < n; i++)
    {
        if (out[i] == 1)
        {
            cout << i+1 << " ";
        }
    }
}
 
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1828 KiB
2Elfogadva0/014ms4824 KiB
3Elfogadva2/23ms2376 KiB
4Elfogadva2/23ms2496 KiB
5Elfogadva2/23ms2680 KiB
6Elfogadva2/23ms2888 KiB
7Elfogadva2/23ms3236 KiB
8Elfogadva2/24ms3664 KiB
9Elfogadva2/24ms3748 KiB
10Elfogadva2/26ms4276 KiB
11Elfogadva2/28ms4940 KiB
12Elfogadva2/28ms4956 KiB
13Elfogadva3/33ms3688 KiB
14Elfogadva3/34ms3916 KiB
15Elfogadva3/34ms4088 KiB
16Elfogadva3/34ms4176 KiB
17Elfogadva3/38ms4800 KiB
18Elfogadva3/38ms4808 KiB
19Elfogadva3/39ms5340 KiB
20Elfogadva3/314ms6344 KiB
21Elfogadva3/314ms6648 KiB
22Elfogadva3/314ms6640 KiB