82282024-01-12 22:12:03bovizdbElágazás nélküli úton levő települések (50 pont)cpp17Time limit exceeded 34/50300ms6772 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][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;
}
SubtaskSumTestVerdictTimeMemory
base34/50
1Accepted0/03ms1824 KiB
2Accepted0/014ms4792 KiB
3Time limit exceeded0/2300ms2204 KiB
4Accepted2/23ms2468 KiB
5Accepted2/23ms2668 KiB
6Wrong answer0/23ms2900 KiB
7Accepted2/23ms2976 KiB
8Accepted2/24ms3356 KiB
9Accepted2/24ms3568 KiB
10Accepted2/26ms3932 KiB
11Accepted2/28ms4728 KiB
12Accepted2/28ms4988 KiB
13Accepted3/33ms3828 KiB
14Wrong answer0/34ms4132 KiB
15Accepted3/34ms4312 KiB
16Wrong answer0/34ms4484 KiB
17Wrong answer0/38ms5108 KiB
18Wrong answer0/38ms5108 KiB
19Accepted3/39ms5624 KiB
20Accepted3/314ms6312 KiB
21Accepted3/314ms6676 KiB
22Accepted3/314ms6772 KiB