45312023-03-29 12:51:16TomaSajtElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5014ms6336 KiB
#include <bits/stdc++.h>
using namespace std;
#define speed cin.tie(0); ios::sync_with_stdio(0)

set<int> sol;
vector<int> vis;
vector<vector<int>> g;
int t = 1;

void dfs(int u, int p) {
    for (int v : g[u]) {
        if (v == p || vis[v] == t) continue;
        sol.insert(v);
        if (g[v].size() > 2) continue;
        dfs(v, u);
    }
}

int main() {
    speed;
    int n, m; cin >> n >> m;
    g.resize(n + 1);
    vis.resize(n + 1);
    while (m--) {
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 1) {
            dfs(i, 0);
        }
        t++;
    }
    cout << sol.size() << '\n';
    for (auto i : sol) cout << i << ' ';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1888 KiB
2Accepted0/014ms3972 KiB
3Accepted2/22ms2332 KiB
4Accepted2/22ms2508 KiB
5Accepted2/22ms2620 KiB
6Accepted2/23ms2740 KiB
7Accepted2/23ms2944 KiB
8Accepted2/23ms3384 KiB
9Accepted2/24ms3800 KiB
10Accepted2/24ms4084 KiB
11Accepted2/28ms4636 KiB
12Accepted2/28ms4688 KiB
13Accepted3/33ms3740 KiB
14Accepted3/34ms4096 KiB
15Accepted3/34ms4504 KiB
16Accepted3/34ms4836 KiB
17Accepted3/37ms4968 KiB
18Accepted3/38ms5248 KiB
19Accepted3/38ms5488 KiB
20Accepted3/313ms6036 KiB
21Accepted3/314ms6336 KiB
22Accepted3/314ms6292 KiB