poj 3107 删点最大分支最小
http://poj.org/problem?id=3107
这实际上就是找重心,在之前有做过:http://www.cnblogs.com/qlky/p/5780933.html
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 110000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m; struct node{int y,val,next;}tree[MAXN<<]; int head[MAXN],vis[MAXN],ptr=,dp[MAXN],num[MAXN],ans,tot,a[MAXN]; void init()
{
mem(head,-);
mem(vis,);
mem(dp,);
ans =INF;
tot = ;
ptr=;
}
void add(int x,int y)
{
tree[ptr].y = y;
tree[ptr].next = head[x];
head[x] = ptr++;
} void dfs(int rt)
{
num[rt]=vis[rt]=;
int tmp = ;
//pf("rt%d\n",rt);
for(int i = head[rt];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
dfs(y);
num[rt]+=num[y];
tmp = max(tmp,num[y]);
//pf("y%d %d\n",y,tmp);
}
tmp = max(tmp,n-num[rt]);
//pf("t%d %d %d\n",rt,tmp,ans);
if(tmp<ans)
{
ans = tmp;
tot = ;
a[tot++] = rt;
}
else if(tmp == ans) a[tot++] = rt;
} int main()
{
int i,j,k,t;
while(~sf("%d",&n))
{
init();
for(i=;i<n;i++)
{
int x,y;
sf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs();
sort(a,a+tot);
pf("%d",a[]);
for(i=;i<tot;i++) pf(" %d",a[i]);
blank;
}
}
poj 3107 删点最大分支最小的更多相关文章
- poj 2378 删点最大分支不超过一半
http://poj.org/problem?id=2378 这题和找重心基本一样,判断条件换一下就行 #include <iostream> #include <string> ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- POJ 1655 Balancing Act && POJ 3107 Godfather
题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- git:当本地分支中的代码和develop分支上有很多冲突,希望删掉本地分支,重新建立新的分支,怎么解决?
git:当本地分支中的代码和develop分支上有很多冲突,希望删掉本地分支,重新建立新的分支,但是git中说你需要先解决当前的冲突,这种情况怎么解决 这种情况可以在代码编辑器中点击commit,然后 ...
- poj 3107 Godfather 求树的重心【树形dp】
poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...
- poj 2914(stoer_wanger算法求全局最小割)
题目链接:http://poj.org/problem?id=2914 思路:算法基于这样一个定理:对于任意s, t V ∈ ,全局最小割或者等于原图的s-t 最小割,或者等于将原图进行 Cont ...
随机推荐
- 使用Shader Graph实现《塞尔达传说:旷野之息》风格的着色器
https://mp.weixin.qq.com/s/19Xq6wYuXP0gQJDvC9Fh0g
- ConcurrentHashMap原理详解
参考链接:https://www.cnblogs.com/chengxiao/p/6842045.html https://www.cnblogs.com/ITtangtang/p/3948786.h ...
- UVA11270 Tiling Dominoes
\(\color{#0066ff}{ 题目描述 }\) 给定一个m×n的矩形网格,用1×2多米诺骨牌完全平铺. 请注意,即使一个平铺的旋转与另一个平铺相匹配,它们仍算作不同的平铺. 下面显示了一个平铺 ...
- 洛谷4316 绿豆蛙的归宿(DAG递推/概率dp)
题目大意: 给定一个DAG,求起点到终点的路径长度期望 根据题意可以知道每一条边都有一定概率被走到 那么\(\displaystyle\begin{aligned} Ans = \sum_{e \in ...
- ArcGIS Engine 添加SDE数据库
public void AddSDELayer(bool ChkSdeLinkModle){ //定义一个属性 IPropertySet propset = new PropertySetCla ...
- shell脚本安装jdk
#!/bin/bash BASE_SERVER=192.168.1.11 yum install -y wget wget $BASE_SERVER/soft-all/jdk-7u45-linux-x ...
- sql 日期对比
来自这里
- Hibernate上传数据到数据库,从数据库读取数据到本地模板代码
1.Hibernate上传数据到数据库: //创建一个session对象 Session session1=HibernateTools.getSession(); //Fenciresult数据库表 ...
- P2895 [USACO08FEB]流星雨Meteor Shower
传送门 预处理出每个位置最早被摧毁的时间,在此之前都可以走 直接dfs加个记忆化和最优性剪枝就好了 一定要注意流星的边界,如果波及到负数坐标的位置不要去考虑会RE 一定要考虑流星砸到边界的情况 如 ( ...
- java坏境内存不够用 大量占用swap 临时加swap
dd if=/dev/sda of=/tmp/mbr.bin bs=512 count=1 查询2进制文件 file 看文件类型 思路 创建一个大文件作为swap 1.1创建文件 [root ...