-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2134d.cpp
More file actions
94 lines (81 loc) · 2.28 KB
/
Copy path2134d.cpp
File metadata and controls
94 lines (81 loc) · 2.28 KB
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
86
87
88
89
90
91
92
93
94
#pragma optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
void dfs(int x, int depth, vi &dist, vi &parent, const vvi &edges) {
dist[x] = depth;
for (auto dest : edges[x]) {
if (dist[dest] != -1)
continue;
parent[dest] = x;
dfs(dest, depth + 1, dist, parent, edges);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vvi edges(n + 1);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
// THE PLAN:
// dfs to find diameter
// walk along diameter to find a, b, c
// where a, b on the diameter, a -> b -> c, c not on diameter
// dfs once...
vi dist(n + 1, -1);
vi parent(n + 1, -1);
dfs(1, 0, dist, parent, edges);
int a = 1;
for (int i = 1; i <= n; i++) {
if (dist[i] > dist[a])
a = i;
}
// dfs again...
dist.assign(n + 1, -1);
parent.assign(n + 1, -1);
dfs(a, 0, dist, parent, edges);
int b = 1;
for (int i = 1; i <= n; i++) {
if (dist[i] > dist[b])
b = i;
}
// reconstruct diameter
vi path; int cur = b;
while (cur != -1) {
path.push_back(cur);
cur = parent[cur];
}
// walk along diameter and find answer
bool found = false;
for (int i = 1; i < path.size() - 1; i++) {
int cur = path[i], prev = path[i - 1], next = path[i + 1];
for (auto dest : edges[cur]) {
if (dest == prev || dest == next)
continue;
found = true;
cout << prev << " " << cur << " " << dest << endl;
break;
}
if (found) {
break;
}
}
if (!found)
cout << -1 << endl;
}
}