You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.6 KiB
49 lines
1.6 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const int N = 1010; //牧场数上限
|
|
|
|
int n; //n个牧场
|
|
int m; //m条有向路连接
|
|
int K; //k只奶牛
|
|
int ans; //ans为最终答案
|
|
int g[N][N]; //g数组为邻接矩阵
|
|
int a[N]; //a数组存储牛的位置
|
|
int sum[N]; //sum数组为每个点被遍历的次数
|
|
bool st[N]; //st数组用来判断点是否已经被访问过
|
|
|
|
//进行图的深度优先遍历
|
|
void dfs(int x) {
|
|
st[x] = true; //将现在访问的点标记为已遍历,防止走回头路
|
|
sum[x]++; //将这个点遍历的次数+1
|
|
//枚举节点编号
|
|
for (int i = 1; i <= n; i++)
|
|
//如果当前节点没有被访问过并且与当前节点有边连接
|
|
if (!st[i] && g[x][i]) dfs(i);//就遍历i号节点
|
|
}
|
|
|
|
int main() {
|
|
cin >> k >> n >> m;
|
|
for (int i = 1; i <= k; i++) cin >> a[i];//输入每只奶牛的顺序
|
|
|
|
//使用邻接矩阵保存数据边
|
|
for (int i = 1; i <= m; i++) {
|
|
int u, v;
|
|
cin >> u >> v;
|
|
//连接两边(注意不是双向边,是单向边)
|
|
g[u][v] = 1;
|
|
}
|
|
//对奶牛的位置进行枚举
|
|
for (int i = 1; i <= k; i++) {
|
|
dfs(a[i]); //从每一只奶牛的位置开始遍历
|
|
memset(st, false, sizeof(st)); //记得每次遍历完都需要清空标记数组
|
|
}
|
|
//统计答案,如果当前节点被访问的次数恰好为奶牛的只数
|
|
for (int i = 1; i <= n; i++)
|
|
if (sum[i] == k) ++ans;
|
|
|
|
//输出最后答案
|
|
cout << ans << endl;
|
|
return 0;//完美结束
|
|
} |