其他分享
首页 > 其他分享> > Cow Picnic S

Cow Picnic S

作者:互联网

P2853 [USACO06DEC]Cow Picnic S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

// 2 4 4
// 2
// 3
// 1 2
// 1 4
// 2 3
// 3 4
// https://www.luogu.com.cn/problem/P2853
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 10000001
int vis[MAX], head[MAX], nums[MAX], idx, k, n, m, cows[MAX], ans;
struct Node
{
    int to, nex;
} edge[MAX];
void dfs(int x)
{
    vis[x] = 1;
    nums[x]++;
    for (int i = head[x]; i; i = edge[i].nex)
    {
        int to = edge[i].to;
        if (!vis[to])
            dfs(to);
    }
}
void add(int u, int v)
{
    edge[++idx] = (Node){v, head[u]};
    head[u] = idx;
}
void input()
{
    cin >> k >> n >> m;
    for (int i = 1; i <= k; i++)
    {
        scanf("%d", cows + i);
    }
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
    }
}
int main()
{
    input();
    for (int i = 1; i <= k; i++)
    {
        memset(vis, 0, sizeof(vis));
        dfs(cows[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        if (nums[i] == k)
            ans++;
    }
    printf("%d", ans);
}

 

标签:head,Picnic,Cow,nums,int,MAX,dfs,vis
来源: https://www.cnblogs.com/Wang-Xianyi/p/16559829.html