253972026-02-19 19:19:42mihalykocsisParti (75 pont)cpp17Accepted 75/7530ms2292 KiB
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<pair<int,int>> wish(N+1);
    vector<int> deg(N+1, 0);
    vector<bool> removed(N+1, false);

    for(int i=1;i<=N;i++){
        cin >> wish[i].first >> wish[i].second;
        deg[wish[i].first]++;
        deg[wish[i].second]++;
    }

    queue<int> q;
    for(int i=1;i<=N;i++){
        if(deg[i] < 2){
            q.push(i);
            removed[i] = true;
        }
    }

    while(!q.empty()){
        int x = q.front(); q.pop();
        int a = wish[x].first;
        int b = wish[x].second;

        if(!removed[a]){
            deg[a]--;
            if(deg[a] < 2){
                removed[a] = true;
                q.push(a);
            }
        }
        if(!removed[b]){
            deg[b]--;
            if(deg[b] < 2){
                removed[b] = true;
                q.push(b);
            }
        }
    }

    vector<int> ans;
    for(int i=1;i<=N;i++){
        if(!removed[i]) ans.push_back(i);
    }

    cout << ans.size() << "\n";
    for(int x: ans) cout << x << " ";
    cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base75/75
1Accepted0/01ms316 KiB
2Accepted0/016ms1336 KiB
3Accepted3/31ms316 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms336 KiB
7Accepted3/31ms316 KiB
8Accepted4/41ms316 KiB
9Accepted4/41ms316 KiB
10Accepted4/41ms336 KiB
11Accepted4/41ms316 KiB
12Accepted4/41ms316 KiB
13Accepted4/42ms316 KiB
14Accepted4/42ms468 KiB
15Accepted4/416ms1380 KiB
16Accepted4/418ms1308 KiB
17Accepted4/421ms1524 KiB
18Accepted4/424ms1844 KiB
19Accepted4/428ms1884 KiB
20Accepted4/429ms2000 KiB
21Accepted4/430ms2292 KiB
22Accepted4/41ms316 KiB