糯米

TI DaVinci, gstreamer, ffmpeg
随笔 - 167, 文章 - 0, 评论 - 47, 引用 - 0
数据加载中……

POJ 2378 Tree Cutting 动态规划

思路:
每个节点记录在它以下的所有孩子的数目。后序遍历就比较容易得出结果了。

#include <stdio.h>

struct node {
    
struct node *next;
    
int idx;
}
;
struct node *map[10032], nodes[10032*2];
int N, nodes_cnt, can[10032];

__inline 
void add(int a, int b)
{
    
struct node *= &nodes[nodes_cnt++];
    p
->idx = b;
    p
->next = map[a];
    map[a] 
= p;
}


int dfs(int idx, int parent)
{
    
struct node *p;
    
int sum, i, res;

    sum 
= 1;
    res 
= 1;
    
for (p = map[idx]; p; p = p->next) {
        
if (p->idx == parent)
            
continue;
        i 
= dfs(p->idx, idx);
        
if (i > N/2)
            res 
= 0;
        sum 
+= i;
    }

    
if (N - sum > N/2)
        res 
= 0;
    can[idx] 
= res;
    
return sum;
}


int main()
{
    
int i, a, b;

    freopen(
"e:\\test\\in.txt""r", stdin);

    scanf(
"%d"&N);
    
for (i = 0; i < N - 1; i++{
        scanf(
"%d%d"&a, &b);

        add(a, b);
        add(b, a);
    }

    dfs(
10);
    
for (i = 1; i <= N; i++
        
if (can[i])
            printf(
"%d\n", i);

    
return 0;
}

posted on 2010-03-09 15:47 糯米 阅读(452) 评论(2)  编辑 收藏 引用 所属分类: POJ

评论

# re: POJ 2378 Tree Cutting 动态规划  回复  更多评论   

我也是在用cppblog的博客。请问像您这样的网页效果是怎么弄出来的啊?我也想把主页整成这样
2010-08-19 10:01 | 崔佳星

# re: POJ 2378 Tree Cutting 动态规划[未登录]  回复  更多评论   

@崔佳星
发表随笔,并且选中那个“显示在主页”。就这样吧。
2010-08-19 12:16 | 糯米

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