76772024-01-10 12:34:07adamA lehető legkevesebb átszállás (50 pont)cpp17Futási hiba 0/504ms4592 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[i] = 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/50
1Futási hiba0/03ms1908 KiB
2Futási hiba0/04ms2732 KiB
3Futási hiba0/13ms2312 KiB
4Futási hiba0/13ms2528 KiB
5Futási hiba0/23ms2796 KiB
6Futási hiba0/23ms2692 KiB
7Futási hiba0/23ms2844 KiB
8Futási hiba0/23ms2992 KiB
9Futási hiba0/23ms3144 KiB
10Futási hiba0/23ms3128 KiB
11Futási hiba0/23ms3276 KiB
12Futási hiba0/23ms3280 KiB
13Futási hiba0/23ms3160 KiB
14Futási hiba0/23ms3428 KiB
15Futási hiba0/23ms3544 KiB
16Futási hiba0/23ms3812 KiB
17Futási hiba0/23ms3956 KiB
18Futási hiba0/24ms4048 KiB
19Futási hiba0/24ms3940 KiB
20Futási hiba0/24ms4084 KiB
21Futási hiba0/24ms4208 KiB
22Futási hiba0/24ms4112 KiB
23Futási hiba0/23ms3928 KiB
24Futási hiba0/23ms3928 KiB
25Futási hiba0/23ms4076 KiB
26Futási hiba0/23ms3972 KiB
27Futási hiba0/23ms4232 KiB
28Futási hiba0/23ms4592 KiB