Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- vector<int> graph [101];
- bool used[101];
- int len[101];
- void bfs(int v){
- len[v]=1;
- used[v]=true;
- queue<int>q;
- q.push(v);
- while(!q.empty()){
- v=q.front();
- q.pop();
- for (int i=0; i<graph[v].size();++i){
- int to=graph[v][i];
- if (!used[to]){
- used[to]=true;
- len[to]=(len[v]+1)%2;
- }
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cin >> n;
- for (int i=1; i<=n; ++i){
- int a;
- while (cin >> a && a!=0){
- graph[i].push_back(a);
- }
- }
- bool ok=true;
- for (int i =1; i<=n; ++i){
- if (graph[i].empty()){
- ok=false;
- }
- }
- if (ok) {
- vector<int>ans;
- int cnt=0;
- for (int i = 1; i <= n; ++i) {
- if (!used[i]) {
- bfs(i);
- }
- }
- for (int i=1; i<=n; ++i){
- if (len[i]==0){
- cnt++;
- ans.push_back(i);
- }
- }
- cout << cnt << '\n';
- for (auto elem: ans){
- cout << elem << " ";
- }
- }
- else{
- cout << 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement