p398.ans1
======================
1
4

=================
p398.ans2
======================
0


=================
p398.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, x;
vector<vector<int>> adj;

void read() {
    cin >> n >> x;
    adj.assign(n + 1, vector<int>(n + 1, 0));
    for(int i = 1; i <= n; i++) {
        int cnt;
        cin >> cnt;
        while(cnt--) {
            int f;
            cin >> f;
            adj[f][i] = 1;
            adj[i][f] = 1;
        }
    }
}

void solve() {
    // A friend of a friend of x is any user i that is not x, is not a direct
    // friend of x, yet shares a common friend o with x (o is a friend of x and
    // o is a friend of i). Scan every candidate i and, using the adjacency
    // matrix, look for such an intermediate o; emit the matching users in
    // increasing order.

    vector<int> li;
    for(int i = 1; i <= n; i++) {
        if(i == x || adj[i][x]) {
            continue;
        }

        bool ok = false;
        for(int o = 1; o <= n; o++) {
            if(adj[x][o] && adj[o][i]) {
                ok = true;
            }
        }

        if(ok) {
            li.push_back(i);
        }
    }

    cout << li.size() << '\n';
    cout << li << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p398.in1
======================
4 2
1 2
2 1 3
2 4 2
1 3

=================
p398.in2
======================
4 1
3 4 3 2
3 1 3 4
3 1 2 4
3 1 2 3

=================
statement.txt
======================
398. Friends of Friends
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



Social networks are very popular now. They use different types of relationships to organize individual users in a network. In this problem friendship is used as a method to connect users. For each user you are given the list of his friends. Consider friendship as a symmetric relation, so if user a is a friend of user b then b is a friend of a.

A friend of a friend for a is such a user c that c is not a friend of a, but there is such b that b is a friend of a and c is a friend of b. Obviously c ≠ a.

Your task is to find the list of friends of friends for the given user x.

Input
The first line of the input contains integer numbers N and x (1 ≤ N ≤ 50, 1 ≤ x ≤ N), where N is the total number of users and x is user to be processed. Users in the input are specified by their numbers, integers between 1 and N inclusive. The following N lines describe friends list of each user. The i-th line contains integer di (0 ≤ di ≤ 50) — number of friends of the i-th user. After it there are di distinct integers between 1 and N — friends of the i-th user. The list doesn't contain i. It is guaranteed that if user a is a friend of user b then b is a friend of a.

Output
You should output the number of friends of friends of x in the first line. Second line should contain friends of friends of x printed in the increasing order.

Example(s)
sample input
sample output
4 2
1 2
2 1 3
2 4 2
1 3
1
4

sample input
sample output
4 1
3 4 3 2
3 1 3 4
3 1 2 4
3 1 2 3

=================
