您的位置:首页 > 其它

Information(图论算法)

2016-04-08 21:46 302 查看

Description

It isa war between Country Alpha and Country Beta. Country Alpha getsfollowing information about Country Beta:CountryBeta has n (2 <= n <= 100) cities numbered from 0 to n-1.Each city has a transmitter and a receiver. One city is able totransmit important information by its transmitter to another city.Meanwhile, if there's information transmitted to one city, it isable to receive the information by its receiver. Of course, it isOK that one city doesn't transmit any information to another cityand that if no information transmitted to one city, the cityreceives no information. Thus, a quantity of cities are able toconstitute a group. In a group, any city is able to transmit theinformation to any city in the group directly or indirectly. Ofcourse, one city belongs to only one group.Now, Country Alpha wants to demolish onecity of Country Beta. Thus, Alpha will make the number of cities inthe largest group as small as possible.

Input

Therare multiple test cases (about 200 cases). There is a blank linebetween each case.The first line of each case contains twopositive integers, n and m (0 <= m <= 9900), in which mindicates the number of the path(es) of the information from onecity to another. In each line of following m line(s), there are twopositive integers, s, t, indicating that some information is fromCity s to City t. No same pair of (s,t).

Output

Output the least number of cities in agroup, which is the largest group after Country Alpha's attack. Bythe way, a group must consist of at least two cities, that is, onecity is not able to be considered as a group. If there is no group,output 0.

SampleInput

2 2
0 1
1 0

SampleOutput

0
解题思路:其实这道题就是求强连通分量,可以用Kosaraju算法,首先利用深度优先搜索遍历原图,并按出栈顺序的先后顺序把点放进一个线性表里,这里可以每次取任意一个点出发DFS,如果还有点未被DFS到,那么继续任选未被遍历到的点出发DFS,然后将整个图转置,按照线性表中的出栈顺序,从后往前作为起点DFS。每次DFS到的就是一个强连通分量。
程序:
var
a,b:array[0..100,0..100] of longint;
f:array[1..100] of longint;
v:array[0..100] of boolean;
f1,n,s,m,x,y,i,j,max,min:longint;
procedure dfs1(x:longint);
var
i:longint;
begin
v[x]:=false;
for i:=0 to n-1 do
if (a[x,i]=1)and(v[i]) then dfs1(i);
inc(f1);
f[f1]:=x;
end;
procedure dfs2(x:longint);
var
i:longint;
begin
v[x]:=false;
for i:=0 to n-1 do
if (b[x,i]=1)and(v[i]) then dfs2(i);
inc(s);
end;
begin
while not eof do
begin
readln(n,m);
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
for i:=1 to m do
begin
readln(x,y);
a[x,y]:=1;
b[y,x]:=1;
end;
min:=maxlongint;
for i:=0 to n-1 do
begin
fillchar(v,sizeof(v),true);
v[i]:=false;
f1:=0;
for j:=0 to n-1 do
if v[j] then dfs1(j);
fillchar(v,sizeof(v),true);
v[i]:=false;
max:=0;
for j:=f1 downto 1 do
if v[f[j]] then
begin
s:=0;
dfs2(f[j]);
if s>max then max:=s;
end;
if (max1) then min:=max;
if max<2 then begin min:=0; break; end;
if min=0 then break;
end;
if min=maxlongint then writeln(0) else writeln(min);
end;
end.
版权属于: Chris原文地址: http://blog.sina.com.cn/s/blog_83ac6af80102v3gn.html转载时必须以链接形式注明原始出处及本声明。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: