MnZn求助
查看原帖
MnZn求助
155767
Tarsal楼主2021/8/14 22:38

wa 0ps dkdkdk

后面有较大hack数据

#include <bits/stdc++.h>
using namespace std;
#define ls (x << 1)
#define rs (x << 1 | 1)
// #define mid ((l + r) >> 1)
#define int long long
#define Rep(x, a, b) for(int x = a; x <= b; ++ x)
#define Dep(x, a, b) for(int x = a; x >= b; -- x)
#define Next(i, x) for(int i = head[x]; i ; i = e[i].nxt)
int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
const int maxn = 3e5 + 10;
const int inf = 3e8;
int head[maxn], u[maxn], cnm[maxn], v[maxn], d[maxn], dis[maxn], n, m, cnt, ans = 1e9, num;
struct node { int to, nxt, val;} e[maxn << 1];
void add(int x, int y, int w) {
    e[++ cnt] = (node) { y, head[x], w}; head[x] = cnt;
} int a[maxn], fa[maxn][30], dep[maxn];
void dfs(int x, int F) {
    dep[x] = dep[F] + 1; fa[x][0] = F;
    Next(i, x) { int v = e[i].to;
        if(v == F) continue ;
        Rep(dep, 1, 20) fa[v][dep] = fa[fa[v][dep - 1]][dep - 1];
        dis[v] = dis[x] + e[i].val; cnm[v] = e[i].val;
        dfs(v, x);
    }
}
int LCA(int x, int y) {
    if(dep[x] > dep[y]) swap(x, y);
    Dep(i, 20, 0) if(dep[fa[y][i]] >= dep[x]) y = fa[y][i];
    // printf("x y : %d %d\n", x, y);
    Dep(i, 20, 0) if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
    return fa[x][0];
}
void Dfs(int x, int F) {
    // printf("in %d %d\n", x, F);
    Next(i, x) {
        int v = e[i].to, w = e[i].val;
        if(v == F) continue;
        Dfs(v, x);
        a[x] += a[v];
    }
}
bool check(int x) {
    memset(a, 0, sizeof(a));
    int _max = 0, Num = 0;
    Rep(i, 1, n) if(d[i] > x) {
        // if(x == 11) { printf("in : %d\n", i);}
        a[u[i]] ++, a[v[i]] ++, a[LCA(u[i], v[i])] -= 2;
        _max = max(_max, d[i] - x), ++ Num;
    } //if(x == 11) Rep(i, 1, n) printf("a : %d %d\n", i, a[i]);
    Dfs(1, 0);
    Rep(i, 1, n) { //if(x == 11) printf("%d %d %d %d %d\n", i, a[i], Num, cnm[i], _max);
        if(a[i] >= Num && _max <= cnm[i]) { ans = min(ans, x); return 1;}
    }
    return 0;
}
signed main() {
    n = read(), m = read();
    Rep(i, 2, n) {
        int a = read(), b = read(), t = read();
        add(a, b, t), add(b, a, t);
    } dfs(1, 0);
    // Rep(i, 1, n) Rep(j, 0, 5) printf("fa : %d %d %d\n", i, j, fa[i][j]);
    Rep(i, 1, m) { u[i] = read(), v[i] = read();
        d[i] = dis[u[i]] + dis[v[i]] - 2 * dis[LCA(u[i], v[i])];
        // printf("%d %d\n", LCA(u[i], v[i]), d[i]);
    } int l = 1, r = inf;
    while(l <= r) {
        // printf("%d %d\n", l, r);
        int mid = l + r >> 1;
        // printf("mid : %d\n", mid);
        if(check(mid)) r = mid - 1;
        else l = mid + 1;
    } printf("%d\n", ans);
    return 0;
}
100 1
7 97 4
89 2 0
40 91 1
70 84 1
36 92 3
28 20 0
25 100 1
76 56 2
58 47 3
87 76 0
57 51 4
6 36 0
71 47 0
13 50 3
83 98 5
19 36 1
75 26 3
50 86 2
81 78 1
70 41 5
44 4 2
21 90 1
18 65 4
51 93 3
22 38 0
10 89 3
28 83 3
72 29 3
62 81 0
25 35 0
5 71 2
17 62 1
88 68 0
10 11 3
4 80 2
56 99 4
27 94 2
53 54 4
67 37 4
40 52 5
23 30 4
64 70 5
52 85 4
22 92 4
13 91 0
90 32 1
61 65 2
81 34 0
75 43 0
8 5 4
38 1 1
12 45 2
68 31 4
97 95 4
38 94 5
81 48 5
12 61 5
60 97 0
39 41 3
46 99 1
32 52 5
55 11 2
29 11 3
71 85 4
55 77 3
72 70 4
8 51 5
8 24 2
64 95 5
79 84 5
30 63 0
27 35 1
73 69 4
83 75 2
87 33 0
98 9 1
66 31 0
3 33 2
30 16 3
87 80 1
4 48 0
16 55 0
73 32 5
14 17 5
29 36 4
76 37 5
96 9 3
81 31 2
8 9 2
16 49 1
83 15 0
68 54 2
18 58 1
61 84 3
83 42 3
26 82 3
42 53 0
25 59 1
21 74 0
96 81


19

2021/8/14 22:38
加载中...