比赛 2024暑假C班集训B 评测结果 AAWAAAWWAAAWWWWWWWWW
题目名称 赛道修建 最终得分 40
用户昵称 darkMoon 运行时间 0.333 s
代码语言 C++ 内存使用 5.29 MiB
提交时间 2024-07-11 11:33:46
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define mp make_pair
using namespace std;
// #define fin cin
// #define fout cou
ifstream fin("2018track.in");
ofstream fout("2018track.out");
auto mread = [](){
    int x;
    fin >> x;
    return x;
};
const int N = 50005;
int n = mread(), m = mread(), d[N];
vector<pair<int, int> > v[N];
void dfs(int x, int fa){
    for(auto t : v[x]){
        if(t.fi == fa){
            continue;
        }
        d[t.fi] = d[x] + t.se;
        dfs(t.fi, x);
    }
    return;
}
void solve1(){
    d[1] = 0;
    dfs(1, 0);
    int ma = 0, p = 0;
    for(int i = 1; i <= n; i ++){
        if(d[i] > ma){
            ma = d[i];
            p = i;
        }
    }
    d[p] = 0;
    dfs(p, 0);
    ma = 0;
    for(int i = 1; i <= n; i ++){
        ma = max(ma, d[i]);
    }
    fout << ma;
    return;
}
void solve3(){
    auto check = [&](int mid){
        int sum = 0, now = 0;
        for(int i = 1; i < n; i ++){
            if(v[i][0].fi > i){
                sum += v[i][0].se;
            }
            else{
                sum += v[i][1].se;
            }
            if(sum >= mid){
                now ++;
                sum = 0;
            }
        }
        // printf("*** %lld %lld\n", mid, now);
        return now >= m;
    };
    int l = 1, r = 500000000;
    while(l < r){
        int mid = (l + r + 1) >> 1;
        if(check(mid)){
            l = mid;
        }
        else{
            r = mid - 1;
        }
    }
    fout << l << "\n";
    return;
}
signed main(){
    int e2 = 1, e3 = 1;
    for(int i = 1, x, y, z; i < n; i ++){
        x = mread(), y = mread(), z = mread();
        v[x].push_back(mp(y, z));
        v[y].push_back(mp(x, z));
        if(x != 1){
            e2 = 0;
        }
        if(y != x + 1){
            e3 = 0;
        }
    }
    if(m == 1){
        solve1();
    }
    else if(e3){
        solve3();
    }
    // else if(e2){
    //     solve2();
    // }
    return 0;
}