树形dp..水

------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define REP( x ) for( edge* e = head[ x ] ; e ; e = e -> next )
 
using namespace std;
 
const int maxn = 50000 + 5;
 
struct edge {
int to;
edge* next;
};
 
edge* pt , EDGE[ maxn << 1 ];
edge* head[ maxn ];
 
void edge_init() {
pt = EDGE;
clr( head , 0 );
}
 
void add( int u , int v ) {
pt -> to = v;
pt -> next = head[ u ];
head[ u ] = pt++;
}
#define add_edge( u , v ) add( u , v ) , add( v , u )
 
 
int d[ maxn ][ 2 ];
 
int dp( int x , int k , int fa ) {
int &ans = d[ x ][ k ];
if( ans != -1 )
   return ans;
ans = 0;
if( k ) {
   REP( x ) if( e -> to != fa )
       ans += dp( e -> to , 0 , x );
   ans++;
} else {
REP( x ) if( e -> to != fa ) 
   ans += max( dp( e -> to , 1 , x ) , dp( e -> to , 0 , x ) );
}
return ans;
}
  
int main() {
    freopen( "test.in" , "r" , stdin );
    
    int n;
    cin >> n;
    edge_init();
    rep( i , n - 1 ) {
    int u , v;
    scanf( "%d%d" , &u , &v );
    u-- , v--;
    add_edge( u , v );
    }
    clr( d , -1 );
    cout << max( dp( 0 , 0 , -1 ) , dp( 0 , 1 , -1 ) ) << "\n";
return 0;
}

------------------------------------------------------------------------

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

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 267  Solved: 198
[Submit][Status][Discuss]

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

BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )的更多相关文章

  1. bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】

    设f[u][0/1]为u这个点不选/选,转移的时候从儿子转移,f[u][1]=sum(f[son][0])+1,f[u][0]=sum(max(f[son][0],f[e[i].to][1])) #i ...

  2. BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 树形DP

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

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

    2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 252  Solved: 1 ...

  4. 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2060 裸的树形dp d[x][1]表示访问x的数量,d[x][0]表示不访问x的数量 d[x][1] ...

  5. 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

    [算法]树形DP [题解]没有上司的舞会?233 f[x][0]=∑max(f[v][0],f[v][1]) f[x][1]=(∑f[v][0])+1 #include<cstdio> # ...

  6. [bzoj2060][Usaco2010 Nov]Visiting Cows 拜访奶牛_树形dp

    Visiting Cows 拜访奶牛 bzoj-2060 Usaco-2010 Nov 题目大意:题目链接. 注释:略. 想法:看起来像支配集. 只是看起来像而已. 状态:dp[pos][flag]表 ...

  7. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  8. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛 树形dp

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  9. [codevs1380]没有上司的舞会([BZOJ2060][Usaco2010 Nov]Visiting Cows 拜访奶牛)

    [codevs1380]没有上司的舞会 试题描述 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现 ...

随机推荐

  1. 手机浏览器下IScroll中click事件

    产品的h5页面几乎都使用了iscroll插件,如果a标签在iscroll里,在部分手机浏览器中会出现无法点击的情况,不管是绑定click事件还是使用a标签的href属性.href属性偶尔还会能点击,c ...

  2. bzoj 1196: [HNOI2006]公路修建问题 二分+并查集

    题目链接 1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1576  Solved: 909[Submit ...

  3. Git Version recovery command introduction - git reset

    reset命令有3种方式: git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 git re ...

  4. Java解析器

    http://www.infoq.com/cn/articles/HIgh-Performance-Parsers-in-Java-V2?utm_source=infoq&utm_medium ...

  5. BZOJ 1005 明明的烦恼 (组合数学)

    题解:n为树的节点数,d[ ]为各节点的度数,m为无限制度数的节点数. 则               所以要求在n-2大小的数组中插入tot各序号,共有种插法: 在tot各序号排列中,插第一个节点的 ...

  6. Java-多线程的实现与启动

    class mythread extends Thread  //多线程的启动 {  private String name;  public mythread(String name)  {   t ...

  7. linux64位系统中g++4.4.7下使用wcsncpy函数有造成段错误的BUG(其它g++版本号未知)

    一.描写叙述 眼下已经linux64位系统中g++4.4.7下发现了该bug.其它的版本号还未測试. 经測试,假设传入wcsncpy函数第一个參数(即目标缓冲区)的地址不能被4整除的话就必然会发生段错 ...

  8. SVN同步出现故障

    1.错误描写叙述    同步SVNStatusSubscribe时报告了错误,1中的0个资源已经同步    同步/frame时错误发生:Error getting status for resourc ...

  9. 《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定

    1.TCP的服务 在一个TCP连接中.仅有双方进行彼此通信. TCP通过下列方式来提供可靠性: 1)应用数据被切割成TCP觉得最适合发送的数据块. 这和UDP全然不同,应用程序产生的数据报长度保持不变 ...

  10. git从github下载代码

    Github作为远程仓库的使用详解  http://blog.csdn.net/djl4104804/article/details/50778717 centos local:        通过g ...