1907: 树的路径覆盖

1907: 树的路径覆盖

Time Limit: 5 Sec  Memory Limit: 259 MB
Submit: 506  Solved: 219
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

1
7
1 2
2 3
2 4
4 6
5 6
6 7

Sample Output

3

HINT

Source

Play with Tree By Amber

 

题解:贪心题么么哒,直接DFS一遍就好啦

 1 /**************************************************************
 2     Problem: 1907
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:392 ms
 7     Memory:3480 kb
 8 ****************************************************************/
 9  
10 type
11     point=^node;
12     node=record
13                g:longint;
14                next:point;
15     end;
16 var
17    i,j,k,l,m,n,tot,t:longint;
18    a:array[0..100000] of point;
19    b:array[0..100000] of longint;
20    c:array[0..100000] of boolean;
21 procedure add(x,y:longint);
22           var p:point;
23           begin
24                new(p);p^.g:=y;p^.next:=a[x];a[x]:=p;
25           end;
26 procedure dfs(y,x:longint);
27           var p:point;tot:longint;
28           begin
29                b[x]:=1;tot:=0;p:=a[x];
30                while p<>nil do
31                      begin
32                           if p^.g<>y then
33                              begin
34                                   dfs(x,p^.g);
35                                   inc(b[x],b[p^.g]);
36                                   if not(c[p^.g]) then inc(tot);
37                              end;
38                           p:=p^.next;
39                      end;
40                if tot>=2 then
41                   begin
42                        dec(b[x],2);
43                        c[x]:=true;
44                   end
45                else if tot=1 then dec(b[x]);
46           end;
47 begin
48      readln(t);
49      while t>0 do
50            begin
51                 readln(n);
52                 fillchar(b,sizeof(b),0);
53                 fillchar(c,sizeof(c),false);
54                 for i:=1 to n do a[i]:=nil;
55                 for i:=1 to n-1 do
56                     begin
57                          readln(j,k);
58                          add(j,k);add(k,j);
59                     end;
60                 dfs(0,1);dec(t);
61                 writeln(b[1]);
62            end;
63 end.

 

 

posted @ 2015-05-01 10:07  HansBug  阅读(219)  评论(0编辑  收藏  举报