34402023-02-27 19:33:08k_balintKét csoportcpp14Accepted 100/100128ms44188 KiB
#include <bits/stdc++.h>
using namespace std;
const int c=3e5+5;

int n;
vector<int> adj[c];
int col[c];
bool rossz[c];
vector<int> q;
bool vis[c];

void dfs(int v, int p){
    vis[v]=1; col[v]=p;
    for(int x:adj[v]){
        if(!vis[v]) dfs(x,p^1);
    }
}

int r(){
    int res=0; char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)){
        res=(res<<3)+(res<<1)+ch-'0';
        ch=getchar();
    }
    return res;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    n=r();
    for(int i=1;i<=n;i++){
        int k;
        while(1){
            k=r();
            if(!k) break;
            adj[i].push_back(k);
        }
    }

    for(int i=1;i<=n;i++){
        if(!vis[i]) dfs(i,0);
    }

    for(int i=1;i<=n;i++){
        int db=0;
        for(int x:adj[i]){
            if(col[i]==col[x]) ++db;
        }
        if(db>=2) q.push_back(i);
    }

    while(!q.empty()){
        int cur=q.back();
        q.pop_back();

        int db=0;
        for(int x:adj[cur]){
            if(col[cur]==col[x]) ++db;
        }

        if(db<2) continue;

        col[cur]^=1;
        for(int x:adj[cur]){
            int db=0;
            for(int y:adj[x]){
                if(col[x]==col[y]) ++db;
            }
            if(db>=2) q.push_back(x);
        }
    }

    int db=0;
    for(int i=1;i<=n;i++){
        if(col[i]) db++;
    }

    cout << db << '\n';
    for(int i=1;i<=n;i++){
        if(col[i]) cout << i << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/08ms15984 KiB
2Accepted0/035ms24188 KiB
3Accepted3/38ms16612 KiB
4Accepted3/38ms16568 KiB
5Accepted3/38ms16696 KiB
6Accepted3/38ms17024 KiB
7Accepted3/38ms17096 KiB
8Accepted2/28ms17244 KiB
9Accepted3/38ms17488 KiB
10Accepted3/38ms17460 KiB
11Accepted3/38ms17412 KiB
12Accepted3/310ms18420 KiB
13Accepted3/310ms18548 KiB
14Accepted3/310ms18680 KiB
15Accepted6/637ms25628 KiB
16Accepted7/741ms26304 KiB
17Accepted7/746ms26264 KiB
18Accepted6/670ms33856 KiB
19Accepted6/675ms34836 KiB
20Accepted6/686ms34784 KiB
21Accepted6/696ms41972 KiB
22Accepted7/7108ms44188 KiB
23Accepted7/7128ms43948 KiB
24Accepted7/7128ms43952 KiB