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的更多相关文章

  1. POJ 3286 How many 0's?(几多0?)

    POJ 3286 How many 0's?(几多0?) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A Benedi ...

  2. POJ 3653 &amp; ZOJ 2935 &amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

    题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http ...

  3. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  4. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  5. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  6. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  7. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. (六)mybatis之构建SqlSessionFactory

    构建SqlSessionFactory 每个mybatis应用都是以SqlSessionFactory的实例为中心的.SqlSessionFactory的实例可以通过SqlSessionFactory ...

  2. (四)VMware Harbor 配置文件

    VMware Harbor 配置文件 :harbor.yml # Configuration file of Harbor # The IP address or hostname to access ...

  3. Hermite 矩阵的特征值不等式

    将要学习 关于 Hermite 矩阵的特征值不等式. Weyl 定理 以及推论.   Weyl 定理 Hermann Weyl 的如下定理是大量不等式的基础,这些不等式要么涉及两个 Hermite 矩 ...

  4. 第1节 flume:13、14、更多flume案例一,通过拦截器实现不同类型的数据区分

    1.6.flume案例一 1. 案例场景 A.B两台日志服务机器实时生产日志主要类型为access.log.nginx.log.web.log 现在要求: 把A.B 机器中的access.log.ng ...

  5. vscode 插件整理

    己亥年  庚午月 癸巳日  宜入宅 忌婚嫁 1.Chinese (Simplified) Language Pack for Visual Studio Code 此中文(简体)语言包为 VS Cod ...

  6. 以太坊开发框架Truffle学习笔记

    from http://truffleframework.com/docs/getting_started/project 1. 安装node.js 8.11.2 LTS 2. 安装Truffle $ ...

  7. Bootstrap历练实例:带有下拉菜单的标签和胶囊导航

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. bootstrap下拉菜单(Dropdowns)

    本章将重点讲解bootstrap下拉菜单(Dropdowns),下拉菜单是可切换的,是以列表格式显示链接的上下文菜单. <!DOCTYPE html><html><hea ...

  9. 【数论】贝壳找房计数比赛&&祭facinv

    震惊!阶乘逆元处理背后竟有如此玄机…… 题目描述 贝壳找房举办了一场计数比赛,比赛题目如下. 给一个字符串 s 和字符串 t,求出 s 的所有去重全排列中 t 出现的次数.比如aab的去重全排列为aa ...

  10. 微信小程序登录对接Django后端实现JWT方式验证登录

    先上效果图 点击授权按钮后可以显示部分资料和头像,点击修改资料可以修改部分资料. 流程 1.使用微信小程序登录和获取用户信息Api接口 2.把Api获取的用户资料和code发送给django后端 3. ...