6484 | 2023-12-04 21:29:13 | szil | Hosszú Láncok | cpp17 | Accepted 100/100 | 116ms | 16932 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<vector<int>> v;
int dfs(int x, int m, int p = -1){
map<int, int> c;
int nagyobb = 0, total = 0;
for(int i : v[x]){
if(i == p)continue;
int t = dfs(i, m, x);
if(t == -1)return -1;
if(t >= m)nagyobb++;
else {
c[t]++; total++;
}
}
if(c.empty()){
if(nagyobb)return m+1;
else return 1;
}
auto it = c.begin();
int nemsik = -1;
while(it != c.end() && total >= 2){
auto it2 = c.lower_bound(m-it->first);
if(it == it2 && it->second == 1) it2++;
if(it2 == c.end()){
if(nagyobb){
it->second--; total--;
if (it->second == 0)
it = c.erase(it);
nagyobb--;
continue;
} else {
if(nemsik != -1)
return -1;
if(it->second > 1){
return -1;
}
nemsik = it->first;
it++;
continue;
}
}
it->second--; total--;
if (it->second == 0)
it = c.erase(it);
if(it == it2 && it->second == 1) {
it = c.erase(it); total--;
} else {
it2->second--; total--;
if (it2->second == 0) {
it2 = c.erase(it2);
}
}
}
if(total == 0){
if(nagyobb)return m+1;
return 1;
}
if(nagyobb >= 2)return m+1;
return c.begin()->first+1;
}
void solve(){
cin>>n;
v.assign(n, {});
for(int i = 0; i < n-1; i++){
int a, b; cin>>a>>b; a--; b--;
v[a].push_back(b);
v[b].push_back(a);
}
int start = 0;
for(int i = 0; i < n; i++){
if(v[i].size() == 1) {
start = i;
break;
}
}
int l = 1, r = n-1;
while(l < r){
int m = (l+r+1)/2;
int res = dfs(start, m);
if(res > m || res == 1){
l = m;
} else {
r = m-1;
}
}
cout<<l<<endl;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int _t = 1;
// cin >> _t;
while (_t--) {
solve();
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2104 KiB | ||||
2 | Accepted | 3ms | 2064 KiB | ||||
3 | Accepted | 3ms | 2300 KiB | ||||
4 | Accepted | 3ms | 2488 KiB | ||||
subtask2 | 10/10 | ||||||
5 | Accepted | 3ms | 2584 KiB | ||||
6 | Accepted | 3ms | 2696 KiB | ||||
7 | Accepted | 3ms | 2784 KiB | ||||
8 | Accepted | 3ms | 2908 KiB | ||||
9 | Accepted | 3ms | 3000 KiB | ||||
10 | Accepted | 3ms | 3088 KiB | ||||
11 | Accepted | 3ms | 2992 KiB | ||||
12 | Accepted | 3ms | 3276 KiB | ||||
13 | Accepted | 3ms | 3268 KiB | ||||
14 | Accepted | 3ms | 3456 KiB | ||||
15 | Accepted | 3ms | 3552 KiB | ||||
subtask3 | 20/20 | ||||||
16 | Accepted | 3ms | 3768 KiB | ||||
17 | Accepted | 3ms | 3976 KiB | ||||
18 | Accepted | 3ms | 4060 KiB | ||||
19 | Accepted | 3ms | 4288 KiB | ||||
20 | Accepted | 2ms | 4340 KiB | ||||
21 | Accepted | 3ms | 4244 KiB | ||||
22 | Accepted | 3ms | 4260 KiB | ||||
23 | Accepted | 3ms | 4364 KiB | ||||
subtask4 | 20/20 | ||||||
24 | Accepted | 3ms | 4260 KiB | ||||
25 | Accepted | 3ms | 4364 KiB | ||||
26 | Accepted | 3ms | 4544 KiB | ||||
27 | Accepted | 3ms | 4524 KiB | ||||
28 | Accepted | 3ms | 4476 KiB | ||||
29 | Accepted | 3ms | 4492 KiB | ||||
30 | Accepted | 3ms | 4792 KiB | ||||
31 | Accepted | 3ms | 4948 KiB | ||||
subtask5 | 50/50 | ||||||
32 | Accepted | 7ms | 5720 KiB | ||||
33 | Accepted | 12ms | 7152 KiB | ||||
34 | Accepted | 26ms | 10400 KiB | ||||
35 | Accepted | 54ms | 15960 KiB | ||||
36 | Accepted | 8ms | 6080 KiB | ||||
37 | Accepted | 18ms | 7248 KiB | ||||
38 | Accepted | 45ms | 11148 KiB | ||||
39 | Accepted | 116ms | 16932 KiB |