poj 3107 Godfather 求树的重心【树形dp】

poj 3107 Godfather

poj 1655差不多,那道会了这个也就差不多了。

题意:从小到大输出树的重心。

题会卡stl,要用邻接表存树。。。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn = 50006;
 7 const int INF = 1 << 30;
 8 int head[maxn];
 9 int son[maxn], ans[maxn];
10 bool vis[maxn];
11 int n, siz, cnt, tot;
12 struct Edge
13 {
14     int to, next;
15 };
16 Edge edge[maxn * 2];
17
18 void Init()
19 {
20     memset(vis, 0, sizeof(vis));
21     memset(head, -1, sizeof(head));
22     siz = INF;
23     cnt = tot = 0;
24 }
25
26
27 void add(int u, int v)
28 {
29     edge[cnt].to = v;
30     edge[cnt].next = head[u];
31     head[u] = cnt++;
32 }
33
34 void dfs(int u)
35 {
36     vis[u] = 1;
37     son[u] = 1;
38     int tmp = 0;
39     for (int i = head[u]; i != -1; i = edge[i].next)
40     {
41         int v = edge[i].to;
42         if (!vis[v]) {
43             dfs(v);
44             son[u] += son[v];
45             tmp = max(tmp, son[v]);
46         }
47     }
48     tmp = max(tmp, n - son[u]);
49     if (tmp == siz) {
50         ans[tot++] = u;
51     }
52     else if (tmp<siz) {
53         tot = 0;
54         ans[tot++] = u;
55         siz = tmp;
56     }
57 }
58
59 int main()
60 {
61     while (scanf("%d", &n) == 1)
62     {
63         Init();
64         for (int i = 1; i < n; i++) {
65             int u, v;
66             scanf("%d%d", &u, &v);
67             add(u, v);
68             add(v, u);
69         }
70         dfs(1);
71         sort(ans, ans + tot);
72         for (int i = 0; i<tot; i++) {
73             printf("%d ", ans[i]);
74         }
75         printf("\n");
76     }
77     return 0;
78 }
时间: 2024-11-16 12:34:24

poj 3107 Godfather 求树的重心【树形dp】的相关文章

POJ 1655 Balancing Act(求树的重心--树形DP)

题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. 思路:随便选一个点把无根图转化成有根图,dfs一遍即可dp出答案 //1348K 125MS C++ 1127B #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; int

POJ 1655 Balancing Act[树的重心/树形dp]

Balancing Act 时限:1000ms 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

POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 通常利用树形DP找重心: BalanceAct: http://poj.org/problem?id=1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最

poj3107 Godfather 求树的重心

Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders. Unfortunately, the structure of Chicago mafia is rather complicated

POJ 题目3107 Godfather(树的重心)

Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4999   Accepted: 1729 Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to

poj1655 树的重心 树形dp

处理处每个节点的孩子有几个,和树的大小就好了. #include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> #define INF 99999999 using namespace std; const int MAXN = 20010; struct node { int to; int v; int next; }edge[

poj 1655 Balancing Act 求树的重心【树形dp】

poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好明白了,不仅要考虑当前结点子树的大小,也要"向上"考虑树的大小. 那么其它就dfs完成就行了,son[] 存以前结点为根的结点个数. 这是用邻接表写: 1 #include<iostream> 2 #include<cstdio> 3 #include<cst

树形dp求树的重心

Balancing Act http://poj.org/problem?id=1655 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7 const int M=50010; 8 vector<int> g[

POJ 1655 Balancing Act (求树的重心)

求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<queue> 5 #include<stack> 6 using namespace std; 7 #define LL long long 8 #define clc(a,b) memset(a