POJ 3099 Go Go Gorelians

http://poj.org/problem?id=3099

树的重心:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心

求树的重心

如何在点中构造符合条件的树

得到树后 从任意一个点出发 dfs一次找到离这个点最远的点作为root1

在以root1出发 同样的方式求得root2

root1-root2就是这个树的直径 那么中心必然在这个直径上, 如果直径上点的个数是奇数的话那么 这个点就是重心 否则有两个重心 分别输出

  1 #include <iostream>
  2 #include <string.h>
  3 #include <stdio.h>
  4 #include <vector>
  5 #include <math.h>
  6 #define MAXN 1007
  7
  8 using namespace std;
  9
 10 int dp[MAXN];
 11 int id[MAXN];
 12 int pre[MAXN];
 13 double dist[MAXN][MAXN];
 14 double x[MAXN], y[MAXN], z[MAXN];
 15 int path[MAXN];
 16 int n;
 17 vector<int> G[MAXN];
 18
 19 double getdis(int i, int j)
 20 {
 21     double dx = x[i] - x[j];
 22     double dy = y[i] - y[j];
 23     double dz = z[i] - z[j];
 24     return sqrt(dx*dx+dy*dy+dz*dz);
 25 }
 26
 27 void build()
 28 {
 29     for(int i = 0; i < n; i++)
 30     {
 31         double dis = 0x3fffffff;
 32         int obj = -1;
 33         for (int j = 0; j < i; j++)
 34         {
 35             if (dist[id[i]][id[j]] < dis)
 36             {
 37                 dis = dist[id[i]][id[j]];
 38                 obj = j;
 39             }
 40         }
 41         if (obj == -1) continue;
 42         int u = id[i], v = id[obj];
 43         G[u].push_back(v);
 44         G[v].push_back(u);
 45     }
 46 }
 47 void dfs(int x, int par, int len)
 48 {
 49     dp[x] = len;
 50     for (int i = 0; i < G[x].size(); i++)
 51     {
 52         if (G[x][i] != par)
 53             dfs(G[x][i], x, len+1);
 54     }
 55
 56 }
 57
 58
 59 void dfs1(int x, int par, int len)
 60 {
 61     dp[x] = len;
 62     pre[x] = par;
 63     for (int i = 0; i < G[x].size(); i++)
 64     {
 65         if(G[x][i] != par)
 66             dfs1(G[x][i], x, len+1);
 67     }
 68 }
 69 int main()
 70 {
 71     while (~scanf("%d", &n))
 72     {
 73         if (n == 0) break;
 74         memset(dp, 0, sizeof(dp));
 75         memset(path, 0, sizeof(path));
 76         memset(dist, 0, sizeof(dist));
 77         memset(pre, 0, sizeof(pre));
 78         for (int i = 0; i < MAXN; i++)
 79             G[i].clear();
 80         for (int i = 0; i < n; i++)
 81         {
 82             scanf("%d%lf%lf%lf", &id[i], &x[i], &y[i], &z[i]);
 83         }
 84         for (int i = 0; i < n; i++)
 85             for (int j = 0; j < n; j++)
 86                 dist[id[i]][id[j]] = getdis(i, j);
 87         build();
 88         dfs(id[0], -1, 0);//权值都为1
 89         int tmp = 0;
 90         //找到离节点最远的节点 得到root1
 91         int root1, root2;
 92         for (int i = 0; i < n; i++)
 93         {
 94             if ( dp[id[i]] > tmp)
 95             {
 96                 tmp = dp[id[i]];
 97                 root1 = id[i];
 98             }
 99         }
100         memset(dp, 0, sizeof(dp));
101         dfs1(root1, -1, 0);
102         tmp = 0;
103         for (int i = 0; i < n; i++)
104         {
105             if (dp[id[i]] > tmp)
106             {
107                 tmp = dp[id[i]];
108                 root2 = id[i];
109             }
110         }//找到root2;
111         int s = 0;
112         for(int i = root2; i != -1; i = pre[i])
113         {
114             path[++s] = i;
115         }
116         int mid = (s+1)/2;
117         if (s % 2 == 1)
118         {
119             printf("%d\n", path[mid]);
120         }
121         else
122         {
123             int a = path[mid], b = path[mid+1];
124             printf("%d %d\n", min(a, b), max(a, b));
125         }
126
127     }
128
129     return 0;
130 }

关于树的重心更多的内容

转载 :http://www.cnblogs.com/patrickzhou/p/5867208.html

时间: 2024-12-24 18:38:00

POJ 3099 Go Go Gorelians的相关文章

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

题目链接: PKU:http://poj.org/problem?id=3653 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1934 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2722 Description The Gorelians are a warlike race that travel the universe conquering new world

POJ 3286 How many 0&#39;s?(多少0?)

POJ 3286 How many 0's?(多少0?) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he write down? 一个

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

题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1934 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=2722 Description The Gorelians are a warlike rac

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

POJ 1385 计算几何 多边形重心

链接: http://poj.org/problem?id=1385 题意: 给你一个多边形,求它的重心 题解: 模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00 所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6