TLE on test 13,本地测试 RE ?
查看原帖
TLE on test 13,本地测试 RE ?
766820
WS_ZZM楼主2024/10/19 12:51
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

typedef const int cint;

#define N 300005

template<typename type>
type read(type &x) {
    int f = 1;
    char c = getchar();
    for (; c != '-' && (c < 48 || c > 57); )
        c = getchar();
    c == '-' ? (f = -1, x = 0) : x = c - 48;
    for (c = getchar(); c > 47 && c < 58; )
        x = x * 10 + c - 48, c = getchar();
    return x *= f;
}

int n, m;

struct Edge {
    int to, l;

    inline Edge(cint _to, cint _l) {
        to = _to, l = _l;
        return;
    }
};

vector<Edge> vt[N];

struct Question {
    int s, t, tot;
} qst[N];

struct LCA_To {
    int to, id;

    inline LCA_To(cint _to, cint _id) {
        to = _to, id = _id;
        return;
    }
};

vector<LCA_To> Q[N];
int lca[N];

int fa[N], pre[N];

int find_fa(cint x)
    {return x == fa[x] ? x : fa[x] = find_fa(fa[x]); }

int vis[N];
void calc_lca(cint p) {
    vis[p] = 1;
    for (auto i : vt[p])
        if (vis[i.to] == 0) {
            calc_lca(i.to);
            fa[i.to] = p;
            pre[i.to] = p;
        }
    vis[p] = 2;
    for (auto i : Q[p])
        if (vis[i.to])
            lca[i.id] = find_fa(i.to);
    return;
}

int tot[N];

void calc_tot(cint p) {
    for (auto i : vt[p])
        if (tot[i.to] == -1) {
            tot[i.to] = tot[p] + i.l;
            calc_tot(i.to);
        }
    return;
}

int cnt[N];

void calc_cnt(cint p) {
    for (auto i : vt[p])
        if (pre[i.to] == p) {
            calc_cnt(i.to);
            cnt[p] += cnt[i.to];
        }
    return;
}

bool check(cint x) {
    memset(cnt, 0, sizeof(int) * (n + 1));
    int cnt_tot = 0;
    for (int i = 1; i <= m; ++i)
        if (qst[i].tot > x) {
            ++cnt[qst[i].s];
            ++cnt[qst[i].t];
            cnt[lca[i]] -= 2;
            ++cnt_tot;
        }
    calc_cnt(1);
    int diff = 0;
    for (int i = 1; i <= n; ++i)
        if (cnt[i] == cnt_tot)
            diff = max(diff, tot[i] - tot[pre[i]]);
    for (int i = 1; i <= m; ++i)
        if (qst[i].tot > x && qst[i].tot - diff > x)
            return false;
    return true;
}

int main() {
    read(n), read(m);
    int edge_sum = 0;
    for (int i = n, x, y, z; --i; ) {
        read(x), read(y), read(z);
        vt[x].emplace_back(Edge(y, z));
        vt[y].emplace_back(Edge(x, z));
        edge_sum += z;
    }
    for (int i = 1; i <= m; ++i) {
        read(qst[i].s), read(qst[i].t);
        Q[qst[i].s].emplace_back(LCA_To(qst[i].t, i));
        Q[qst[i].t].emplace_back(LCA_To(qst[i].s, i));
    }
    { // calc LCA
        for (int i = 1; i <= n; ++i)
            fa[i] = i;
        calc_lca(1);
    }
    { // calc total of each question
        memset(tot, -1, sizeof(int) * (n + 1));
        tot[1] = 0;
        calc_tot(1);
        for (int i = 1; i <= m; ++i)
            qst[i].tot = tot[qst[i].s] + tot[qst[i].t] - (tot[lca[i]] << 1);
    }
    int l = -1, r = edge_sum;
    for (int mid; l + 1 < r; ) {
        mid = l + r >> 1;
        if (check(mid))
            r = mid;
        else
            l = mid;
    }
    printf("%d\n", r);
    return 0;
}
2024/10/19 12:51
加载中...