abcdeffa's Blog

当局者迷,旁观者清。

0%

GMOJ S5042 【最小直径】

Description

你有一个 $n$ 个点 $m$ 条边的森林,编号从 $0$ 开始,第 $i$ 条边有边权 $w_i$,你现在要添加若干边权为 $l$ 的边,使森林变为一颗树,
请你求出这棵树的直径最小是多少。

Solution

对于每棵树,我们求出它的 直径半径 ,那么答案为所有树的直径长度的最大值与第一大的半径加第二大的半径加 $l$ 再与第二大的半径加第三大的半径加 $2l$ 取 $\max$ 后的结果。

后两种情况是我们用半径最大的点为根做菊花图,这里用到了 贪心 的思想。

时间复杂度 $O(n \log n)$ 。

Code

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <cstdio>
#define inf 999999999
#define maxN 500010
#define maxM 1000010
struct nodea{ int h, v, q; } a[maxN];
struct nodeb{ int x, y, c, g; } b[maxM];
int color[maxN], dis[maxN], bj[maxN], f[maxN];
int last[maxN], ge[maxN], bh[maxN];
int len = 0, cnt = 0, n = 0, m = 0, L = 0;
int min (int x, int y)
{
return x < y ? x : y;
}
int max (int x, int y)
{
return x > y ? x : y;
}
int read ()
{
int x = 0;
char c = getchar();
while(c < '0' || c > '9')
{
c = getchar();
}
while(c >= '0' && c <= '9')
{
x = x * 10 + (c - '0');
c = getchar();
}
return x;
}
void ins (int x, int y, int c)
{
len++;
b[len].x = x;
b[len].y = y;
b[len].c = c;
b[len].g = a[x].h;
a[x].h = len;
}
void dfs (int x)
{
for(int i = a[x].h;i;i = b[i].g)
{
int y = b[i].y;
if(!color[y])
{
ge[++last[0]] = last[cnt];
bh[last[0]] = y;
last[cnt] = last[0];
color[y] = color[x];
dfs(y);
}
}
}
int bfs (int st, int flag)
{
for(int i = last[color[st]];i;i = ge[i])
{
int x = bh[i];
a[x].q = inf;
}
int tou = 1, wei = 2;
f[1] = st;
a[st].v = 1;
a[st].q = 0;
while(tou != wei)
{
int x = f[tou];
for(int i = a[x].h;i;i = b[i].g)
{
int y = b[i].y;
if(a[x].q + b[i].c < a[y].q)
{
a[y].q = a[x].q + b[i].c;
if(!a[y].v)
{
a[y].v = 1;
f[wei] = y;
wei++;
if(wei >= maxN)
{
wei = 1;
}
}
}
}
a[x].v = 0;
tou++;
if(tou >= maxN)
{
tou = 1;
}
}
for(int i = last[color[st]];i;i = ge[i])
{
int x = bh[i];
}
if(flag)
{
int ma = 0, wz = 0;
for(int i = last[color[st]];i;i = ge[i])
{
int x = bh[i];
if(a[x].q > ma)
{
wz = x;
ma = a[x].q;
}
}
return wz;
}
dis[st] = max(dis[st], a[st].q);
for(int i = last[color[st]];i;i = ge[i])
{
int x = bh[i];
dis[x] = max(dis[x], a[x].q);
}
return 0;
}
void px (int l, int r)
{
int x = l, y = r, mid = bj[(l + r) / 2];
while(x <= y)
{
while(bj[x] > mid)
{
x++;
}
while(bj[y] < mid)
{
y--;
}
if(x <= y)
{
int t = bj[x];
bj[x] = bj[y];
bj[y] = t;
x++;
y--;
}
}
if(l < y)
{
px(l, y);
}
if(x < r)
{
px(x, r);
}
}
int main ()
{
n = read();
m = read();
L = read();
for(int i = 1;i <= m; i++)
{
int x = 0, y = 0, c = 0;
x = read() + 1;
y = read() + 1;
c = read();
ins(x, y, c);
ins(y, x, c);
}
last[0] = 0;
for(int i = 1;i <= n; i++)
{
if(!color[i])
{
color[i] = ++cnt;
bh[++last[0]] = i;
last[cnt] = last[0];
dfs(i);
int X = bfs(i, 1);
int Y = bfs(X, 1);
bfs(X, 0);
bfs(Y, 0);
}
}
for(int i = 1;i <= cnt; i++)
{
bj[i] = 2147483647;
}
for(int i = 1;i <= n; i++)
{
if(dis[i] == inf)
{
dis[i] = 0;
}
bj[color[i]] = min(bj[color[i]], dis[i]);
}
px(1, cnt);
int ans = 0;
for(int i = 1;i <= n; i++)
{
ans = max(ans, dis[i]);
}
ans = max(ans, max(bj[1] + bj[2] + L, bj[2] + bj[3] + 2 * L));
printf("%d", ans);
return 0;
}