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 itself).
The cows want to gather in the same pasture for their picnic, but (because of the one-way paths) some cows may only be able to get to some pastures. Help the cows out by figuring out how many pastures are reachable by all cows, and hence are possible picnic locations.
K(1≤K≤100)只奶牛分散在N(1≤N≤1000)个牧场.现在她们要集中起来进餐.牧场之间有M(1≤M≤10000)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?
输入输出格式
输入格式:
Line 1: Three space-separated integers, respectively: K, N, and M
Lines 2..K+1: Line i+1 contains a single integer (1..N) which is the number of the pasture in which cow i is grazing.
Lines K+2..M+K+1: Each line contains two space-separated integers, respectively A and B (both 1..N and A != B), representing a one-way path from pasture A to pasture B.
输出格式:
Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.
输入输出样例
输入样例#1:
2 4 4 2 3 1 2 1 4 2 3 3 4
输出样例#1:
2 我可怜的dfs、、、50分,剩下的点全T了、、
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 11000 using namespace std; bool vis[N],vist[N]; int k,n,m,x,y,ans,tot,a[N],sum[N],head[N]; int read() { int x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } struct Edge { int to,next,from; }edge[N]; int add(int x,int y) { tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot; } void dfs(int x) { if(vis[x]) return ; vis[x]=true;vist[x]=true; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(!vis[to]) dfs(to); } vis[x]=false; } int main() { k=read(),n=read(),m=read(); for(int i=1;i<=k;i++) a[i]=read(); for(int i=1;i<=m;i++) x=read(),y=read(),add(x,y); for(int i=1;i<=k;i++) { memset(vist,0,sizeof(vist)); dfs(a[i]); for(int i=1;i<=n;i++) if(vist[i]) sum[i]++; } for(int i=1;i<=n;i++) if(sum[i]==k) ans++; printf("%d",ans); return 0; }
dfs
转bfs
我们枚举每一个牛的起始点,用bfs拓展一下它都能到哪里,然后让这个点的计数加1,
最后看一下都有多少点的计数是k就好了嘛。
这样看起来好像跟最短路的思路差不多,不过不用反向建边,我觉得应该更好想吧,而且拓展可达点这种问题不应该就用bfs而不是dfs吗
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 11000 using namespace std; queue<int>q; bool vis[N]; int k,n,m,x,y,ans,tot,a[N],sum[N],head[N]; int read() { int x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } struct Edge { int to,next,from; }edge[N]; int add(int x,int y) { tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot; } int main() { k=read(),n=read(),m=read(); for(int i=1;i<=k;i++) a[i]=read(); for(int i=1;i<=m;i++) x=read(),y=read(),add(x,y); for(int i=1;i<=k;i++) { memset(vis,0,sizeof(vis)); q.push(a[i]);vis[a[i]]=true; while(!q.empty()) { int x=q.front();q.pop(); for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(!vis[to]) { vis[to]=true; q.push(to); } } } for(int i=1;i<=n;i++) if(vis[i]) sum[i]++; } for(int i=1;i<=n;i++) if(sum[i]==k) ans++; printf("%d",ans); return 0; }
AC的bfs