POJ 3099 Go Go Gorelians
http://poj.org/problem?id=3099
树的重心:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心
求树的重心
如何在点中构造符合条件的树
得到树后 从任意一个点出发 dfs一次找到离这个点最远的点作为root1
在以root1出发 同样的方式求得root2
root1-root2就是这个树的直径 那么中心必然在这个直径上, 如果直径上点的个数是奇数的话那么 这个点就是重心 否则有两个重心 分别输出
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#define MAXN 1007 using namespace std; int dp[MAXN];
int id[MAXN];
int pre[MAXN];
double dist[MAXN][MAXN];
double x[MAXN], y[MAXN], z[MAXN];
int path[MAXN];
int n;
vector<int> G[MAXN]; double getdis(int i, int j)
{
double dx = x[i] - x[j];
double dy = y[i] - y[j];
double dz = z[i] - z[j];
return sqrt(dx*dx+dy*dy+dz*dz);
} void build()
{
for(int i = ; i < n; i++)
{
double dis = 0x3fffffff;
int obj = -;
for (int j = ; j < i; j++)
{
if (dist[id[i]][id[j]] < dis)
{
dis = dist[id[i]][id[j]];
obj = j;
}
}
if (obj == -) continue;
int u = id[i], v = id[obj];
G[u].push_back(v);
G[v].push_back(u);
}
}
void dfs(int x, int par, int len)
{
dp[x] = len;
for (int i = ; i < G[x].size(); i++)
{
if (G[x][i] != par)
dfs(G[x][i], x, len+);
} } void dfs1(int x, int par, int len)
{
dp[x] = len;
pre[x] = par;
for (int i = ; i < G[x].size(); i++)
{
if(G[x][i] != par)
dfs1(G[x][i], x, len+);
}
}
int main()
{
while (~scanf("%d", &n))
{
if (n == ) break;
memset(dp, , sizeof(dp));
memset(path, , sizeof(path));
memset(dist, , sizeof(dist));
memset(pre, , sizeof(pre));
for (int i = ; i < MAXN; i++)
G[i].clear();
for (int i = ; i < n; i++)
{
scanf("%d%lf%lf%lf", &id[i], &x[i], &y[i], &z[i]);
}
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
dist[id[i]][id[j]] = getdis(i, j);
build();
dfs(id[], -, );//权值都为1
int tmp = ;
//找到离节点最远的节点 得到root1
int root1, root2;
for (int i = ; i < n; i++)
{
if ( dp[id[i]] > tmp)
{
tmp = dp[id[i]];
root1 = id[i];
}
}
memset(dp, , sizeof(dp));
dfs1(root1, -, );
tmp = ;
for (int i = ; i < n; i++)
{
if (dp[id[i]] > tmp)
{
tmp = dp[id[i]];
root2 = id[i];
}
}//找到root2;
int s = ;
for(int i = root2; i != -; i = pre[i])
{
path[++s] = i;
}
int mid = (s+)/;
if (s % == )
{
printf("%d\n", path[mid]);
}
else
{
int a = path[mid], b = path[mid+];
printf("%d %d\n", min(a, b), max(a, b));
} } return ;
}
关于树的重心更多的内容
转载 :http://www.cnblogs.com/patrickzhou/p/5867208.html
POJ 3099 Go Go Gorelians的更多相关文章
- POJ 3286 How many 0's?(几多0?)
POJ 3286 How many 0's?(几多0?) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A Benedi ...
- POJ 3653 & ZOJ 2935 & HDU 2722 Here We Go(relians) Again(最短路dijstra)
题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- (转)SpringMVC学习(一)——SpringMVC介绍与入门
http://blog.csdn.net/yerenyuan_pku/article/details/72231272 SpringMVC介绍 SpringMVC是什么? SpringMVC和Stru ...
- VM虚拟机下的Linux不能上网
虚拟机linux上网配置 图解教程 首先查看window7主机下的网络配置VMNet1或VMNet8是否开启,其实linux系统的网络连接跟linux系统一致 在虚拟机界面将桥接改为NAT连接 点虚拟 ...
- javascript的offset、client、scroll使用方法
offsetTop 指元素距离上方或上层控件的位置,整型,单位像素. offsetLeft 指元素距离左方或上层控件的位置,整型,单位像素. offsetWidth 指元素控件自身的宽度,整型,单位像 ...
- 一. python基础知识
第一章.变量与判断语句 1.第一个python程序 # -*- coding:utf-8 -*- # Author: Raymond print ("hello world") p ...
- 【贪心】bzoj1572: [Usaco2009 Open]工作安排Job
先是没怎么理解这个贪心……然后贪心又被细节弄挂…… Description Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. ...
- 004 html常用标签
html常用标签 1.无语义标签 <div></div> <span></span> 2.常用语义标签 <hn></hn> 标题 ...
- RN服务
https://facebook.github.io/react-native/docs/headless-js-android.html 当app在 后台运行 时,我们可以使用RN服务来同时地刷新数 ...
- C/SV/VERILOG语句块界定符不一样
C是一对大括号{} SV /VERILOG 是begin...end
- docker 安装 openresty
文章来源: 1.拉取镜像 # docker pull openresty/openresty 2.启动openresty # docker run -it --name openresty -p : ...
- Python对Dict排序
对下面的Dict: aps = {} for key in T.keys(): ap = average_precision(T[key], P[key]) aps[key] = ap 如果用valu ...