poj 2378 树上的DP

题意:

给一棵n个节点连通的树

条件 : 去掉一个节点 使剩下的每个连通分量 都不超过n/2个节点

让找出所有符合上述条件的点 按照从小到大的顺序输出

思路:

首先建树

然后在建树的过程中,统计每个节点的子树中 最多的节点个数

同时统计以此结点为根的树的 节点总数 sum[i]

可以根据 n - sum[i] 算出上面一个祖先连通分量的节点个数

判断即可

code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<cmath>
using namespace std;

#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-4
#define maxd 10e4
#define mem(a, b) memset(a, b, sizeof(a))
typedef pair<int,int> pii;
typedef long long LL;
//------------------------------
const int maxn = 10005;

vector<int> g[maxn];
int n;
int all[maxn], son[maxn];
int vis[maxn];

void init(){
    int u,v;
    for(int i = 0; i < n-1; i++){
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
}
vector<int> ans;
bool judge(int x){
    if(son[x] <= n/2 && n - all[x] <= n/2) return true;
    return false;
}
int dfs(int u, int fa){
    if(vis[u]) return all[u];
    vis[u] = 1;

    int& ans1 = son[u];
    int& ans2 = all[u];
    ans1 = 0, ans2 = 1;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == fa) continue;
        int tmp = dfs(v, u);
        ans1 = max(ans1, tmp);
        ans2 += tmp;
    }
    if(judge(u)) ans.push_back(u);
    return ans2;
}
void solve(){
    memset(vis, 0, sizeof(vis));
    dfs(1, 0);
    sort(ans.begin(), ans.end());
    for(int i = 0; i < ans.size(); i++){
        printf("%d\n",ans[i]);
    }
}
int main(){
    scanf("%d",&n);
    init();
    solve();
    return 0;
}

(思路写得太多了...)

树一类的题目,应该多考虑利用树的性质来解决问题

时间: 2024-08-05 19:37:36

poj 2378 树上的DP的相关文章

POJ 2378 树状dp

Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3849   Accepted: 2304 Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible

POJ 2342 (树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)

POJ 3616 Milking Time DP题解

典型的给出区间任务和效益值,然后求最大效益值的任务取法. 属于一维DP了. 一维table记录的数据含义:到当前任务的截止时间前的最大效益值是多少. 注意, 这表示当前任务一定要选择,但是最终结果是不一定选择最后一个任务,故此最后需要遍历找到table数组的最大值,当然计算过程中使用一个数记录最终最大值也是可以的. 状态转移方程就是: tbl[i] = MAX({from tbl[0]->tbl[i-1] }+ weight[i] ),即区间0到i-1加上i的当前效益值. #include <

poj 2948 Martian Mining (dp)

题目链接 完全自己想的,做了3个小时,刚开始一点思路没有,硬想了这么长时间,想了一个思路, 又修改了一下,提交本来没抱多大希望 居然1A了,感觉好激动..很高兴dp又有所长进. 题意: 一个row*col的矩阵,每个格子内有两种矿yeyenum和bloggium,并且知道它们在每个 格子内的数量是多少.最北边有bloggium的收集站,最西边有 yeyenum 的收集站. 现在要在这些格子上面安装向北或者向西的传送带(每个格子自能装一种).问最多能采到多少矿. 传送带只能直着走,不可弯曲,不能交

POJ 3017 单调队列dp

Cut the Sequence Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8764   Accepted: 2576 Description Given an integer sequence { an } of length N, you are to cut the sequence into several parts every one of which is a consecutive subseque

Codeforces 487B. Strip(求区间最值+线段树上的dp)

B. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some p