Time Limit:10000MS Memory Limit:65536K
Total Submit:35 Accepted:8
Description
兴趣小组的同学来自各个学校,为了增加友谊,晚会上又进行了一个传话的游戏,如果a认识b,那么a
收到某个消息,就会把这个消息传给b,以及所有a认识的人。
如果a认识b,b不一定认识a。
所有人从1到n编号,给出所有“认识”关系,问如果i发布一条消息,那么会不会经过若干次传话后,这
个消息传回给i,1 <= i <= n。
Input
第一行是两个数n (n < 1000)和m (m < 10000),两数之间有一个空格,表示人数和认识关系。
接下来m行,每行两个数a和b,表示a认识b。1 <= a,b <= n。认识关系可能会重复给出,但一行的两个
数不会相同。
Output
一共有n行,每行一个字符T或F。第i行如果是T,表示i发出一条消息会传回给i;如果是F,表示i发出
一条消息不会传回给i。
Sample Input
4 6
1 2
2 3
4 1
3 1
1 3
2 3
Sample Output
T
T
T
F
Source
NOIdaokan
算法:DFS
挺裸的DFS,从每个点开始做DFS,如果还能到这个点就直接退出,同时表明可以回到点i,否则就是不
能回到原点。
类似于SPFA,只是不用再做松弛操作了。。。。。。
program NDK1046;
const
maxn=1000;
maxm=20000;
type
atp=record
y,next:longint;
end;
var
n,m,head,tail,tot:longint;
que:array [0..maxm] of longint;
first:array [0..maxn] of longint;
b:array [0..maxn] of boolean;
bb:array [0..maxn,0..maxn] of boolean;
map:array [0..maxm] of atp;
procedure init;
var
i,x,y:longint;
begin
readln(n,m);
for i:=1 to m do
begin
readln(x,y);
if not bb[x,y] then
begin
bb[x,y]:=true;
inc(tot);
map[tot].y:=y;
map[tot].next:=first[x];
first[x]:=tot;
end;
end;
end;
function SPFA(x:longint):boolean;
var
t:longint;
begin
fillchar(b,sizeof(b),false);
head:=0;
tail:=1;
que[1]:=x;
b[x]:=true;
while head<tail do
begin
inc(head);
t:=first[que[head]];
while t>0 do
begin
if map[t].y=x then exit(true);
if not b[map[t].y] then
begin
inc(tail);
que[tail]:=map[t].y;
b[map[t].y]:=true;
end;
t:=map[t].next;
end;
end;
exit(false);
end;
procedure main;
var
i:longint;
t:boolean;
begin
for i:=1 to n do
begin
t:=SPFA(i);
if t then writeln('T') else writeln('F');
end;
end;
begin
assign(input,'NDK1046.in'); reset(input);
assign(output,'NDK1046.out'); rewrite(output);
init;
main;
close(input); close(output);
end.
1744

被折叠的 条评论
为什么被折叠?



