P2853 [USACO06DEC]牛的野餐Cow Picnic

-------------------------

长时间不写代码了,从学校中抽身出来真的不容易啊

------------------------

链接:Miku

-----------------------

这道题的思路就在于建反图,如果每一头牛都能到达的话,那么在反图上,这个点也一定能到达每一头牛。

那么我们的目的就明确了,找到所有能在反图上找到每一头牛的点。

-----------------------

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int p;
int head[1001];
int f;
struct b{
    int ne;
    int to;
} e[10005];
int k,n,m;
int con;
int cow[1001];
int x,y;
int ans;
int sum;
int vis[1001];
void add(int f,int t){
    p++;
    e[p].ne=head[f];
    e[p].to=t;
    head[f]=p;
}
void dfs( int now){
    if(vis[now]||f)
    return ;
    vis[now]=1;
    sum+=cow[now];
    if(sum==k){
        f=1;
        return ;
    }
    for(int i=head[now];i;i=e[i].ne){
        int v=e[i].to;
        dfs(v);
    }
    return ;
}
int main(){
    cin>>k>>n>>m;
    for(int i=1;i<=k;++i){
        cin>>x;
        cow[x]++;
    }
    for(int i=1;i<=m;++i){
        cin>>y>>x;
        add(x,y);
    }
    for(int i=1;i<=n;++i){
        f=0;
        sum=0;
        memset(vis,0,sizeof(vis));
        dfs(i);
        if(f)
        ans++;
    }
    cout<<ans;
    return 0;
}

Ac

原文地址:https://www.cnblogs.com/For-Miku/p/12203636.html

时间: 2024-10-15 08:38:37

P2853 [USACO06DEC]牛的野餐Cow Picnic的相关文章

洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic dfs 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k,p[10000],can[10000]; 4 int w[1000+15][1000+15]; 5 bool vis[10000]; 6 7 void dfs(int pre) 8 { 9 for(int j=1;j<=n;j++) 10 { 11 if(w[pre][j]&&!

洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way path

bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走过的点就是符合条件的点. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define N 1002 6 int val[N],in[N],k,n,m

洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic

题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way paths (no path connects a pasture to

[USACO06DEC]牛的野餐Cow Picnic DFS

题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way paths (no path connects a pasture to

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[Submit][Status] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N <= 1,000)

[BZOJ1648][Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 781  Solved: 483 [Submit][Status][Discuss] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N &

BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

直接从每个奶牛所在的farm dfs , 然后算一下.. ---------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<vector> #define rep( i ,

【题解】P2854 [USACO06DEC]牛的过山车Cow Roller Coaster

P2854 [USACO06DEC]牛的过山车Cow Roller Coaster 题目描述 The cows are building a roller coaster! They want your help to design as fun a roller coaster as possible, while keeping to the budget. The roller coaster will be built on a long linear stretch of land o