提交记录 47720


用户 题目 状态 得分 用时 内存 语言 代码长度
jiegec noip18c. 【NOIP2018】赛道修建 Accepted 100 45.74 ms 7644 KB C++17 4.46 KB
提交时间 评测时间
2026-09-13 01:42:54 2026-09-13 01:43:00
// This code is AI-generated. (AI 生成的代码)
// NOIP2018 赛道修建.
//   m == 1        -> tree diameter (one O(n) pass).
//   m == n - 1    -> shortest edge (one track per edge).
//   general       -> binary search answer X; Check(X): DFS, child chains are
//                    closed if >= X or paired with the smallest sufficient
//                    partner; the largest unpaired chain goes to the parent.
//                    Small child sets use an inlined insertion sort + bitmask
//                    pairing instead of std::sort + lower_bound + DSU.
#pragma GCC optimize("O3")
#include <cstdio>
#include <algorithm>
using namespace std;
enum { MAXN = 50005, MAXE = 100005 };
static int head[MAXN], nx_[MAXE], to_[MAXE], wt_[MAXE], ec;
static int X, formed, target;
static int pool[MAXN], sp;
static int nxtp[MAXN];
static char usedp[MAXN];

static inline int findn(int p, int end) {
    int r = p;
    while (r < end && usedp[r]) r = nxtp[r];
    while (p < r) { int t = nxtp[p]; nxtp[p] = r; p = t; }
    return r;
}
static void ae(int u, int v, int w) {
    to_[++ec] = v; wt_[ec] = w; nx_[ec] = head[u]; head[u] = ec;
}
static long long diam;
static long long diameter(int u, int fa, int ignore) {
    (void)ignore;
    long long b1 = 0, b2 = 0;
    for (int e = head[u]; e; e = nx_[e]) {
        int v = to_[e];
        if (v == fa) continue;
        long long d = diameter(v, u, 0) + wt_[e];
        if (d > b1) { b2 = b1; b1 = d; } else if (d > b2) b2 = d;
    }
    if (b1 + b2 > diam) diam = b1 + b2;
    return b1;
}
static int dfs(int u, int fa) {
    int base = sp, k = 0;
    for (int e = head[u]; e; e = nx_[e]) {
        int v = to_[e];
        if (v == fa) continue;
        int d = dfs(v, u) + wt_[e];
        if (formed >= target) return 0;
        if (d >= X) formed++;
        else { pool[base + k] = d; k++; sp = base + k; }
    }
    if (k == 0) return 0;
    if (k == 1) { int r = pool[base]; sp = base; return r; }
    if (k <= 24) {
        int *a = pool + base;
        for (int i = 1; i < k; i++) {
            int v = a[i], j = i - 1;
            while (j >= 0 && a[j] > v) { a[j + 1] = a[j]; j--; }
            a[j + 1] = v;
        }
        unsigned used = 0;
        int ret = 0;
        for (int i = 0; i < k; i++) {
            if (used >> i & 1) continue;
            int t = X - a[i], j = i + 1;
            while (j < k && ((used >> j & 1) || a[j] < t)) j++;
            if (j < k) {
                used |= (1u << i) | (1u << j);
                formed++;
                if (formed >= target) { sp = base; return 0; }
            } else {
                ret = a[i];
            }
        }
        sp = base;
        return ret;
    }
    int end = base + k;
    sort(pool + base, pool + end);
    for (int i = base; i <= end; i++) { nxtp[i] = i; usedp[i] = 0; }
    int ret = 0;
    for (int i = base; i < end; i++) {
        if (usedp[i]) continue;
        int pos = (int)(lower_bound(pool + i + 1, pool + end, X - pool[i]) - pool);
        pos = findn(pos, end);
        if (pos < end) {
            nxtp[i] = findn(i + 1, end);
            nxtp[pos] = findn(pos + 1, end);
            usedp[i] = usedp[pos] = 1;
            formed++;
            if (formed >= target) { sp = base; return 0; }
        } else {
            ret = pool[i];
        }
    }
    sp = base;
    return ret;
}
static char ib[1 << 22], ob[32];
static char *gp;
static inline int rd() { while (*gp < '0') gp++; int v = 0; while (*gp >= '0' && *gp <= '9') v = v * 10 + (*gp++ - '0'); return v; }
static void out(int ans) {
    char *op = ob; int k = 0; char t[16];
    if (ans == 0) t[k++] = '0';
    while (ans) { t[k++] = (char)('0' + ans % 10); ans /= 10; }
    while (k) *op++ = t[--k];
    *op++ = '\n';
    fwrite(ob, 1, op - ob, stdout);
}
int main() {
    int len = (int)fread(ib, 1, sizeof(ib) - 1, stdin);
    ib[len] = 0; gp = ib;
    int n = rd(), m = rd();
    long long total = 0;
    int minw = 0x7fffffff;
    for (int i = 1; i < n; i++) {
        int a = rd(), b = rd(), w = rd();
        ae(a, b, w); ae(b, a, w); total += w;
        if (w < minw) minw = w;
    }
    if (m == 1) { diam = 0; diameter(1, 0, 0); out((int)diam); return 0; }
    if (m == n - 1) { out(minw); return 0; }
    target = m;
    int lo = 0, hi = (int)(total / m), ans = 0;
    while (lo <= hi) {
        int mid = (lo + hi) >> 1;
        X = mid; formed = 0; sp = 0;
        if (mid == 0) { ans = 0; lo = 1; continue; }
        dfs(1, 0);
        if (formed >= m) { ans = mid; lo = mid + 1; }
        else hi = mid - 1;
    }
    out(ans);
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #19.41 us40 KBAcceptedScore: 5

Testcase #210.73 us40 KBAcceptedScore: 5

Testcase #310.51 us40 KBAcceptedScore: 5

Testcase #442.81 us88 KBAcceptedScore: 5

Testcase #5802.54 us1 MB + 188 KBAcceptedScore: 5

Testcase #61.349 ms1 MB + 528 KBAcceptedScore: 5

Testcase #731.033 ms1 MB + 464 KBAcceptedScore: 5

Testcase #845.74 ms2 MB + 392 KBAcceptedScore: 5

Testcase #9222.3 us188 KBAcceptedScore: 5

Testcase #1012.826 ms4 MB + 504 KBAcceptedScore: 5

Testcase #1124.18 ms7 MB + 476 KBAcceptedScore: 5

Testcase #1216.62 us40 KBAcceptedScore: 5

Testcase #1315.91 us40 KBAcceptedScore: 5

Testcase #1441.77 us44 KBAcceptedScore: 5

Testcase #1541.36 us48 KBAcceptedScore: 5

Testcase #16213.91 us100 KBAcceptedScore: 5

Testcase #17265.4 us96 KBAcceptedScore: 5

Testcase #1816.684 ms1 MB + 324 KBAcceptedScore: 5

Testcase #1920.553 ms1 MB + 720 KBAcceptedScore: 5

Testcase #2035.467 ms2 MB + 904 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2026-09-24 17:20:04 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