-
Notifications
You must be signed in to change notification settings - Fork 0
/
200810-1.cpp
85 lines (82 loc) · 1.61 KB
/
200810-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// https://leetcode-cn.com/problems/minimum-height-trees/
// 挑战失败:超出时间限制
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
vector<vector<int>> m(n);
for (auto& r : m) {
r.resize(n);
for (auto& e : r) {
e = n;
}
}
for (auto& e : edges) {
m[e[0]][e[1]] = 1;
m[e[1]][e[0]] = 1;
}
for (int i = 0; i < n; ++i) {
m[i][i] = 1;
}
for (;;) {
bool flag = false;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
if (m[i][j] > m[i][k] + m[k][j]) {
m[i][j] = m[i][k] + m[k][j];
flag = true;
}
}
}
}
if (!flag) break;
}
vector<int> depth(n);
int max_depth = n;
for (int i = 0; i < n; ++i) {
depth[i] = m[i][0];
for (int j = 1; j < n; ++j) {
if (depth[i] < m[i][j]) {
depth[i] = m[i][j];
}
}
if (max_depth > depth[i]) {
max_depth = depth[i];
}
}
#if 0
for (auto& r : m) {
for (auto e : r) cout << e << " "; cout << endl;
}
#endif
vector<int> r;
for (int i = 0; i < n; ++i) {
if (depth[i] == max_depth) {
r.push_back(i);
}
}
return r;
}
};
void print(const vector<int>& a)
{
cout << "[ ";
for (auto& e : a) cout << e << " ";
cout << "]" << endl;
}
int main()
{
Solution s;
{
vector<vector<int>> a = {{1, 0}, {1, 2}, {1, 3}};
print(s.findMinHeightTrees(4, a)); // answer: [1]
}
{
vector<vector<int>> a = {{0, 3}, {1, 3}, {2, 3}, {4, 3}, {5, 4}};
print(s.findMinHeightTrees(6, a)); // answer: [3, 4]
}
return 0;
}