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.

 

 

转载于:https://www.cnblogs.com/HansBug/p/4470403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值