leetcode Minimum Height Trees

来源:互联网 发布:淘宝网店怎么过户 编辑:程序博客网 时间:2024/04/30 01:35

原题链接:https://leetcode.com/problems/minimum-height-trees/

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;    }};
0 0
原创粉丝点击