Logo Wy Online Judge

WyOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#439#72. 「NOIP2015」运输计划Pigsyy100732ms35312kbC++233.5kb2025-04-24 13:28:322025-04-24 13:28:33

answer

#include <cctype>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
constexpr int kB = 1 << 20;
char rbuf[kB + 1], *ps = rbuf + kB;
char Getchar() {
    if (ps - rbuf == kB)
        std::cin.read(rbuf, kB), ps = rbuf;

    return *ps++;
}
template <typename T = int>
T Read() {
    T x = 0;
    int f = 1;
    char c;

    while (!std::isdigit(c = Getchar()))
        if (c == '-')
            f = -1;

    x = c - '0';

    while (std::isdigit(c = Getchar()))
        x = x * 10 + c - '0';

    return x * f;
}
template <typename T>
void Read(T &x) {
    x = 0;
    int f = 1;
    char c;

    while (!std::isdigit(c = Getchar()))
        if (c == '-')
            f = -1;

    x = c - '0';

    while (std::isdigit(c = Getchar()))
        x = x * 10 + c - '0';

    x *= f;
}
template <typename T, typename... Args>
void Read(T &x, Args &...args) {
    Read(x), Read(args...);
}
template <typename T1, typename T2>
void checkmax(T1 &x, T2 y) {
    if (x < y)
        x = y;
}
template <typename T1, typename T2>
void checkmin(T1 &x, T2 y) {
    if (x > y)
        x = y;
}
struct Edge {
    int to, nxt, cost;
} e[600001];
int n, m, E, head[300001], dis[300001], dep[300001], fa[300001], sz[300001],
    son[300001], top[300001], w[300001], seq[300001], tot;
