126292024-12-26 23:45:16BucsMateElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5027ms1336 KiB
#include <iostream>
#include <vector>

using namespace std;

void search_graph(int start, vector<vector<int>> &adj, bool visited[])
{
    int currNode = start;
    int lastNode = -1;
    bool valid = true;
    while(valid){
        if(adj[currNode].size() > 2){
            return;
        }
        valid = false;
        for(int i = 0; i < adj[currNode].size(); i++){
            int newNode = adj[currNode][i];
            if(newNode != lastNode){
                lastNode = currNode;
                currNode = newNode;
                visited[currNode] = true;
                valid = true;
                break;
            }
        }
    }
}

int main()
{
    int N, M;
    cin >> N >> M;
    vector<vector<int>> adj(N+2, vector<int>());
    for(int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    bool visited[10001] = {};
    for(int i = 1; i <= N; i++){
        if(adj[i].size() == 1){
            search_graph(i, adj, visited);
        }
    }

    vector<int> sol;
    for(int i = 1; i <= N; i++){
        if(visited[i]){
            sol.push_back(i);
        }
    }
    cout << sol.size() << endl;
    for(int i = 0; i < sol.size(); i++){
        cout << sol[i] << " ";
    }
    cout << endl;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms500 KiB
2Accepted0/027ms1332 KiB
3Accepted2/21ms320 KiB
4Accepted2/21ms320 KiB
5Accepted2/21ms320 KiB
6Accepted2/21ms320 KiB
7Accepted2/21ms320 KiB
8Accepted2/22ms320 KiB
9Accepted2/24ms556 KiB
10Accepted2/27ms556 KiB
11Accepted2/213ms772 KiB
12Accepted2/213ms752 KiB
13Accepted3/32ms320 KiB
14Accepted3/33ms320 KiB
15Accepted3/34ms568 KiB
16Accepted3/34ms568 KiB
17Accepted3/312ms772 KiB
18Accepted3/312ms748 KiB
19Accepted3/316ms944 KiB
20Accepted3/326ms1116 KiB
21Accepted3/327ms1336 KiB
22Accepted3/327ms1272 KiB