目录
测试点信息
Subtask #0
#1
P2680_1.in
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
P2680_1.out
19
图:
我发现之间有5这个长度,弄这个是因为我差分写错了没发现。。
50分参考代码(开了n^2的数组,MLE了):
//所有飞船一起飞的啊,,,
//二分,分的是答案,超过的他们去减边,如果可以都比mid小,那么答案成立
struct edge
{
int u, v;
ll elen;
};
bool cmp(edge a,edge b)
{
return a.elen < b.elen;
}
void solve()
{
int n, m;
cin >> n >> m;
vector<vector<int>>alist(n + 1);
vector<vector<int>>cost(n + 1,vector<int>(n+1));
for (int i = 0; i < n - 1; i++)
{
int a, b, t;
cin >> a >> b >> t;
alist[a].push_back(b);
alist[b].push_back(a);
cost[a][b] = cost[b][a] = t;
}
vector<vector<int>>fa(n + 1, vector<int>(22));
vector<int>dep(n + 1);
vector<int>plen(n + 1);
auto dfs = [&](int cur, int pa, auto dfs)->void {
dep[cur] = dep[pa] + 1;
plen[cur] += cost[cur][pa];
for (auto x : alist[cur])
{
if (x != pa)
{
fa[x][0] = cur;
plen[x] = plen[cur];
dfs(x, cur, dfs);
}
}
};
dfs(1, 0, dfs);
//倍增求父亲
for (int p = 1; p < 22; p++)
{
for (int i = 1; i <= n; i++)
{
fa[i][p] = fa[fa[i][p - 1]][p - 1];
}
}
auto LCA = [&](int a, int b)->pair<int, int>
{
ll ans = 0;
if (dep[a] < dep[b])swap(a, b);
while (dep[a]