void Add(int f, int t, int c) {
    e[E] = {t, head[f], c}, head[f] = E++;
}
void Dfs(int u, int f) {
    sz[u] = 1, dep[u] = dep[fa[u] = f] + 1;

    for (int i = head[u]; i != -1; i = e[i].nxt) {
        int v = e[i].to, c = e[i].cost;

        if (v == f)
            continue;

        dis[v] = dis[u] + (w[v] = c), Dfs(v, u), sz[u] += sz[v];

        if (sz[v] > sz[son[u]])
            son[u] = v;
    }
}
void Dfs2(int u, int t) {
    seq[++tot] = u, top[u] = t;

    if (son[u])
        Dfs2(son[u], t);

    for (int i = head[u]; i != -1; i = e[i].nxt) {
        int v = e[i].to;

        if (v == fa[u] || v == son[u])
            continue;

        Dfs2(v, v);
    }
}
int GetLca(int u, int v) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]])
            std::swap(u, v);

        u = fa[top[u]];
    }

    return dep[u] < dep[v] ? u : v;
}
struct Route {
    int u, v, lca, len;
} b[300001];
int a[300001];
bool Check(int x) {
    int cnt = 0, lim = 0;
    std::memset(a + 1, 0, 4 * n);

    for (int i = 1; i <= m; i++)
        if (b[i].len > x) {
            cnt++, checkmax(lim, b[i].len - x);
            a[b[i].u]++, a[b[i].v]++, a[b[i].lca] -= 2;
        }

    for (int i = n; i >= 1; i--) {
        int u = seq[i];

        if (a[u] == cnt && w[u] >= lim)
            return true;

        a[fa[u]] += a[u];
    }

    return false;
}
int main(int argc, char const *argv[]) {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    Read(n, m);
    std::memset(head + 1, -1, 4 * n);

    for (int i = 1; i < n; i++) {
        int u = Read(), v = Read(), c = Read();
        Add(u, v, c), Add(v, u, c);
    }

    Dfs(1, 0), Dfs2(1, 1);

    for (int i = 1; i <= m; i++) {
        Read(b[i].u, b[i].v);
        b[i].lca = GetLca(b[i].u, b[i].v);
        b[i].len = dis[b[i].u] + dis[b[i].v] - 2 * dis[b[i].lca];
    }

    int l = 0, r = 1000 * n, ans = -1;

    while (l <= r) {
        int mid = (l + r) >> 1;

        if (Check(mid))
            ans = mid, r = mid - 1;
        else
            l = mid + 1;
    }

    std::cout << ans;
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 4ms
memory: 13624kb

input:

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
...

output:

19

result:

ok "19"

Test #2:

score: 5
Accepted
time: 0ms
memory: 19628kb

input:

100 100
1 2 5
2 3 4
3 4 4
4 5 4
5 6 4
6 7 4
7 8 4
8 9 4
9 10 4
10 11 4
11 12 5
12 13 5
13 14 4
14 15...

output:

148

result:

ok "148"

Test #3:

score: 5
Accepted
time: 2ms
memory: 13612kb

input:

100 100
47 81 0
59 86 3
78 12 3
47 70 3
60 25 2
13 9 1
98 16 3
95 41 1
6 61 2
26 88 4
45 32 1
4 27 5...

output:

87

result:

ok "87"

Test #4:

score: 5
Accepted
time: 2ms
memory: 13540kb

input:

2000 1
250 1494 838
537 1817 65
1457 249 786
392 532 696
464 284 728
31 1166 37
8 229 717
3 935 222
...

output:

0

result:

ok "0"

Test #5:

score: 5
Accepted
time: 3ms
memory: 15832kb

input:

1000 1000
1 2 992
2 3 993
3 4 998
4 5 993
5 6 990
6 7 992
7 8 995
8 9 991
9 10 996
10 11 997
11 12 9...

output:

198081

result:

ok "198081"

Test #6:

score: 5
Accepted
time: 3ms
memory: 13824kb

input:

2000 2000
1 2 992
2 3 999
3 4 991
4 5 1000
5 6 997
6 7 994
7 8 997
8 9 993
9 10 998
10 11 993
11 12 ...

output:

198031

result:

ok "198031"

Test #7:

score: 5
Accepted
time: 7ms
memory: 16240kb

input:

3000 3000
1 2 996
2 3 999
3 4 990
4 5 996
5 6 998
6 7 996
7 8 999
8 9 999
9 10 999
10 11 990
11 12 9...

output:

197977

result:

ok "197977"

Test #8:

score: 5
Accepted
time: 3ms
memory: 19768kb

input:

1000 1000
623 1 287
142 993 996
317 578 995
946 177 998
261 994 500
651 541 998
712 264 996
828 909 ...

output:

476815

result:

ok "476815"

Test #9:

score: 5
Accepted
time: 7ms
memory: 15840kb

input:

2000 2000
456 953 996
1231 1518 999
1117 936 995
1230 1354 998
1223 27 998
733 845 999
497 1387 995
...

output:

951734

result:

ok "951734"

Test #10:

score: 5
Accepted
time: 3ms
memory: 15896kb

input:

3000 3000
2759 1237 997
1280 2052 999
200 1417 1000
578 2215 1000
685 1260 996
2127 1902 998
929 234...

output:

1426537

result:

ok "1426537"

Test #11:

score: 5
Accepted
time: 33ms
memory: 18520kb

input:

80000 1
54731 72034 911
5545 25122 923
6888 12608 877
3703 28962 503
21165 13946 861
27574 2078 809
...

output:

5824898

result:

ok "5824898"

Test #12:

score: 5
Accepted
time: 44ms
memory: 19088kb

input:

100000 1
13948 19012 139
17382 3319 549
40006 1758 996
67534 73344 474
35101 62664 404
86444 84025 3...

output:

3917912

result:

ok "3917912"

Test #13:

score: 5
Accepted
time: 38ms
memory: 26780kb

input:

70000 70000
1 2 993
2 3 993
3 4 1000
4 5 991
5 6 999
6 7 993
7 8 996
8 9 991
9 10 993
10 11 997
11 1...

output:

198052

result:

ok "198052"

Test #14:

score: 5
Accepted
time: 44ms
memory: 26240kb

input:

80000 80000
1 2 993
2 3 999
3 4 998
4 5 999
5 6 992
6 7 990
7 8 996
8 9 1000
9 10 991
10 11 994
11 1...

output:

198109

result:

ok "198109"

Test #15:

score: 5
Accepted
time: 44ms
memory: 26316kb

input:

90000 90000
1 2 997
2 3 996
3 4 992
4 5 990
5 6 993
6 7 994
7 8 1000
8 9 996
9 10 990
10 11 994
11 1...

output:

198093

result:

ok "198093"

Test #16:

score: 5
Accepted
time: 59ms
memory: 30932kb

input:

100000 100000
1 2 992
2 3 995
3 4 995
4 5 990
5 6 990
6 7 991
7 8 1000
8 9 993
9 10 994
10 11 994
11...

output:

198036

result:

ok "198036"

Test #17:

score: 5
Accepted
time: 49ms
memory: 21596kb

input:

80000 80000
32648 9718 998
69876 32351 999
20984 13505 996
50253 15719 998
2010 38951 999
78393 2053...

output:

38000613

result:

ok "38000613"

Test #18:

score: 5
Accepted
time: 64ms
memory: 23292kb

input:

90000 90000
76441 85711 996
55850 45344 999
84561 22075 999
77487 89094 998
81698 30175 995
61207 36...

output:

42751360

result:

ok "42751360"

Test #19:

score: 5
Accepted
time: 84ms
memory: 26816kb

input:

100000 100000
51869 61218 995
57793 42005 997
82875 32465 1000
27167 74661 999
47183 12541 998
85456...

output:

47501733

result:

ok "47501733"

Test #20:

score: 5
Accepted
time: 239ms
memory: 35312kb

input:

300000 300000
278718 186322 996
75883 200663 995
102434 126640 999
209901 181570 996
280005 118903 9...

output:

142501313

result:

ok "142501313"