题解:尼玛这道题居然都被卡了一次——原因很逗比,因为中间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.