1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 432 Solved: 270
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
题解:尼玛这道题居然都被卡了一次——原因很逗比,因为中间BFS当此点访问过时应该跳过,结果我一开始脑抽写了个 if c[p^.g]=1 then continue; 仔细想想,当这种情况下p指针还没等跳到下一个就continue了啊,不死循环才怪!!!(phile:多大了还犯这种错!!!)。。。然后没别的了,就是对于每个牛都用BFS或者DFS来搜一编能够到达的点,然后没了——复杂度才O(K(N+M))肯定没问题。。。(所以一开始当我看到红色的TLE时真心被吓到了QAQ)
1 type
2 point=^node;
3 node=record
4 g:longint;
5 next:point;
6 end;
7
8 var
9 i,j,k,l,m,n,f,r:longint;
10 P:point;
11 a:array[0..1050] of point;
12 b,c,d,e:array[0..1050] of longint;
13 procedure add(x,y:longint);inline;
14 var p:point;
15 begin
16 new(p);
17 p^.g:=y;
18 p^.next:=a[x];
19 a[x]:=p;
20 end;
21 begin
22 readln(e[0],n,m);
23 for i:=1 to n do
24 begin
25 d[i]:=1;
26 a[i]:=nil;
27 end;
28 for i:=1 to e[0] do
29 readln(e[i]);
30 for i:=1 to m do
31 begin
32 readln(j,k);
33 add(j,k);
34 end;
35 for i:=1 to e[0] do
36 begin
37 fillchar(c,sizeof(c),0);
38 fillchar(b,sizeof(b),0);
39 c[e[i]]:=1;
40 b[1]:=e[i];
41 f:=1;r:=2;
42 while f<r do
43 begin
44 p:=a[b[f]];
45 while p<>nil do
46 begin
47 if c[p^.g]=0 then
48 begin
49 c[p^.g]:=1;
50 b[r]:=p^.g;
51 inc(r);
52 end;
53 p:=p^.next;
54 end;
55 inc(f);
56 end;
57 for j:=1 to n do
58 d[j]:=d[j]*c[j];
59 end;
60 l:=0;
61 for i:=1 to n do l:=l+d[i];
62 writeln(l);
63 end.
- 1.开发规范-- 常用的版本控制
- phalcon-入门篇1(基本介绍与环境搭建)
- 基于PhalApi的Redis拓展
- PhalApi-Image -- 图像处理
- 为什么微信红包长这样?
- Visual Studio 2008 每日提示(三十六)
- zephir-(6)运算符
- zephir-(12)php函数和异常处理
- phalapi-进阶篇7(使用缓存以及用redis拓展解决实际问题)
- 5个不为人知的Java API使用技巧
- zephir-(3)你的第一个PHP拓展
- zephir-(7)数组
- phalapi-进阶篇5(数据库读写分离以及多库使用)
- phalapi-进阶篇4(notrom进阶以及事务操作)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- linux sed指令详解
- Grafana使用zabbix自定义模板
- linux shell之变量的使用规则
- 第11期:压缩表
- grafana-zabbix插件安装和配置zabbix mysql
- grafana使用教程之API key
- Grafana使用教程之安装
- Java基础数据类型之包装类equals和==详解
- SCP不用密码传输文件
- Java 使用Collections.reverse对list集合进行降序排序
- Liquibase异常 mysql数据库 Cannot add foreign key constraint
- Linux获取文件最后修改时间
- Crontab脚本无法正常执行问题
- Python 输入时间字符串以分钟单位计算时间差
- Linux下执行bcp指令