ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1106. Two Teams

C++ AC
Posted by D4nick 26 Jan 2019 19:52
I recommend you this resource: https://e-maxx.ru/algo/bfs
also you can make a function instead of the lyambda, but I just like lyambdas more.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
    int N;
    cin >> N;
    vector <vector <int>> g(N);
    int h;
    for (int i = 0; i < N; i++) {
        for (;;) {
            cin >> h;
            if (!h)
                break;
            g[i].push_back(h - 1);
        }
    }
    int n = g.size();
    queue<int> q;
    vector<bool> used(n);
    vector<int> d(n);
    int s;
    auto bfs = [&](int start) {
        s = start;
        q.push(s);
        used[s] = true;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (size_t i = 0; i<g[v].size(); ++i) {
                int to = g[v][i];
                if (!used[to]) {
                    used[to] = true;
                    q.push(to);
                    d[to] = d[v] + 1;
                }
            }
        }
    };
    for (int start = 0; start < n; ++start)
        if (!used[start]) {
            bfs(start);
        }
    vector <int> ans;
    for (int i = 0; i < n; i++) {
        if (d[i] % 2 == 0)
            ans.push_back(i + 1);
    }
    int size = ans.size();
    cout << size << '\n';
    for (int i = 0; i < size; i++)
        cout << ans[i] << " ";
}

Edited by author 26.01.2019 22:53

Edited by author 30.01.2019 03:16