题目链接:http://poj.org/problem?id=2378

一棵树,去掉一个点剩下的每棵子树节点数不超过n/2。问有哪些这样的点,并按照顺序输出。

dfs回溯即可。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e4 + ;
vector <int> ans;
struct Edge {
int next, to;
}edge[N << ];
int d[N], head[N], cnt, n; inline void add(int u, int v) {
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
} void dfs(int u, int p) {
bool ok = true;
d[u] = ;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs(v, u);
if(d[v] * > n)
ok = false;
d[u] += d[v];
}
if((n - d[u]) * > n)
ok = false;
if(ok)
ans.push_back(u);
} int main()
{
while(~scanf("%d", &n)) {
int u, v;
memset(head, -, sizeof(head));
cnt = ;
ans.clear();
for(int i = ; i < n; ++i) {
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, -);
sort(ans.begin(), ans.end());
for(int i = ; i < ans.size(); ++i) {
printf("%d\n", ans[i]);
}
}
return ;
}

POJ 2378 Tree Cutting (DFS)的更多相关文章

  1. POJ 2378 Tree Cutting (树的重心,微变形)

    题意: 给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求.求所有这样的点,若没有这样的点,输出NONE. 思路: 只需要拿“求树的重心”的代码改一行就OK了.因 ...

  2. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  3. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  4. POJ 2378.Tree Cutting 树形dp 树的重心

    Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4834   Accepted: 2958 Desc ...

  5. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  6. 题解报告:poj 1321 棋盘问题(dfs)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  7. POJ 2251 Dungeon Master(dfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  8. [ACM] POJ 3740 Easy Finding (DFS)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16202   Accepted: 4349 Description Give ...

  9. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

随机推荐

  1. Web程序员最常用的11款PHP框架

    PHP框架是Web程序员和开发人员最为有用的工具. PHP框架可以帮助用户更快地开发项目. 今天我将为开发人员带来几款最好的PHP框架,希望能对你有用. 1.Agavi Agavi是一款强大的,可扩展 ...

  2. linux修改文件夹及其子文件夹的权限

    加入-R 参数,就可以将读写权限传递给子文件夹例如chmod -R 777 /home/mypackage那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777.777是读.写.执行 ...

  3. 插件svn简单使用

    首先安装服务器Server 傻瓜式样操作.下一步下一步就这样完成了,选择的是个人版. 然后打开Server的图形化界面:VisualSVN Server Manager Repositories:资源 ...

  4. Sublime 的中文乱码问题

    Sublime Text 是现在最受欢迎的文本编辑器,没有之一.它非常简洁,而且对各种代码的高亮显示很美观.但是,它默认不支持 GBK.Shift-JIS 等中文.日本编码格式,故打开此类文件会出现乱 ...

  5. 【C#学习笔记】保存文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. acess() 判断目录是否存在

    acess()功能描述: 检查调用进程是否可以对指定的文件执行某种操作. <pre lang="c" escaped="true">#include ...

  7. RegEx正则表达式学习笔记

    一.实用的例子 public static void main(String[] args) { // 简单练习 System.out.println("-123".matches ...

  8. 【转】linux设备驱动程序中的阻塞机制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275272.html 阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经 ...

  9. activity_main.xml与fragment_main.xml

    见: http://blog.sina.com.cn/s/blog_3e28c8a50101fqvw.html http://blog.sina.com.cn/s/blog_3e28c8a50101f ...

  10. AutoCompleteTextView不能使用的问题

    AutoCompleteTextView按照网络上的方法写之后不能使用 解决方法: android:layout_width="fill_parent" 而不能是wrap_pare ...