1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
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.
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
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
牧场3,4是这样的牧场.
HINT
Source
题解:尼玛这道题居然都被卡了一次——原因很逗比,因为中间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 奶牛野餐的更多相关文章
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- 【BZOJ】1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1648 水题.. dfs记录能到达的就行了.. #include <cstdio> #in ...
- 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 ...
- bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐【dfs】
从每个奶牛所在草场dfs,把沿途dfs到的草场的con都+1,最后符合条件的草场就是con==k的,扫一遍统计一下即可 #include<iostream> #include<cst ...
- bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐
Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...
- 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 ...
- 【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 ...
随机推荐
- shell 命令合并文本
之前想把代码打印出来看来着,后来合并完之后放在word里发现有2000多页,然后放弃了~anyway,这个命令还是挺有用的. 比如我有文本a001.dat, a002.dat, a003.dat .. ...
- Java中正则表达式的几种用法
多数内容转载自:http://www.jb51.net/tools/regex.htm ,有改动 用到了java.util.regex包: 1. 验证 Pattern pattern = Patter ...
- Swiper.js使用遇到的问题总结onSlideChangeEnd回调偶尔触发,偶尔不触发等;
Swiper 是一个开源免费的移动触摸插件. 在使用中遇到这样一个问题,记录一下. page 间切换效果 使用 fade 的时候,如果每个页面的大小不一样, 比如第一个页面全屏, 第二个页面比第一个小 ...
- python 基础学习小记
Python应该是写起来最舒服的动态语言了,一下是一些读书笔记,本文中安装的是3.0,有几点需要注意: print "xxx" 要换成 print("xxx") ...
- W3Cschool学习笔记——XHTML基础教程
XHTML 是更严格更纯净的 HTML 代码. XHTML 是什么? XHTML 指可扩展超文本标签语言(EXtensible HyperText Markup Language). XHTML 的目 ...
- 【Java】Objects 源码学习
2017-02-10 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6387321.html 本篇概要 Objects 与 Object ...
- 【译】JavaScript Promise API
原文地址:JavaScript Promise API 在 JavaScript 中,同步的代码更容易书写和 debug,但是有时候出于性能考虑,我们会写一些异步的代码(代替同步代码).思考这样一个场 ...
- Vuex 模块化与项目实例 (2.0)
Vuex 强调使用单一状态树,即在一个项目里只有一个 store,这个 store 集中管理了项目中所有的数据以及对数据的操作行为.但是这样带来的问题是 store 可能会非常臃肿庞大不易维护,所以就 ...
- C# 数据类型 数据转换 自己的见解和方式
数据类型分为:基本数据类和引用类型, 基本数据类型又分为整型,浮点型,字符型,布尔型. 引用类型又分为:字符串型,日期时间,枚举类型,结构类型. int long float = 10.5f; dou ...
- js设计模式--工厂模式
工厂模式: 工厂模式的目的是为了创建对象,它经常是在类和类的方法中实现.简单的工厂模式是由一个方法来决定到底要创建哪类的实例,这些实例经常拥有相同的接口,这种模式在所实例化的类型在编译期并不确定,而是 ...