69442023-12-21 07:46:02MagyarKendeSZLGElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5014ms5528 KiB
#include <iostream>
#include <vector>
#include <set>

using namespace std;
#define speed cin.tie(0); ios::sync_with_stdio(0)

vector<vector<int>> neighborS;
set<int> result;

void dfs(int node, int parent) {
    for (int neighbor : neighborS[node]) {
        if (neighbor == parent) continue;
        result.insert(neighbor);
        if (neighborS[neighbor].size() > 2) continue;
        dfs(neighbor, node);
    }
}

int main() {
    speed;

    int N, M;
    cin >> N >> M;

    neighborS.resize(N + 1);
    
    while (M--) {
        int U, V;
        cin >> U >> V;
        neighborS[U].push_back(V);
        neighborS[V].push_back(U);
    }

    for (int i = 1; i <= N; i++) {
        if (neighborS[i].size() == 1) { // zsakfalu
            dfs(i, 0);
        }
    }

    cout << result.size() << '\n';
    for (int node : result) cout << node << ' ';
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1888 KiB
2Accepted0/014ms3932 KiB
3Accepted2/23ms2168 KiB
4Accepted2/23ms2296 KiB
5Accepted2/23ms2504 KiB
6Accepted2/23ms2716 KiB
7Accepted2/23ms3084 KiB
8Accepted2/24ms3160 KiB
9Accepted2/24ms3512 KiB
10Accepted2/26ms3812 KiB
11Accepted2/28ms4204 KiB
12Accepted2/28ms4388 KiB
13Accepted3/33ms3460 KiB
14Accepted3/34ms3656 KiB
15Accepted3/34ms3720 KiB
16Accepted3/34ms4112 KiB
17Accepted3/38ms4380 KiB
18Accepted3/38ms4412 KiB
19Accepted3/39ms4732 KiB
20Accepted3/314ms5264 KiB
21Accepted3/314ms5528 KiB
22Accepted3/314ms5528 KiB