96752024-02-23 22:13:31Pbg4444Parti (75 pont)cpp17Runtime error 72/7593ms16956 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> adj[n];
    vector<int> s(n, 0);

    int a, b;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;

        adj[i].push_back(a-1);
        adj[i].push_back(b-1);
        s[a-1]++;
        s[b-1]++;
    }

    queue<int> q;
    vector<bool> v(n, 0);

    for (int i = 0; i < n; i++) {
        if (s[i] >= 2) continue;

        q.push(i);
    }

    while (!q.empty()) {
        int z = q.front();
        q.pop();

        if (v[z]) continue;

        v[z] = 1;

        for (int y : adj[z]) {
            s[y]--;
            if (s[y] < 2) q.push(y);
        }
    }

    int out = 0;
    string ans = "";
    for(int i = 0; i < n; i++){
        if(!v[i]){
            out++;
            ans += to_string(i+1) + " ";
        }
    }
    ans.erase(ans.size()-1);
    cout<<out;
    if(ans != "")cout<<"\n"<<ans;
}

SubtaskSumTestVerdictTimeMemory
base72/75
1Accepted0/03ms1808 KiB
2Accepted0/046ms8628 KiB
3Accepted3/33ms2524 KiB
4Accepted3/33ms2592 KiB
5Runtime error0/34ms3192 KiB
6Accepted3/33ms2864 KiB
7Accepted3/33ms3100 KiB
8Accepted4/43ms3292 KiB
9Accepted4/43ms3304 KiB
10Accepted4/44ms3624 KiB
11Accepted4/43ms3684 KiB
12Accepted4/44ms3956 KiB
13Accepted4/44ms4360 KiB
14Accepted4/46ms4508 KiB
15Accepted4/446ms10320 KiB
16Accepted4/454ms11788 KiB
17Accepted4/464ms13008 KiB
18Accepted4/471ms14272 KiB
19Accepted4/482ms15520 KiB
20Accepted4/492ms16952 KiB
21Accepted4/493ms16956 KiB
22Accepted4/43ms4116 KiB