输入:
2 4 4
2
3
1 2
1 4
2 3
3 4
输出:
2
代码如下:
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1010;
bool vis[N];
vector<int>v[N];
int r[N];
int mk[N];void dfs(int u) {mk[u]++;for (int i = 0; i < v[u].size(); i++) {if (!vis[v[u][i]]) {vis[v[u][i]] = true;dfs(v[u][i]);}}
}int main() {int n, m, k;cin >> k >> n >> m;for (int i = 1; i <= k; i++)cin >> r[i];for (int i = 1; i <= m; i++) {int a, b;cin >> a >> b;v[a].push_back(b);}for (int i = 1; i <= k; i++) {memset(vis, 0, sizeof(vis));vis[r[i]] = true;dfs(r[i]);}int ans = 0;for (int i = 1; i <= n; i++) {if (mk[i] == k)ans++;}cout << ans << endl;return 0;
}