129562025-01-03 21:48:16szabelrElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5029ms3428 KiB
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
vector<int> adj[100001];
int h(int a, int b){
    return a < b;
}
int main()
{
    bool visited[10001]{ 0 };
    int jok[10001];
    int n, m,a,b,db=0;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> a >> b;
        adj[b].push_back(a);
        adj[a].push_back(b);
    }
    for (int i = 1; i <= n; i++) {
        if (adj[i].size() == 1) {
            int elozo = i;
            int most=adj[i][0];
            if (visited[most] == 0) {
                db++;
                jok[db] = most;
                visited[most] = 1;
            }
            while (adj[most].size() == 2) {
               
                if (adj[most][0] != elozo) {
                    elozo = most;
                    most = adj[elozo][0];
                    if (visited[most] == 0) {
                        db++;
                        jok[db] = most;
                        visited[most] = 1;
                    }
                    
                }
                else {
                    elozo = most;
                    most = adj[elozo][1];
                    if (visited[most] == 0) {
                        db++;
                        jok[db] = most;
                        visited[most] = 1;
                    }
                }
            }
        }
    }
    sort(jok + 1, jok + db + 1);
    cout << db << endl;
    for (int i = 1; i <= db; i++) {
        cout << jok[i]<<" ";
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2796 KiB
2Accepted0/028ms3384 KiB
3Accepted2/23ms2616 KiB
4Accepted2/23ms2616 KiB
5Accepted2/23ms2616 KiB
6Accepted2/23ms2616 KiB
7Accepted2/23ms2616 KiB
8Accepted2/24ms2616 KiB
9Accepted2/27ms2800 KiB
10Accepted2/28ms2744 KiB
11Accepted2/216ms2964 KiB
12Accepted2/216ms3084 KiB
13Accepted3/34ms2804 KiB
14Accepted3/34ms2616 KiB
15Accepted3/36ms2840 KiB
16Accepted3/37ms2804 KiB
17Accepted3/314ms2984 KiB
18Accepted3/314ms2872 KiB
19Accepted3/317ms3036 KiB
20Accepted3/328ms3384 KiB
21Accepted3/329ms3320 KiB
22Accepted3/328ms3428 KiB