76002024-01-10 00:49:26adamElágazás nélküli úton levő települések (50 pont)cpp17Hibás válasz 25/5090ms63844 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);

    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) {
            reachable.push_back(village_tree[i][0]);
            continue;
        }
        int check = village_tree[i][0];
        while (!stop) {
            if (village_tree[check].size() > 2){
                reachable.push_back(check);
                stop = true;
            }
            else if (village_tree[check].size() == 2) {
                reachable.push_back(check);
                if (village_tree[check][0] == prev)
                    check = village_tree[check][1];
                else
                    check = village_tree[check][0];
            } else {
                stop = true;
                reachable.push_back(check);
            }
        }

    }
    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
base25/50
1Elfogadva0/03ms1816 KiB
2Elfogadva0/030ms3876 KiB
3Hibás válasz0/23ms2492 KiB
4Hibás válasz0/23ms2648 KiB
5Elfogadva2/23ms2656 KiB
6Elfogadva2/23ms3008 KiB
7Elfogadva2/23ms3044 KiB
8Elfogadva2/24ms3116 KiB
9Elfogadva2/26ms3176 KiB
10Elfogadva2/28ms3500 KiB
11Elfogadva2/216ms4076 KiB
12Elfogadva2/216ms4076 KiB
13Futási hiba0/383ms63844 KiB
14Futási hiba0/386ms63816 KiB
15Futási hiba0/387ms63780 KiB
16Futási hiba0/385ms63752 KiB
17Futási hiba0/390ms63660 KiB
18Hibás válasz0/314ms4128 KiB
19Hibás válasz0/317ms4492 KiB
20Elfogadva3/329ms4696 KiB
21Elfogadva3/330ms5004 KiB
22Elfogadva3/330ms5136 KiB