P1574: [Usaco2009 Jan]地震损坏Damage
卧槽卧槽卧槽,这道水题竟然让我WA了两遍!!评测系统卡了然后手贱又提交了一次,然后就悲催了呜呜。。
把与不能回家但牛棚完好的牛相邻的牛棚赋值为不能走(false),可以证明,如果该牛回不了家,则周围一圈的牛要么牛棚坏了,要么也是一样回不了,所以结果就是,该牛周围的牛都回不了牛庄,所以是一道贪心题,最后来一遍bfs搜索有多少牛可以回去,总数一减就是答案了。
1 type
2 node=record
3 f,t:longint;
4 end;
5 var p,c,n,i,j,f,t,sum:longint;
6 head:array[0..30001] of longint;
7 can:array[0..30001] of boolean;
8 b:array[0..200001] of node;
9 q:array[0..6000000] of longint;
10 procedure insert(m,f,t:longint);
11 begin
12 b[m].f:=head[f];
13 b[m].t:=t;
14 head[f]:=m;
15 end;
16 procedure delete(j:longint);
17 var nowe:longint;
18 begin
19 nowe:=head[j];
20 while nowe<>0 do
21 begin
22 can[b[nowe].t]:=false;
23 nowe:=b[nowe].f;
24 end;
25 end;
26 procedure bfs;
27 var l,r,now,nowe:longint;
28 begin
29 l:=1; r:=1; q[1]:=1; can[1]:=false;
30 while l<=r do
31 begin
32 now:=q[l];
33 nowe:=head[now];
34 while nowe<>0 do
35 begin
36 if can[b[nowe].t] then begin
37 //writeln(b[nowe].t);
38 can[b[nowe].t]:=false;
39 inc(r);
40 //inc(sum);
41 q[r]:=b[nowe].t;
42 end;
43 nowe:=b[nowe].f;
44 end;
45 inc(l);
46 end;
47 sum:=r;
48 end;
49 begin
50 readln(p,c,n);
51 fillchar(can,sizeof(can),true);
52 for i:=1 to c do
53 begin
54 readln(f,t);
55 if f=t then continue;
56 insert(2*i-1,f,t);
57 insert(2*i,t,f);
58 end;
59 for i:=1 to n do
60 begin
61 readln(j);
62 can[j]:=false;
63 delete(j);
64 end;
65 bfs;
66 writeln(p-sum);
67 end.
1 type 2 node=record 3 f,t:longint; 4 end; 5 var p,c,n,i,j,f,t,sum:longint; 6 head:array[0..30001] of longint; 7 can:array[0..30001] of boolean; 8 b:array[0..200001] of node; 9 q:array[0..6000000] of longint; 10 procedure insert(m,f,t:longint); 11 begin 12 b[m].f:=head[f]; 13 b[m].t:=t; 14 head[f]:=m; 15 end; 16 procedure delete(j:longint); 17 var nowe:longint; 18 begin 19 nowe:=head[j]; 20 while nowe<>0 do 21 begin 22 can[b[nowe].t]:=false; 23 nowe:=b[nowe].f; 24 end; 25 end; 26 procedure bfs; 27 var l,r,now,nowe:longint; 28 begin 29 l:=1; r:=1; q[1]:=1; can[1]:=false;//sum:=1; 30 while l<=r do 31 begin 32 now:=q[l]; 33 nowe:=head[now]; 34 while nowe<>0 do 35 begin 36 //if (b[nowe].t=4) and (can[b[nowe].t]) then writeln('sb'); 37 if can[b[nowe].t] then begin 38 //writeln(b[nowe].t); 39 can[b[nowe].t]:=false; 40 inc(r); 41 //inc(sum); 42 q[r]:=b[nowe].t; 43 end; 44 nowe:=b[nowe].f; 45 end; 46 inc(l); 47 end; 48 sum:=r; 49 end; 50 begin 51 readln(p,c,n); 52 fillchar(can,sizeof(can),true); 53 for i:=1 to c do 54 begin 55 readln(f,t); 56 if f=t then continue; 57 insert(2*i-1,f,t); 58 insert(2*i,t,f); 59 end; 60 for i:=1 to n do 61 begin 62 readln(j); 63 can[j]:=false; 64 delete(j); 65 end; 66 //for i:=1 to p do 67 //if can[i] then writeln('t') 68 //else writeln('f'); 69 bfs; 70 writeln(p-sum); 71 end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)