57082023-09-09 18:14:19TomaSajtElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5014ms5956 KiB
#include <bits/stdc++.h>
using namespace std;

set<int> sol;
vector<vector<int>> g;

void dfs(int u, int p) {
  for (int v : g[u]) {
    if (v == p) continue;
    sol.insert(v);
    if (g[v].size() >= 3) continue;
    dfs(v, u);
  }
}
int main() {
  cin.tie(0), ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  g.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);
  }

  cout << sol.size() << '\n';
  for (int i : sol) cout << i << ' ';
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2168 KiB
2Accepted0/014ms4228 KiB
3Accepted2/23ms2592 KiB
4Accepted2/23ms2644 KiB
5Accepted2/23ms2856 KiB
6Accepted2/22ms3064 KiB
7Accepted2/23ms3204 KiB
8Accepted2/23ms3636 KiB
9Accepted2/24ms4056 KiB
10Accepted2/26ms4208 KiB
11Accepted2/28ms4524 KiB
12Accepted2/28ms4836 KiB
13Accepted3/33ms3984 KiB
14Accepted3/34ms4236 KiB
15Accepted3/34ms4352 KiB
16Accepted3/34ms4660 KiB
17Accepted3/38ms5156 KiB
18Accepted3/38ms5148 KiB
19Accepted3/39ms5424 KiB
20Accepted3/314ms5584 KiB
21Accepted3/314ms5900 KiB
22Accepted3/314ms5956 KiB