题意:

  给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求。求所有这样的点,若没有这样的点,输出NONE。

思路:

  只需要拿“求树的重心”的代码改一行就OK了。因为依然是在判别最大连通块的点数。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <deque>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x7f7f7f7f
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
int n, edge_cnt, head[N]; struct node
{
int from, to, next;
node(){};
node(int from,int to,int next):from(from),to(to),next(next){};
}edge[N*]; void add_node(int from,int to)
{
edge[edge_cnt]=node(from,to,head[from]);
head[from]=edge_cnt++;
}
deque<int> que; int DFS(int t,int far)
{
node e;
int big=, sum=;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to==far) continue; int tmp=DFS(e.to, t);
big=max(big, tmp);
sum+=tmp;
}
big=max(big, n-sum-); if(big<=n/) que.push_back(t);//只改这处地方
return sum+;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b;
while(~scanf("%d",&n))
{
edge_cnt=;
memset(head,-,sizeof(head)); for(int i=; i<n; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b);
add_node(b,a);
}
DFS(,-);
sort(que.begin(),que.end());
if(que.empty()) puts("NONE");
else
{
while(!que.empty())
{
printf("%d\n",que.front());
que.pop_front();
}
}
}
return ;
}

AC代码

POJ 2378 Tree Cutting (树的重心,微变形)的更多相关文章

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

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

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

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

  3. POJ 2378 Tree Cutting (DFS)

    题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...

  4. POJ 2378 Tree Cutting 子树统计

    题目大意:给出一棵树.将树中的一个节点去掉之后,这棵树会分裂成一些联通块.求去掉哪些点之后.全部联通块的大小不超过全部节点的一半.并按顺序输出. 思路:基础的子树统计问题,仅仅要深搜一遍就能够出解.这 ...

  5. poj 2378 Tree Cutting 树形dp

    After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...

  6. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  7. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  8. POJ 1741 Tree【树分治】

    第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重 ...

  9. POJ 3723 Tree(树链剖分)

    POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...

随机推荐

  1. 关于数组array的一些误区

    $arr1 = array(1,2,3,4); $arr2 = array(5,6,7,8,9,10); var_dump ( $arr1 + $arr2);    //输出数组形式的(1,2,3,4 ...

  2. 【222】◀▶ IDL 输入输出函数说明

    参考:I/O - General Input/Output Routines —— 基本输入输出函数 01   PRINT/PRINTF 格式化输出. 02   READ/READF 格式化输入. 0 ...

  3. 性能测试之Jmeter学习(二)

    一.Jmeter的基本操作 1.添加|移除测试元件 2.加载和保存测试元件 3.配置测试对中的测试元件 4.保存测试计划 5.运行测试计划 6.终止测试 7.错误报告 二.Jmeter体系结构 注释: ...

  4. 使用SQL访问MongoDB

    使用SQL访问MongoDB 简介 使用SQL访问MongoDB有多种解决方案,就我所知的,除了今天要介绍的MongoDB Connector for BI外,还有Studio 3T,但后者只有在企业 ...

  5. YUV格式学习:YUV420P、YV12、NV12、NV21格式转换成RGB24(转载)

    转自:http://www.latelee.org/my-study/yuv-learning-yuv420p-to-rgb24.html 对于YUV420的格式,网上有一大堆资料,这里就不说了.直奔 ...

  6. Unity 与 Android (Android Studio)的交互

    http://blog.csdn.net/kuerjinjin/article/details/50177633 1.大体思路: 在Android Studio 中编译导出Jar库,提供函数供 Uni ...

  7. bzoj 3504: [Cqoi2014]危桥【最大流】

    妙啊,很容易想到连(s,a1,an)(s,b1,bn)(a2,t,an)(b2,t,bn),这样,但是可能会发生a1流到b2或者b1流到a2这种不合法情况 考虑跑两次,第二次交换b1b2,如果两次都合 ...

  8. 当前View的坐标相对其他View的位置坐标

    // 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect - (CGRect)convertRect:(CGRect)rect toView:(UIView *) ...

  9. Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) C

    This is an interactive problem. You should use flush operation after each printed line. For example, ...

  10. ERP实施顾问,请找准自己的定位

    最近给一些实施顾问做了培训.这些实施顾问都是我们渠道伙伴中具有较高提升潜质的顾问,期待做一次集中培训,他们能够在ERP项目实施上有所突破与提升,并能够为公司的ERP项目实施工作承担更多职责,分担更多压 ...