POJ 2378 Tree Cutting (DFS)
题目链接: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)的更多相关文章
- POJ 2378 Tree Cutting (树的重心,微变形)
题意: 给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求.求所有这样的点,若没有这样的点,输出NONE. 思路: 只需要拿“求树的重心”的代码改一行就OK了.因 ...
- 【HDU5909】Tree Cutting(FWT)
[HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- POJ 2378.Tree Cutting 树形dp 树的重心
Tree Cutting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2958 Desc ...
- POJ 3009-Curling 2.0(DFS)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 5125 Desc ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- 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 ...
- [ACM] POJ 3740 Easy Finding (DFS)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16202 Accepted: 4349 Description Give ...
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
随机推荐
- JAVA 日志管理
http://www.360doc.com/content/10/0704/05/2017726_36768082.shtml http://javacrazyer.iteye.com/blog/11 ...
- 使用 github.io 免费建站
/*************************************************************************** * 使用 github.io 免费建站 * 说 ...
- 13行代碼開發出来的PHP框架[转]
<?PHP /** PHP極簡框架 交流: QQ群: 223494678 http://7di.net 用法 http://URL http://URL/hello http://URL/sev ...
- 【Python】入门 list有些不懂
# -*- coding: utf-8 -*- # -*- coding: cp936 -*- 首行加这个 代码里就可以加注释 raw_input("Press Enter Exit&quo ...
- Ant编译环境
使用ant编译有关于C compiler(cc task)的时候,需要确认vs的环境有无配置, 否则会遇到形形色色的问题,比如各种 vs编译工具找不到的错误, 一个比较简单的解决方案,就是在执行ant ...
- 配置dg出现的错误
ORA-09925: Unable to create audit trail file Linux-x86_64 Error: 30: Read-only file system 没有创建adump ...
- 【转】XCode环境变量及路径设置 -- 待学习
原文网址:http://www.cnblogs.com/oc-bowen/p/5140541.html 一般我们在xcode里面配置包含工程目录下头文件的时候,都要关联着相对路径和绝对路径,如果只是自 ...
- 3500常用汉字与标点符号(已排除不支持GB2312的)
.?!,.::“”‘’…()<>〈〉[].,:;!?-'_"'()[]<>|&~;+-*/=<>0123456789ABCEFGHIJKLMNOP ...
- myeclipse 8.5最新注册码(过期时间到2016年)
myeclipse 8.5最新注册码(过期时间到2016年) 这几天myeclipse弹出注册码过期,去网上一搜,要么已过期,要么就剩一两个月.倒腾了半天,自己申请了几个注册码,给大家分享一下 Sub ...
- MySQL · 引擎特性 · InnoDB 崩溃恢复过程
MySQL · 引擎特性 · InnoDB 崩溃恢复过程 在前面两期月报中,我们详细介绍了 InnoDB redo log 和 undo log 的相关知识,本文将介绍 InnoDB 在崩溃恢复时的主 ...