-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf_1000c.cpp
More file actions
52 lines (42 loc) · 1.27 KB
/
cf_1000c.cpp
File metadata and controls
52 lines (42 loc) · 1.27 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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<set<int>> map(n+1);
for (int i = 0; i < n-1; ++i) {
int u, v;
cin >> u >> v;
map[u].insert(v);
map[v].insert(u);
}
vector<int> scores(n+1);
for (int i = 1; i <= n; ++i) {
scores[i] = map[i].size() - 1;
}
int max1 = 1;
for (int i = 2; i <= n; ++i) {
if (scores[i] > scores[max1]) max1 = i;
}
int max2 = (max1 == 1 ? 2 : 1);
for (int i = 1; i <= n; ++i) {
if (i != max1 && scores[i] > scores[max2]) max2 = i;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (i == max1) continue;
bool adj = map[max1].find(i) != map[max1].end();
ans = max(ans, 1 + scores[max1] + scores[i] - (adj ? 1 : 0));
}
for (int i = 1; i <= n; ++i) {
if (i == max2) continue;
bool adj = map[max2].find(i) != map[max2].end();
ans = max(ans, 1 + scores[max2] + scores[i] - (adj ? 1 : 0));
}
cout << ans << '\n';
}
return 0;
}