[NOIP2015]运输计划

Description


$$
1 \le n, m \le 3 \times 10 ^5
$$

Solution

怀念以前 NOIP 的难度…

默认以 1 为根,且化边权为点权。1 的点权未定义。

答案显然有单调性,于是二分答案 $p$。

那么航道长度大于 $p$ 的都至少需要一个虫洞,故把这些航道拿出来单独考虑。

首先这个虫洞显然会建在这些航道路径的交集上。

这个可以用树上差分很方便的维护。具体地,将每条航道的两个端点打上 +1 标记,lca 打上 -2 标记(已化边为点),最后统计子树和,如果一个点子树标记和与这些长度大于 $p$ 的航道的路径条数相等(即所有航道全部经过了这个点与其父亲之间的连边),那么它与其父亲所连的这条边在交集中。

考虑选交集中的哪条边最优。因为只能选一条,易得一定是选边权最大的那条。

然后就做完了。(感觉我在水)

Code

std

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>

const int maxN = 3e5 + 2;

int n, m, ecnt;
int head[maxN], st[maxN], ed[maxN], lca[maxN], len[maxN];
int top[maxN], size[maxN], dep[maxN], son[maxN], wet[maxN], fa[maxN], dis[maxN];
int tag[maxN];
int next[maxN << 1], to[maxN << 1], val[maxN << 1];

inline bool Chkmax(int& x, int y)
{ return x < y ? x = y, true : false; }

inline int read()
{
int x = 0;
char ch;
while (!isdigit(ch = getchar()));
while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar()));
return x;
}

inline void Link(int u, int v, int w)
{
++ecnt;
next[ecnt] = head[u], to[ecnt] = v, val[ecnt] = w;
head[u] = ecnt;
}

void DFS1(int u)
{
size[u] = 1;
for (int i = head[u]; i; i = next[i])
{
int v = to[i];
if (v == fa[u])
continue;
fa[v] = u;
dep[v] = dep[u] + 1, wet[v] = val[i], dis[v] = dis[u] + val[i];
DFS1(v);
size[u] += size[v];
if (size[v] > size[son[u]])
son[u] = v;
}
}

void DFS2(int u)
{
if (son[u])
top[son[u]] = top[u], DFS2(son[u]);
for (int i = head[u]; i; i = next[i])
{
int v = to[i];
if (!top[v])
top[v] = v, DFS2(v);
}
}

int LCA(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] ? v : u;
}

void Getsum(int u)
{
for (int i = head[u]; i; i = next[i])
{
int v = to[i];
if (v != fa[u])
Getsum(v), tag[u] += tag[v];
}
}

bool Check(int lim)
{
int cnt = 0, max = 0, maxx = 0;
memset(tag + 1, 0, n * sizeof(int));
for (int i = 1; i <= m; ++i)
{
if (len[i] > lim)
{
Chkmax(maxx, len[i]);
++cnt;
++tag[st[i]], ++tag[ed[i]];
tag[lca[i]] -= 2;
}
}
Getsum(1);
for (int i = 1; i <= n; ++i)
{
if (tag[i] > cnt)
throw 0;
if (tag[i] == cnt)
Chkmax(max, wet[i]);
}
return maxx - max <= lim;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("wib.in", "r", stdin);
freopen("wib.out", "w", stdout);
#endif
int l = 0, r = 0, mid;
n = read(), m = read();
for (int i = 2; i <= n; ++i)
{
int u = read(), v = read(), w = read();
Link(u, v, w), Link(v, u, w);
}
DFS1(1);
top[1] = 1, DFS2(1);
for (int i = 1; i <= m; ++i)
{
st[i] = read(), ed[i] = read();
lca[i] = LCA(st[i], ed[i]);
len[i] = dis[st[i]] - dis[lca[i]] + dis[ed[i]] - dis[lca[i]];
Chkmax(r, len[i]);
}
while (l < r)
{
mid = (l + r) >> 1;
if (Check(mid))
r = mid;
else
l = mid + 1;
}
printf("%d\n", l);
return 0;
}

gen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from random import randint
import os
from cyaron import *
cnt = 0
while True :
n = int(5)
m = int(5)
tree = Graph.tree(n, weight_limit = 10)
outs = "%d %d\n" % (n, m)
for i in tree.iterate_edges() :
outs += "%d %d %d\n" % (i.start, i.end, i.weight)
for i in range(m) :
outs += "%d %d\n" % (randint(1, n), randint(1, n))
print(outs, file = open("wib.in", "w"))
os.system("./bf && ./wib")
if os.system("diff bf.out wib.out") :
print("WA")
exit()