posts - 74,  comments - 33,  trackbacks - 0
Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1440 Accepted: 443

Description

Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.

Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ AN; 1 ≤ BN; AB) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.

Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.

Input

* Line 1: A single integer: N
* Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B

Output

* Line 1: A single integer indicating the minimum number of towers to install

Sample Input

5
1 3
5 2
4 3
3 5

Sample Output

2

Source

USACO 2008 January Gold
今天软件工程的时候想通了这道题目成功的把3中状态和DP联系起来,以前思考的时候一直从root想leaves执行程序,而下午仔细想想如果从root出发,而执行程序的时候无法统计最后的点数,更可怕的是不是root影响leaves而是leaves影响着root的取值达到最优值!而这道题就是这样可以计算下利用DFS遍历Tree我们知道DFS时间度为O(n)空间上用邻接表记录关系图,O(n)
说下3中状态转移,我就不讲了,我这人口才不好,看写的比说的清楚,哈哈~~
第一种状态:就是一个节点不放置塔,而它的所有leaves被覆盖,当然它自己能被它的leaves控制。
第二种状态:一个节点和它的所有leaves全被覆盖,而这个节点放置控制塔。
第三种状态:一个节点暂时没有被覆盖,也没有放置节点。
DFS函数如下:
void DFS(int x){
    
int i,sign,flag;
    
for(flag=sign=i=0;i<v[x].size();i++){
        
int t=v[x][i];
           
if(!mark[t]){
            sign
=1;mark[t]=1;
            DFS(v[x][i]);
            
if(dp[t][0]<dp[t][1])dp[x][0]+=dp[t][0];
             
else {
                  dp[x][
0]+=dp[t][1];
                  sign
=1;
             }

             dp[x][
1]+=getmin(getmin(dp[t][0],dp[t][1]),dp[t][2]);
             dp[x][
2]+=dp[t][0];
           }

    }

    
if(!sign)dp[x][0]=dp[x][1]=1;
    
else{
        dp[x][
1]++;
           
if(!flag)dp[x][0]+=1;
    }

}
posted on 2009-04-09 21:45 KNIGHT 阅读(298) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(8)

随笔档案

文章档案

Friends

OJ

搜索

  •  

最新评论

阅读排行榜

评论排行榜