75192024-01-09 13:16:41adamElágazás nélküli úton levő települések (50 pont)cpp17Wrong answer 25/5078ms63476 KiB
#include <bits/stdc++.h>
using namespace std;
int main()
{

    int village_count = 0;
    int road_count = 0;
    cin >> village_count;
    cin >> road_count;
    vector<vector<int>> village_tree(village_count, vector<int>(0, 0));
    for (int i = 0; i < road_count; i++)
    {
        int village_id = 0;
        int village_to = 0;

        cin >> village_id;
        cin >> village_to;
        village_tree[village_id - 1].push_back(village_to - 1);
        village_tree[village_to - 1].push_back(village_id - 1);
    }
    vector<int> accessible(0, 0);

    for (int i = 0; i < village_count; i++)
    {
        if (village_tree[i].size() != 1)
            continue;
        bool ended = false;
        int previous = i;
        int check = village_tree[i][0];

        while (!ended)
        {
            accessible.push_back(check);
            if (village_tree[check].size() > 2)
            {

                ended = true;
            }
            else
            {
                if (village_tree[check].size() == 1)
                    ended = true;
                else if (village_tree[check][0] == previous)
                    check = village_tree[check][1];
                else
                    check = village_tree[check][0];
                previous = check;
            }
        }
    }
    sort(accessible.begin(), accessible.end());

    cout << accessible.size() << "\n";
    for (int v : accessible)
    {
        cout << v+1 << " ";
    }
    cout << "\n";

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base25/50
1Accepted0/03ms1848 KiB
2Accepted0/030ms3876 KiB
3Wrong answer0/23ms2212 KiB
4Wrong answer0/23ms2328 KiB
5Accepted2/22ms2404 KiB
6Accepted2/23ms2552 KiB
7Accepted2/23ms2672 KiB
8Accepted2/24ms3128 KiB
9Accepted2/26ms3520 KiB
10Accepted2/29ms3852 KiB
11Accepted2/214ms4120 KiB
12Accepted2/216ms4324 KiB
13Wrong answer0/34ms3456 KiB
14Runtime error0/378ms63476 KiB
15Wrong answer0/36ms4052 KiB
16Runtime error0/375ms63436 KiB
17Wrong answer0/314ms4336 KiB
18Wrong answer0/314ms4372 KiB
19Wrong answer0/317ms4748 KiB
20Accepted3/328ms4956 KiB
21Accepted3/328ms5264 KiB
22Accepted3/328ms5412 KiB