75402024-01-09 13:45:42adamElágazás nélküli úton levő települések (50 pont)cpp17Runtime error 33/5086ms63132 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());
    vector<int> accessible2; 
        accessible2.push_back(accessible[0]);
    for (int i = 1; i < accessible.size(); i++) {
        if (accessible[i-1] != accessible[i])
            accessible2.push_back(accessible[i]);
    }   

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

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base33/50
1Accepted0/03ms1808 KiB
2Accepted0/028ms3928 KiB
3Accepted2/23ms2276 KiB
4Accepted2/23ms2424 KiB
5Runtime error0/23ms2772 KiB
6Accepted2/23ms3084 KiB
7Accepted2/23ms3080 KiB
8Accepted2/24ms3240 KiB
9Accepted2/27ms3576 KiB
10Accepted2/28ms3952 KiB
11Accepted2/214ms4336 KiB
12Accepted2/216ms4584 KiB
13Wrong answer0/34ms3644 KiB
14Runtime error0/386ms63132 KiB
15Wrong answer0/36ms4364 KiB
16Runtime error0/386ms62912 KiB
17Wrong answer0/314ms5004 KiB
18Accepted3/314ms4900 KiB
19Accepted3/317ms5352 KiB
20Accepted3/328ms5808 KiB
21Accepted3/328ms6080 KiB
22Accepted3/329ms6080 KiB