76272024-01-10 09:31:08adamElágazás nélküli úton levő települések (50 pont)cpp17Wrong answer 18/5030ms6072 KiB
#include <bits/stdc++.h>
#include <algorithm>

using namespace std;

int main() {
    int village_count;
    cin >> village_count;
    int road_count;
    cin >> road_count;

    vector<vector<int>> village_tree(village_count);
    vector<bool> have_been_here(village_count, false);

    for (int i = 0; i < road_count; i++) {

        int village_id;
        int destination_id;


        cin >> village_id >> destination_id;

        if (!count(village_tree[village_id - 1].begin(), village_tree[village_id - 1].end(), destination_id - 1)) {
            village_tree[village_id - 1].push_back(destination_id - 1);
        }
        if (!count(village_tree[destination_id - 1].begin(), village_tree[destination_id - 1].end(), village_id - 1)) {
            village_tree[destination_id - 1].push_back(village_id - 1);
        }

    }

    vector<int> reachable;

    for (int i = 0; i < village_count; i++) {
        if (village_tree[i].size() != 1)
            continue;
        bool stop = false;
        int prev = i;
        if (village_tree[village_tree[i][0]].size() > 2) {
            if (have_been_here[village_tree[i][0]]) continue;
            have_been_here[village_tree[i][0]] = true;
            reachable.push_back(village_tree[i][0]);
            continue;
        }
        int check = village_tree[i][0];
        have_been_here[village_tree[i][0]] = true;
        while (!stop) {
            if (have_been_here[check]) break;
            if (village_tree[check].size() > 2){
                have_been_here[check] = true;
                reachable.push_back(check);
                stop = true;
            }
            else if (village_tree[check].size() == 2) {
                reachable.push_back(check);
                have_been_here[check] = true;
                if (village_tree[check][0] == prev)
                    check = village_tree[check][1];
                else
                    check = village_tree[check][0];
            } else {
                stop = true;
                reachable.push_back(check);
                have_been_here[check] = true;
            }
        }

    }
    cout << reachable.size() << endl;

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

    for (int i = 0; i < reachable.size(); i++) {
        cout << reachable[i] + 1 << " ";
    }
    cout << endl;


    return 0;
}
SubtaskSumTestVerdictTimeMemory
base18/50
1Wrong answer0/03ms1956 KiB
2Accepted0/030ms3976 KiB
3Wrong answer0/23ms2096 KiB
4Accepted2/22ms2104 KiB
5Accepted2/22ms2108 KiB
6Wrong answer0/23ms2352 KiB
7Wrong answer0/23ms2236 KiB
8Accepted2/24ms2324 KiB
9Wrong answer0/27ms2656 KiB
10Accepted2/28ms3096 KiB
11Accepted2/216ms3740 KiB
12Accepted2/217ms4008 KiB
13Wrong answer0/34ms3152 KiB
14Wrong answer0/34ms3472 KiB
15Wrong answer0/36ms3864 KiB
16Wrong answer0/36ms4160 KiB
17Wrong answer0/314ms4560 KiB
18Wrong answer0/314ms4572 KiB
19Wrong answer0/317ms5040 KiB
20Accepted3/329ms5600 KiB
21Wrong answer0/329ms6072 KiB
22Accepted3/330ms5960 KiB