poj2186 Popular Cows
2016-06-05 18:55
399 查看
Description
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive,
if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Line 1: Two space-separated integers, N and M
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
* Line 1: A single integer that is the number of cows who are considered popular by every other cow.
题解:
首先用Tarjan求出联通分量的个数,然后依次求各个联通分量的出度,如果仅有一个连通分量出度为0则这个联通分量内的点的个数就是答案;如果有多于一个的联通分量的出度为0,则说明此有向图肯定不连通。因此直接输出0。
代码:
procedure dfs(k:longint);
var
tmp:longint;
begin
inc(d);
l[k]:=d;
f1[k]:=d;
v[k]:=true;
inc(t);
s[t]:=k; f[k]:=true;
temp:=h[k];
while temp<>-1 do
begin
if v[e[temp].t]=false then
begin
dfs(e[temp].t);
l[k]:=min(l[k],l[e[temp].t]);
end
else
if f[e[temp].t] then l[k]:=min(l[k],f1[e[temp].t]);
temp:=e[temp].next;
end;
if f1[k]=l[k] then begin inc(ans); main(k); end;
end;
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive,
if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Line 1: Two space-separated integers, N and M
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
* Line 1: A single integer that is the number of cows who are considered popular by every other cow.
题解:
首先用Tarjan求出联通分量的个数,然后依次求各个联通分量的出度,如果仅有一个连通分量出度为0则这个联通分量内的点的个数就是答案;如果有多于一个的联通分量的出度为0,则说明此有向图肯定不连通。因此直接输出0。
代码:
procedure dfs(k:longint);
var
tmp:longint;
begin
inc(d);
l[k]:=d;
f1[k]:=d;
v[k]:=true;
inc(t);
s[t]:=k; f[k]:=true;
temp:=h[k];
while temp<>-1 do
begin
if v[e[temp].t]=false then
begin
dfs(e[temp].t);
l[k]:=min(l[k],l[e[temp].t]);
end
else
if f[e[temp].t] then l[k]:=min(l[k],f1[e[temp].t]);
temp:=e[temp].next;
end;
if f1[k]=l[k] then begin inc(ans); main(k); end;
end;
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)