P2853 [USACO06DEC]Cow Picnic S

简介: P2853 [USACO06DEC]Cow Picnic S

题目描述

20210511212557928.png

输入

2 4 4
2
3
1 2
1 4
2 3
3 4

输出 #1复制

2


思路:DFS+图的存储(vector构建的邻接表). 从K个奶牛所在的地方进行DFS遍历,最后只有book[i] = k的牧场满足条件.

参考代码

#include<bits/stdc++.h>
using namespace std;
struct edge{
  int u,v;
  edge(int u1,int v1):u(u1),v(v1){
  };
};
int visited[1000+10],k,n,m,arr[105],uu,vv,cnt[1000+10],total;//visited:用于标记是否走过,book:用于计算遍历次数 
vector<int> e[1000+5];
void dfs(int x){
  visited[x] = 1;
  cnt[x]++;
  for(int i = 0; i < e[x].size(); i++){
    if(!visited[e[x][i]]){
      dfs(e[x][i]);
    }
  }
}
int main()
{
  cin>>k>>n>>m;
  for(int i = 1; i <= k; i++){
    cin>>arr[i];
  }
  for(int i = 0; i < m; i++){
    cin>>uu>>vv;
    e[uu].push_back(vv);
  }
  for(int i = 0; i < k; i++){
    memset(visited,0,sizeof(visited));
    dfs(arr[i]);
  }
  for(int i = 1; i <= n; i++){
    if(cnt[i]==k){
      total++;
    }
  }
  cout<<endl; 
  cout<<total<<endl;
  return 0;
}
相关文章
|
9月前
CF1342B Binary Period(数学分析)
CF1342B Binary Period(数学分析)
16 0
|
11月前
[USACO 2021.02 Feb]Problem 1. Year of the Cow
[USACO 2021.02 Feb]Problem 1. Year of the Cow
|
11月前
[USACO 2021.02 Feb]Problem 3. Clockwise Fence
[USACO 2021.02 Feb]Problem 3. Clockwise Fence
|
18天前
|
关系型数据库 调度 索引
xv6(2) 启动代码部分
启动代码部分
62 1
|
9月前
|
BI
[USACO 2009 Dec S]Music Notes
[USACO 2009 Dec S]Music Notes
|
9月前
[USACO 2010 Feb S]Chocolate Eating
[USACO 2010 Feb S]Chocolate Eating
|
11月前
[USACO 2021.02 Feb]Problem 2. Comfortable Cows
[USACO 2021.02 Feb]Problem 2. Comfortable Cows
luogu CF776D The Door Problem(2-sat问题)
luogu CF776D The Door Problem(2-sat问题)
48 0
POJ3678——Katu Puzzle(2-SAT)
POJ3678——Katu Puzzle(2-SAT)
102 0
POJ3678——Katu Puzzle(2-SAT)

热门文章

最新文章