64842023-12-04 21:29:13szilHosszú Láncokcpp17Accepted 100/100116ms16932 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2104 KiB
2Accepted3ms2064 KiB
3Accepted3ms2300 KiB
4Accepted3ms2488 KiB
subtask210/10
5Accepted3ms2584 KiB
6Accepted3ms2696 KiB
7Accepted3ms2784 KiB
8Accepted3ms2908 KiB
9Accepted3ms3000 KiB
10Accepted3ms3088 KiB
11Accepted3ms2992 KiB
12Accepted3ms3276 KiB
13Accepted3ms3268 KiB
14Accepted3ms3456 KiB
15Accepted3ms3552 KiB
subtask320/20
16Accepted3ms3768 KiB
17Accepted3ms3976 KiB
18Accepted3ms4060 KiB
19Accepted3ms4288 KiB
20Accepted2ms4340 KiB
21Accepted3ms4244 KiB
22Accepted3ms4260 KiB
23Accepted3ms4364 KiB
subtask420/20
24Accepted3ms4260 KiB
25Accepted3ms4364 KiB
26Accepted3ms4544 KiB
27Accepted3ms4524 KiB
28Accepted3ms4476 KiB
29Accepted3ms4492 KiB
30Accepted3ms4792 KiB
31Accepted3ms4948 KiB
subtask550/50
32Accepted7ms5720 KiB
33Accepted12ms7152 KiB
34Accepted26ms10400 KiB
35Accepted54ms15960 KiB
36Accepted8ms6080 KiB
37Accepted18ms7248 KiB
38Accepted45ms11148 KiB
39Accepted116ms16932 KiB