"Bzoj 2060" [Usaco2010 nov]visiting cows visit cows

Source: Internet
Author: User

2060: [Usaco2010 nov]visiting cows visiting cows

Time Limit:3 Sec Memory limit:64 MB
submit:257 solved:190
[Submit] [Status] [Discuss]
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 were labeled 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 the C1 and C2 of a route.
Output

A single integer that represents the maximum number of cows that Bessie can visit.
Sample Input

7

6 2

3 4

2 3

1 2

7 6

3 p

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 Output

4

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

    1. She can also visit-cows on the top row: {1,3}, {1,4}, or

{2,4}.

HINT

Source

Gold

Tree-shaped DP water problem.

< Span class= "Mrow" id= "mathjax-span-28109" >f [ i ] [ 0 Span style= "Display:inline-block; width:0px; Height:2.503em; " > Said i Not selected, to i Maximum answer for the subtree of the root;
< Span class= "Mrow" id= "mathjax-span-28124" >f [ i ] [ 1 Span style= "Display:inline-block; width:0px; Height:2.503em; " > Said i Not selected, to i The maximum answer for the root subtree.

F [I][0]=∑max (F [J ][0],F [J ][1])
F [I][1]=1+∑F [J ][0]

#include <iostream>#include <algorithm>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#define M 50005using namespace STD;structedge{intY,ne;} e[m*3];inttot,h[m],f[m][2],n;voidAddedge (intXintY) {e[++tot].y=y;    E[TOT].NE=H[X]; H[x]=tot;}voidDfsintXintFA) {f[x][1]=1, f[x][0]=0; for(intI=h[x];i;i=e[i].ne) {intY=E[I].Y;if(Y==FA)Continue;        DFS (Y,X); f[x][1]+=f[y][0],f[x][0]+=max (f[y][1],f[y][0]); }}intMain () {scanf("%d", &n); for(intI=1; i<n;i++) {intx, y;scanf("%d%d", &x,&y);        Addedge (x, y);    Addedge (Y,X); } DFS (1,0);printf("%d\n", Max (f[1][0],f[1][1]));return 0;}

"Bzoj 2060" [Usaco2010 nov]visiting cows visit cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.