Balancing Act(poj1655)】的更多相关文章

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12703   Accepted: 5403 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m…
树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可以通过树形DP来求解树的重心. 设\(Max_i\)代表删去i节点后树中剩下子树中节点最多的一个子树的节点数.由于删去节点i至少将原树分为两部分,所以满足\(\ \frac{1}{2}n \leq Max_i\),我们要求的就是一个\(i\),使得\(Max_i\)最小. 对于Max数组,我们可以列…
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9072   Accepted: 3765 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a f…
题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i) #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int INF = 1 << 30; const int N = 100000 + 10; int H[N…
题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4993 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of on…
Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m…
Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the…
Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14251   Accepted: 6027 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m…
Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m…
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.1655 Balancing Act 题目大意: 有t组数据.每组数据给出n个点和n-1条边,构成一棵树,求该树的重心及删掉该点后形成的每棵子树的节点数. 代码: #include<cctype> #include<cstdio> #include<cstring> #inclu…
Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或者dfs,这里我用的dfs 基本方法就是随便设定一个根节点,然后找出这个节点的子树大小(包括这个节点),然后总点数减去子树的大小就是向父亲节点走去的点数,使这几部分的最大值最小 */ #include<iostream> #include<cstdio> #include<alg…
传送门 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14070   Accepted: 5939 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one…
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好明白了,不仅要考虑当前结点子树的大小,也要“向上”考虑树的大小. 那么其它就dfs完成就行了,son[] 存以前结点为根的结点个数. 这是用邻接表写: #include<iostream> #include<cstdio> #include<cstring> #includ…
Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10311   Accepted: 4261 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m…
http://poj.org/problem?id=1655 (题目链接) 题意 给出一棵树,求树上一点使得使得删除一点后该树的最大子树最小. solution 树的重心裸题. 随意取一点作为根节点,dfs维护当前节点的最大子树大小以及它的父亲子树大小,更新答案. 代码 // poj1655 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include&…
题目大意:一棵n个节点的树,找出最大子树最小的节点. 题目分析:过程类似求重心. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector> # include<queue> # include<list> # include<set> # include<map> # include<string>…
思路:DP求树的重心.对于每个结点$i$,$d_i=\displaystyle{\sum_{j\in s(i)}}d_j+1$.删去这个点能得到的最大子树大小即为$\max(\max\limits_{j\in s(i)}\{d(j)\},n-d(i))$.然后取最小的结点即可. #include<cstdio> #include<cctype> #include<vector> #include<cstring> inline int getint() {…
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created…
题意与分析 树的重心板子题. 值得考虑的是,重心究竟有哪些优秀的性质? 这里是一些网上能看到的性质: (判定性质)找到一个点,其所有的子树中最大的子树节点数最少(子树可以"倒着看"),那么这个点就是这棵树的重心. 以这个点为根,那么所有的子树(不算整个树自身)的大小都不超过整个树大小的一半. 树中所有点到某个点的距离和中,到重心的距离和是最小的:如果有两个重心,那么他们的距离和一样. 把两个树通过一条边相连得到一个新的树,那么新的树的重心在连接原来两个树的重心的路径上. 把一个树添加或…
树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN 222222 struct Edge{ int u,v,next; }edge[MAXN<<]; int NE,head[MAXN]; void addEdge(int u,int…
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting…
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 const int INF=0x7f7f7f7f; 6 const int N=20005; 7 int head[N],to[N*2],nxt[N*2],f[N],size[N]; 8 int tot,n,T,…
求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<stack> using namespace std; #define LL long long #define clc(a,b) memset(a,b,sizeof(a)) #d…
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的重心,如果有多个,找出编号最小的那个,并输出他的子树当中最大的节点数. 思路:利用dfs求出每个点的所有孩子数目,然后在dfs一下求出树的重心. 用途:树的重心在树分治的点分治中有重要作用.具体可以看上篇树分治的题目http://www.cnblogs.com/Howe-Young/p/477685…
这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include<iostream> #include<stdio.h> #include<string.h> #include<set> #include<ctime> #include<algorithm> #include<queue> #…
标题效果:鉴于一棵树.除去一个点之后,这棵树将成为一些中国联通的块.之后该点通过寻求取消最低形成块的最大数目. 思维:树DP思维.通过为每个子树尺寸的根节点深搜索确定.之后该节点然后除去,,还有剩下的部分.求一下这些块中数目的最大值.就是去掉这个点时的ans.然后更新总的ans. 这个题事实上就是树的重心. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algori…
<题目链接> 题目大意:给你一棵树,任意去除某一个点后,树被分成了几个联通块,则该点的平衡值为所有分成的连通块中,点数最大的那个,问你:该树所有点中,平衡值最小的那个点是什么? 解题分析: 运用DFS,找到以u为根节点,所有子节点数的最大值,然后求出这些最大值的最小值. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using names…
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the ba…
题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS C++ 1127B #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; in…
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 20005 using namespace std; struct Edge { int to; int next; }e[*maxn]; int t,n,x,y,tot; int head[maxn],num[maxn],balance[maxn]; void Init() { tot=; memset(head,-,sizeof(h…