2060: [Usaco2010 nov]visiting cows visit cow time limit:3 Sec Memory limit:64 MB
submit:252 solved:185
[Submit] [Status] Description After weeks of hard work, Bessie finally ushered in a holiday. As the most sociable cow in the herd, she wants to visit N (1<=n<=50000) friends. These friends are labeled as 1. N. These cows have an unusual transport system with N-1 roads, each connecting a pair of cows numbered C1 and C2 (1 <= C1 <= N; 1 <= C2 <= N; C1<>C2). In this way, there is a unique pathway between each pair of cows. FJ hoped that Bessie would return to the farm as soon as possible. So he instructed Bessie to visit only one of the two cows that were directly connected to a road. Of course, Bessie wanted her vacation to be as long as possible, so she wanted to know the maximum number of cows she could visit. Input line 1th: A single integer n 2nd. N rows: Two integers per line representing a single integer of C1 and C2.output of a road, representing the maximum number of cows that Bessie can visit. Sample Input7
6 2
3 4
2 3
1 2
7 6
5 6
INPUT DETAILS:
Bessie knows 7 cows. Cows 6 and 2 is directly connected by a road,
As is 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 Output4
OUTPUT DETAILS:
Bessie can visit four cows. The best combinations include the cows
On the top row and both 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 the cows on the top row: {1,3}, {1,4}, or
{2,4}.
HINT Source
Gold
The root of the puzzle: tree-like DP, tree-like DP, according to the following sub-nodes selected and not selected to transfer the finished
1/**************************************************************2Problem:20603 User:hansbug4 language:pascal5 result:accepted6Time: theMs7Memory:2576KB8****************************************************************/9 Ten type OnePoint=^node; ANode=Record - G:longint; - Next:point; the End; -Vec=Record - A0,a1:longint; - End; + var - I,j,k,l,m,n:longint; +A:Array[0..100000] ofPoint ; AB:Array[0..100000] ofLongint; at T:vec; - functionMax (x,y:longint): Longint;inline; - begin - ifX>y ThenMax:=xElsemax:=y; - End; - procedureAdd (x,y:longint); inline; in varP:point; - begin toNew (p);p ^.g:=y; +p^.next:=a[x];a[x]:=p; - End; the functionDFS (x:longint): Vec;inline; * varP:point;t,v:vec; $ beginPanax Notoginsengb[x]:=1; -p:=A[x]; thet.a0:=0; t.a1:=1; + whileP<>Nil Do A begin the ifb[p^.g]=0 Then + begin -v:=DFS (P^.G); $t.a1:=t.a1+v.a0; $t.a0:=t.a0+Max (v.a1,v.a0); - End; -p:=P^.next; the End; - exit (t);Wuyi End; the begin - READLN (n); Wu fori:=1 toN Doa[i]:=Nil; -Fillchar (b,sizeof (b),0); About fori:=1 toN-1 Do $ begin - readln (j,k); - Add (j,k); add (k,j); - End; AT:=dfs (1); + Writeln (Max (t.a0,t.a1)); the End.
2060: [Usaco2010 nov]visiting cows visiting cows