1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 103;
int n, r, a, krawedzie[MAX_N], dp[MAX_N], krawedzie2[MAX_N];
vector <int> graph[MAX_N];
queue<int> q;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> r;
		for (int j = 1; j <= r; j++) {
			cin >> a;
			graph[i].push_back(a);
			krawedzie[a]++;
			krawedzie2[i]++;
		}
	}
	for (int i = 1; i <= n; i++) {
		if(krawedzie[i] == 0) {
			q.push(i);
			dp[i] = 1;
		}
	}
	while(!q.empty()) {
		int node = q.front();
		q.pop();
		for (int i = 0; i < graph[node].size(); i++) {
			dp[graph[node][i]] += dp[node];
			krawedzie[graph[node][i]]--;
			if(krawedzie[graph[node][i]] == 0) q.push(graph[node][i]);
		}
	}
	int wynik = dp[n];
	for (int i = 1; i <= n; i++) {
		if(krawedzie2[i] == 0) {
			wynik += dp[n] - dp[i];
		}
	}
	cout << wynik << "\n";
}