75402024-01-09 13:45:42adamElágazás nélküli úton levő települések (50 pont)cpp17Futási hiba 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base33/50
1Elfogadva0/03ms1808 KiB
2Elfogadva0/028ms3928 KiB
3Elfogadva2/23ms2276 KiB
4Elfogadva2/23ms2424 KiB
5Futási hiba0/23ms2772 KiB
6Elfogadva2/23ms3084 KiB
7Elfogadva2/23ms3080 KiB
8Elfogadva2/24ms3240 KiB
9Elfogadva2/27ms3576 KiB
10Elfogadva2/28ms3952 KiB
11Elfogadva2/214ms4336 KiB
12Elfogadva2/216ms4584 KiB
13Hibás válasz0/34ms3644 KiB
14Futási hiba0/386ms63132 KiB
15Hibás válasz0/36ms4364 KiB
16Futási hiba0/386ms62912 KiB
17Hibás válasz0/314ms5004 KiB
18Elfogadva3/314ms4900 KiB
19Elfogadva3/317ms5352 KiB
20Elfogadva3/328ms5808 KiB
21Elfogadva3/328ms6080 KiB
22Elfogadva3/329ms6080 KiB