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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 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) 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)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?

Input

* 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.

第1行输入K,N,M.接下来K行,每行一个整数表示一只奶牛所在的牧场编号.接下来M行,每行两个整数,表示一条有向路的起点和终点

Output

* Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.

所有奶牛都可到达的牧场个数

Sample Input

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

INPUT DETAILS:

4<--3
^ ^
| |
| |
1-->2

The pastures are laid out as shown above, with cows in pastures 2 and 3.

Sample Output

2

牧场3,4是这样的牧场.

HINT

 

Source

Silver

题解:尼玛这道题居然都被卡了一次——原因很逗比,因为中间BFS当此点访问过时应该跳过,结果我一开始脑抽写了个 if c[p^.g]=1 then continue; 仔细想想,当这种情况下p指针还没等跳到下一个就continue了啊,不死循环才怪!!!(phile:多大了还犯这种错!!!)。。。然后没别的了,就是对于每个牛都用BFS或者DFS来搜一编能够到达的点,然后没了——复杂度才O(K(N+M))肯定没问题。。。(所以一开始当我看到红色的TLE时真心被吓到了QAQ)

 type
point=^node;
node=record
g:longint;
next:point;
end; var
i,j,k,l,m,n,f,r:longint;
P:point;
a:array[..] of point;
b,c,d,e:array[..] of longint;
procedure add(x,y:longint);inline;
var p:point;
begin
new(p);
p^.g:=y;
p^.next:=a[x];
a[x]:=p;
end;
begin
readln(e[],n,m);
for i:= to n do
begin
d[i]:=;
a[i]:=nil;
end;
for i:= to e[] do
readln(e[i]);
for i:= to m do
begin
readln(j,k);
add(j,k);
end;
for i:= to e[] do
begin
fillchar(c,sizeof(c),);
fillchar(b,sizeof(b),);
c[e[i]]:=;
b[]:=e[i];
f:=;r:=;
while f<r do
begin
p:=a[b[f]];
while p<>nil do
begin
if c[p^.g]= then
begin
c[p^.g]:=;
b[r]:=p^.g;
inc(r);
end;
p:=p^.next;
end;
inc(f);
end;
for j:= to n do
d[j]:=d[j]*c[j];
end;
l:=;
for i:= to n do l:=l+d[i];
writeln(l);
end.

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐的更多相关文章

  1. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

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

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1648 水题.. dfs记录能到达的就行了.. #include <cstdio> #in ...

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

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

  5. bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐【dfs】

    从每个奶牛所在草场dfs,把沿途dfs到的草场的con都+1,最后符合条件的草场就是con==k的,扫一遍统计一下即可 #include<iostream> #include<cst ...

  6. bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

  7. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  8. bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

    Description The cows are building a roller coaster! They want your help to design as fun a roller co ...

  9. 【BZOJ】1649: [Usaco2006 Dec]Cow Roller Coaster(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1649 又是题解... 设f[i][j]表示费用i长度j得到的最大乐趣 f[i][end[a]]=ma ...

随机推荐

  1. Unity3D在NGUI中使用mask

    过程是这样的:最近一直想做一个头像的mask效果,后来发现原来unity的mask需要用shader来写,网上找了不少资料,也能实现,不过大多数都是用render texture作为相机投影的text ...

  2. Flex之HTML5视频播放解决方案

    Flex的video类对于视频播放在ios操作系统下表现出不兼容,采用调用ios源生播放器的思路,那么怎么调呢? 话说Html5 和Flex是竞争关系,这次利用Html5的video标签实现ios播放 ...

  3. OGG学习笔记03-单向复制简单故障处理

    OGG学习笔记03-单向复制简单故障处理 环境:参考:OGG学习笔记02-单向复制配置实例 实验目的:了解OGG简单故障的基本处理思路. 1. 故障现象 故障现象:启动OGG源端的extract进程, ...

  4. Chrome 插件集锦

    原文出处:CN_Simo 子曾曰:"工欲善其事,必先利其器.居是邦也."--语出<论语·卫灵公>:其后一百多年,荀子也在其<劝学>中倡言道:"吾尝 ...

  5. web开发的性能准则(减少页面加载时间方面)

    准则(概述) 减少 HTTP 请求 使用CDN加速 避免空的src或href属性值 增加过期头 启GZIP压缩 把css文件放到头部 把javascript放到尾部 避免使用css表达式 删除不使用的 ...

  6. C++编程练习(10)----“图的最小生成树“(Prim算法、Kruskal算法)

    1.Prim 算法 以某顶点为起点,逐步找各顶点上最小权值的边来构建最小生成树. 2.Kruskal 算法 直接寻找最小权值的边来构建最小生成树. 比较: Kruskal 算法主要是针对边来展开,边数 ...

  7. HTTP学习(一)初识HTTP

    作为一名准前端开发工程师,必须要对http基础知识有一定的了解,可是想学习HTTP相关的知识,发现国内只有两本相关的图书,<HTTP权威指南>和<图解http>,所有的书但凡带 ...

  8. SpringMVC简版教程、部分功能

    注:本文只用注解来实现 前言 SpringMVC各种流程图流程图(其他的各种流程图) jsp.xml.action彼此之间的关系,都如何使用 spring-mvc.xml如何配置,放在哪里? acti ...

  9. californium 框架设计分析

    Californium 源码分析 1. Californium 项目简介 Californium 是一款基于Java实现的Coap技术框架,该项目实现了Coap协议的各种请求响应定义,支持CON/NO ...

  10. url传参后获取参数

    当我们通过url传参跳转到其他页面,如: http://www.xxx.com/content.html?id=217&name=txf&phone=15829087165 在跳转后的 ...