69422023-12-21 07:27:04MagyarKendeSZLGElágazás nélküli úton levő települések (50 pont)cpp17Wrong answer 31/5014ms5672 KiB
#include <iostream>
#include <vector>
#include <algorithm>

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

vector<vector<int>> neighborS;
vector<bool> visited;
vector<int> result;

void dfs(int node) {
    if (visited[node]) return;
    visited[node] = 1;

    result.push_back(node);

    if (neighborS[node].size() <= 2) {
        for (int neighbor : neighborS[node]) {
            dfs(neighbor);
        }
    }
}

int main() {
    speed;

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

    neighborS.resize(N + 1);
    visited.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
            visited[i] = 1;
            dfs(neighborS[i][0]);
        }
    }
    
    if (result.empty()) {
        cout << 0;
        exit(0);
    }

    sort(result.begin(), result.end());

    cout << result.size() << '\n';
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base31/50
1Accepted0/03ms1892 KiB
2Accepted0/014ms3968 KiB
3Wrong answer0/23ms2428 KiB
4Accepted2/23ms2632 KiB
5Accepted2/23ms2728 KiB
6Wrong answer0/23ms2936 KiB
7Accepted2/23ms3040 KiB
8Accepted2/24ms3128 KiB
9Accepted2/24ms3576 KiB
10Accepted2/26ms3648 KiB
11Accepted2/28ms4300 KiB
12Accepted2/28ms4428 KiB
13Accepted3/33ms3492 KiB
14Wrong answer0/34ms3812 KiB
15Wrong answer0/34ms3856 KiB
16Wrong answer0/34ms4080 KiB
17Wrong answer0/38ms4376 KiB
18Wrong answer0/38ms4764 KiB
19Accepted3/39ms5064 KiB
20Accepted3/314ms5672 KiB
21Accepted3/314ms5584 KiB
22Accepted3/314ms5636 KiB