2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

时间:2022-05-08
本文章向大家介绍2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛,主要内容包括2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛、Description、Input、Output、Sample Input、Sample Output、HINT、Source、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

Time Limit: 3 Sec  Memory Limit: 64 MB

Submit: 252  Solved: 185

[Submit][Status]

Description

经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号为C1和C2的奶牛(1 <= C1 <= N; 1 <= C2 <= N; C1<>C2).这样,在每一对奶牛之间都有一条唯一的通路. FJ希望贝茜尽快的回到农场.于是,他就指示贝茜,如果对于一条路直接相连的两个奶牛,贝茜只能拜访其中的一个.当然,贝茜希望她的假期越长越好,所以她想知道她可以拜访的奶牛的最大数目.

Input

第1行:单独的一个整数N 第2..N行:每一行两个整数,代表了一条路的C1和C2.

Output

单独的一个整数,代表了贝茜可以拜访的奶牛的最大数目.

Sample Input

7 6 2 3 4 2 3 1 2 7 6 5 6 INPUT DETAILS: Bessie knows 7 cows. Cows 6 and 2 are directly connected by a road, as are cows 3 and 4, cows 2 and 3, etc. The illustration below depicts the roads that connect the cows: 1--2--3--4 | 5--6--7

Sample Output

4 OUTPUT DETAILS: Bessie can visit four cows. The best combinations include two cows on the top row and two on the bottom. She can't visit cow 6 since that would preclude visiting cows 5 and 7; thus she visits 5 and 7. She can also visit two cows on the top row: {1,3}, {1,4}, or {2,4}.

HINT

Source

Gold

 题解:树状DP啦啦树状DP,按照下面的子节点选和不选进行转移完事

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