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

Source: Internet
Author: User

2060: [Usaco2010 nov]visiting cows visit cow time limit: 3 Sec Memory Limit: MB
Submit: Solved: 256
[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 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&LT;&GT;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< BR style= "Font-family:arial,verdana,helvetica,sans-serif" >
output details:

Bessie can visit four cows. The best combinations include the COWS
on the top row and both O n 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-cows on the top row: {1,3}, {1,4}, or
{2, 4}.

< /span>HINT

Source

Gold

[Submit] [Status] [Discuss]

"Solving" "simple tree-type DP"

F[i][0] indicates that the current point is not selected, and F[i][1] indicates the selection of the current point. Recursive DP "

#include <cstdio> #include <cstring> #include <algorithm>using namespace  std;int  a[1000010 ],next[1000010],p[50010],tot;int f[50010][2];int n,ans;inline void Add (int x,int y) {tot++; a[tot]=y; next[tot]=p[x]; p[ x]=tot;tot++; A[tot]=x; Next[tot]=p[y]; P[y]=tot;return;} inline void dp (int x,int FA) {bool B=false;int u=p[x];while (u!=0) {if (A[U]!=FA)  {  b=true;  DP (A[U],X);  f[x][1]+=f[a[u]][0];//because the current point is selected, so his son can not be selected   F[x][0]+=max (f[a[u]][0],f[a[u]][1]);//Because the current point is not selected, so his son can choose not to choose   u= Next[u]; }return;} int main () {int i,j;scanf ("%d", &n), for (i=1;i<n;++i) {int x, Y, scanf ("%d%d", &x,&y), add (x, y);} for (I=1;i <=n;++i) F[i][1]=1; DP (1,0); Ans=max (f[1][0],f[1][1]); printf ("%d\n", ans); 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.