leetcode Minimum Height Trees
2015-12-01 19:45
344 查看
原题链接:https://leetcode.com/problems/minimum-height-trees/
Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
Example 1:
Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
return [1]
Example 2:
Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
return [3, 4]
题目大意:给你一张无向图,可以选择任意节点作为根使其成为有根树。找到一些节点
使得这棵树的高度最小。。
我的思路:先找到树中的最长链,其中点(节点)即为所求。。
找树中的最长链两次bfs即可。。
Description
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
Example 1:
Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
0 | 1 / \ 2 3
return [1]
Example 2:
Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
0 1 2 \ | / 3 | 4 | 5
return [3, 4]
题目大意:给你一张无向图,可以选择任意节点作为根使其成为有根树。找到一些节点
使得这棵树的高度最小。。
我的思路:先找到树中的最长链,其中点(节点)即为所求。。
找树中的最长链两次bfs即可。。
class Solution { private: typedef vector<int> vec; typedef vector<pair<int, int>> vpi; public: vec findMinHeightTrees(int n, vpi& edges) { tot = 0, ret.clear(); if (edges.empty()) { ret.push_back(0); return ret; } init(n, edges); vec ans = solve(n); __free__(); return ans; } private: vec ret; int tot, *head, *dist, *pre; struct edge { int to, next; }*G; inline void init(int n, vpi& edges) { int m = n + 10; pre = new int[m]; head = new int[m]; dist = new int[m]; memset(pre, -1, sizeof(int)* m); memset(head, -1, sizeof(int)* m); m = edges.size(); G = new edge[(m + 10) << 1]; for (int i = 0; i < m; i++) { int u = edges[i].first, v = edges[i].second; add_edge(u, v); } } inline void add_edge(int u, int v) { G[tot].to = v, G[tot].next = head[u], head[u] = tot++; G[tot].to = u, G[tot].next = head[v], head[v] = tot++; } inline int bfs(int s, int n, bool f = false) { int id = s, max_dist = 0; memset(dist, -1, sizeof(int) * (n + 10)); queue<int> q; q.push(s); dist[s] = 0; while (!q.empty()) { int u = q.front(); q.pop(); if (dist[u] > max_dist) { max_dist = dist[id = u]; } for (int i = head[u]; ~i; i = G[i].next) { int &v = G[i].to; if (-1 == dist[v]) { dist[v] = dist[u] + 1; if (f) pre[v] = u; q.push(v); } } } return id; } inline vec solve(int n) { int s = bfs(0, n); int t = bfs(s, n, true); vec ans; for (; ~t; t = pre[t]) ret.push_back(t); n = ret.size(); if (!n) return ans; ans.push_back(ret[n / 2]); if (!(n & 1)) ans.push_back(ret[n / 2 - 1]); if (ans.size() > 1 && ans[0] > ans[1]) swap(ans[0], ans[1]); return ans; } inline void __free__() { delete []G; delete []pre; delete []dist; delete []head; } };
相关文章推荐
- HTML标签以及各个标签属性大全(网页制作必备)
- 修改数据库密码
- Light OJ 1008 Fibsieve`s Fantabulous Birthday【规律】
- 企业应用打包的时候 修改ipa包的bundle identifier
- 禁用驱动签名强制
- 129、Sum Root to Leaf Numbers
- 【Codeforces 343D】Water Tree dfs序建树+线段树
- BZOJ2300: [HAOI2011]防线修建
- hadoop分布式安装在ubuntu下安装JDK
- hadoop分布式安装在ubuntu下安装JDK
- Oracle数据库查询锁表
- centos6之pacemaker 高可用Web使用详解(转)
- 分享我设计的iOS项目目录结构
- Python几个标准类型内建函数
- 条件编译
- 数组和指针
- Oracle数据库导入导出语句
- 备忘——java如何设置全局变量
- android socket简单编程(java在PC本地创建服务器)
- 求矩形和圆柱的体积(用到了接口)