提交记录 47719


用户 题目 状态 得分 用时 内存 语言 代码长度
jiegec noip18c. 【NOIP2018】赛道修建 Accepted 100 50.574 ms 7644 KB C++17 3.78 KB
提交时间 评测时间
2026-09-13 01:42:17 2026-09-13 01:42:22
// This code is AI-generated. (AI 生成的代码)
// NOIP2018 赛道修建: binary search the answer X.  Check(X): DFS; child chains
// reaching a node are either closed (length >= X) or paired with each other to
// reach X (smallest with the smallest sufficient partner); the largest unpaired
// chain is passed to the parent.  The search stops once m tracks are formed.
//
// Most tree nodes have very few children, so 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 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; }
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;
    for (int i = 1; i < n; i++) {
        int a = rd(), b = rd(), w = rd();
        ae(a, b, w); ae(b, a, w); total += w;
    }
    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;
    }
    char *op = ob; int k = 0; char t[16];
    while (ans) { t[k++] = (char)('0' + ans % 10); ans /= 10; }
    while (k) *op++ = t[--k];
    *op++ = '\n';
    fwrite(ob, 1, op - ob, stdout);
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #110.63 us40 KBAcceptedScore: 5

Testcase #210.15 us40 KBAcceptedScore: 5

Testcase #310.33 us40 KBAcceptedScore: 5

Testcase #4258.2 us92 KBAcceptedScore: 5

Testcase #550.574 ms1 MB + 456 KBAcceptedScore: 5

Testcase #637.138 ms1 MB + 740 KBAcceptedScore: 5

Testcase #731.153 ms1 MB + 464 KBAcceptedScore: 5

Testcase #846.296 ms2 MB + 392 KBAcceptedScore: 5

Testcase #9201.18 us188 KBAcceptedScore: 5

Testcase #1012.747 ms4 MB + 504 KBAcceptedScore: 5

Testcase #1124.113 ms7 MB + 476 KBAcceptedScore: 5

Testcase #1216.64 us40 KBAcceptedScore: 5

Testcase #1316.31 us40 KBAcceptedScore: 5

Testcase #1442.53 us44 KBAcceptedScore: 5

Testcase #1541.31 us48 KBAcceptedScore: 5

Testcase #16230.56 us100 KBAcceptedScore: 5

Testcase #17274.22 us96 KBAcceptedScore: 5

Testcase #1816.722 ms1 MB + 324 KBAcceptedScore: 5

Testcase #1920.701 ms1 MB + 720 KBAcceptedScore: 5

Testcase #2035.861 ms2 MB + 904 KBAcceptedScore: 5


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