88462024-02-01 18:34:26szilHosszú Láncokcpp17Time limit exceeded 30/100600ms16500 KiB
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")

using namespace std;
using ll = long long;

#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define all(a) (a).begin(), (a).end()
#define sortedpair(a, b) {min((a), (b)), max((a), (b))}

const ll MOD = 1e9+7;

int n;
vector<vector<int>> v;

int dfs(int x, int m, int p = -1){
    multiset<int> c;
    int nagyobb = 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.insert(t);
    }
    if(c.empty()){
        if(nagyobb)return m+1;
        else return 1;
    }
    auto it = c.begin();
    bool nemsikerult = false;
    while(it != c.end() && c.size() >= 2){
        auto it2 = c.lower_bound(m-*it);
        if(it == it2)it2++;
        if(it2 == c.end()){
            if(nagyobb){
                it = c.erase(it);
                nagyobb--;
            } else {
                if(nemsikerult)
                    return -1;
                nemsikerult = true;
                it++;
                continue;
            }
        }
        it = c.erase(it);
        if(it == it2){
            it = c.erase(it);
        } else {
            c.erase(it2);
        }
    }

    if(c.size() == 0){
        if(nagyobb)return m+1;
        return 1;
    }
    if(nagyobb >= 2)return m+1;
    return *c.begin()+1;


    // multiset<int> c; // konstans ??
    // for(int i : v[x]){
    //     if(i == p)continue;
    //     int t = dfs(i, m, x);
    //     if(t == -1)return -1;
    //     c.insert(t);
    // }
    // if(c.empty()) return 1;
    // auto it = c.begin();
    // while(it != c.end() && *it < (m+1)/2){
    //     auto it2 = c.lower_bound(m-*it);
    //     if(it2 == c.end()){
    //         it++;
    //         continue;
    //     }
    //     it = c.erase(it);
    //     if(it == it2){
    //         c.erase(it);
    //         break;
    //     }
    //     c.erase(it2);
    // }
    // if(c.empty()){
    //     return 1;
    // }
    // if(*c.begin() >= (m+1)/2){
    //     if(c.size() % 2 == 1) return 1 + *c.rbegin();
    //     else return 1;
    // }
    // if(c.size() == 1){
    //     return *c.begin()+1;
    // }
    // if(*next(c.begin()) < (m+1)/2){
    //     return -1;
    // }
    // if(c.size()%2 == 0){
    //     return -1;
    // }
    // return 1 + *c.begin();
    // while(*c.begin() < m && c.size() >= 2){
    //     c.erase(c.begin()); c.erase(c.begin());
    // }
    // if(c.empty()) return 1;
    // return *c.rbegin() + 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;
        }
    }

    if(v[v[start][0]].size() == n-1){
        cout<<(n%2 == 1 ? 2 : 1)<<endl;
        return;
    }

    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
1Accepted3ms1828 KiB
2Accepted3ms2056 KiB
3Accepted3ms2288 KiB
4Accepted2ms2328 KiB
subtask210/10
5Accepted3ms2564 KiB
6Accepted2ms2540 KiB
7Accepted3ms2672 KiB
8Accepted3ms3032 KiB
9Accepted3ms3148 KiB
10Accepted3ms3348 KiB
11Accepted3ms3560 KiB
12Accepted3ms3924 KiB
13Accepted3ms3956 KiB
14Accepted3ms3944 KiB
15Accepted3ms4140 KiB
subtask30/20
16Accepted3ms4184 KiB
17Accepted3ms4156 KiB
18Time limit exceeded600ms4344 KiB
19Accepted3ms4460 KiB
20Accepted3ms4692 KiB
21Accepted3ms4924 KiB
22Accepted3ms4992 KiB
23Accepted3ms4984 KiB
subtask420/20
24Accepted3ms4976 KiB
25Accepted3ms4892 KiB
26Accepted3ms5028 KiB
27Accepted3ms5028 KiB
28Accepted3ms5016 KiB
29Accepted3ms5160 KiB
30Accepted3ms5192 KiB
31Accepted3ms5244 KiB
subtask50/50
32Accepted7ms5984 KiB
33Accepted12ms7092 KiB
34Time limit exceeded541ms7180 KiB
35Accepted52ms15976 KiB
36Accepted8ms6240 KiB
37Accepted18ms7408 KiB
38Accepted45ms11108 KiB
39Accepted114ms16500 KiB