ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#463 | #87. 「NOIP2018」赛道修建 | Pigsyy | 0 | 0ms | 0kb | C++23 | 5.6kb | 2025-04-24 20:42:40 | 2025-04-24 20:42:41 |
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
#define rr register
#define nn 50010
using namespace std;
int read() {
int re = 0, sig = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
sig = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
re = (re << 1) + (re << 3) + c - '0';
c = getchar();
}
return re * sig;
}
struct node {
int to, len, nxt;
} ed[nn * 2];
int head[nn];
void addedge(int u, int v, int len) {
static int top = 1;
ed[top].len = len, ed[top].to = v;
ed[top].nxt = head[u], head[u] = top;
top++;
ed[top].len = len, ed[top].to = u;
ed[top].nxt = head[v], head[v] = top;
top++;
}
int n, m;
int lensum = 0;
bool vis[nn];
int f[nn];
int cnt;
void work1() {
int k, maxn;
int dis[nn];
queue <int> q;
memset(dis, -1, sizeof(dis));
dis[1] = 0, q.push(1);
while (!q.empty()) {
k = q.front(), q.pop();
for (int i = head[k] ; i ; i = ed[i].nxt) {
if (dis[ed[i].to] == -1) {
dis[ed[i].to] = dis[k] + ed[i].len;
q.push(ed[i].to);
}
}
}
maxn = 0;
for (int i = 1 ; i <= n ; i++)
if (dis[i] > maxn)
maxn = dis[i], k = i;
memset(dis, -1, sizeof(dis));
dis[k] = 0, q.push(k);
while (!q.empty()) {
k = q.front(), q.pop();
for (int i = head[k] ; i ; i = ed[i].nxt) {
if (dis[ed[i].to] == -1) {
dis[ed[i].to] = dis[k] + ed[i].len;
q.push(ed[i].to);
}
}
}
maxn = 0;
for (int i = 1 ; i <= n ; i++)
if (dis[i] > maxn)
maxn = dis[i];
printf("%d", maxn);
}
int tmp[nn], siz = 0;
bool cmp(int a, int b) {
return a > b;
}
void dfs(rr int x, rr int maxn) {
/*
vis[x] = true;
vector <int> tmp;
for(int i = head[x] ; i ; i = ed[i].nxt)
if(!vis[ed[i].to]){
dfs(ed[i].to , maxn);
if(f[ed[i].to] + ed[i].len >= maxn) cnt++;
else tmp.push_back(f[ed[i].to] + ed[i].len);
}
int nowcnt = 0;
if(tmp.empty())return;
sort(tmp.begin() , tmp.end() , cmp);
int i = 0 , j = tmp.size() - 1;
for( ; i < j ; i++){
while(tmp[i] + tmp[j] < maxn)
j--;
if(i >= j)
break;
cnt++; nowcnt++;
j--;
}
f[x] = 0;
int l = 0 , r = tmp.size() - 1;
int mid , res;
while(l <= r){
mid = (l + r) / 2;
rr int cntt = 0;
rr int i = 1 , j = siz;
for( ; i < j ; i++){
if(i == mid)continue;
while(tmp[i] + tmp[j] < maxn || j == mid)
j--;
if(i >= j)
break;
cntt++;
j--;
}
if(nowcnt == cntt) r = mid - 1 , res = mid;
else l = mid + 1;
}
f[x] = tmp[res];
// printf("%d\n" , tmp.size());
return;
/*/
vis[x] = true;
for (rr int i = head[x] ; i ; i = ed[i].nxt)
if (!vis[ed[i].to]) {
dfs(ed[i].to, maxn);
}
siz = 0;
for (rr int i = head[x] ; i ; i = ed[i].nxt)
if (!vis[ed[i].to]) {
if (f[ed[i].to] + ed[i].len >= maxn)
cnt++;
else
tmp[++siz] = f[ed[i].to] + ed[i].len;
}
// if(siz == 0)return;
sort(tmp + 1, tmp + siz + 1, cmp);
int nowcnt = 0;
for (rr int i = 1, j = siz ; i < j ; ++i) {
while (tmp[i] + tmp[j] < maxn)
--j;
if (i >= j)
break;
++nowcnt;
--j;
}
f[x] = 0;
rr int l = 1, r = siz;
rr int mid;
rr int res = 0;
while (l <= r) {
mid = (l + r) / 2;
rr int cntt = 0;
rr int i = 1, j = siz;
for (; i < j ; i++) {
if (i == mid)
continue;
while (tmp[i] + tmp[j] < maxn || j == mid)
--j;
if (i >= j)
break;
++cntt;
--j;
}
if (nowcnt == cntt)
r = mid - 1, res = mid;
else
l = mid + 1;
}
cnt += nowcnt;
f[x] = tmp[res];
vis[x] = false;
//*/
/* cout << x << ":\n";
for(int i = 1 ; i <= siz ; i ++)
cout << tmp[i] << '\t';
cout << endl;*/
}
bool check(int maxn) {
memset(f, 0, sizeof(f));
memset(vis, 0, sizeof(vis));
cnt = 0;
dfs(1, maxn);
return cnt >= m;
}
int main() {
freopen("track.in", "r", stdin);
freopen("track.out", "w", stdout);
bool ty1, ty2;
ty1 = ty2 = true;
n = read();
m = read();
for (int i = 1 ; i < n ; i++) {
int u, v, len;
u = read(), v = read(), len = read();
lensum += len;
addedge(u, v, len);
}
if (m == 1) {
work1();
return 0;
}
rr int l = 1, r = lensum;
r /= m;
while (l < r) {
int mid = (l + r) / 2;
if ((l + r) & 1)
mid++;
if (check(mid))
l = mid;
else
r = mid - 1;
}
printf("%d", l);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 0
Dangerous Syscalls
input:
5 1 5 2 2277 2 3 8305 3 4 1648 1 4 7142
output:
result:
Test #2:
score: 0
Dangerous Syscalls
input:
10 3 3 4 5767 2 3 12 1 2 3285 5 6 167 7 8 7227 6 7 5877 9 10 2738 8 9 5248 4 5 304
output:
result:
Test #3:
score: 0
Dangerous Syscalls
input:
15 8 1 14 1664 1 15 9705 1 4 1253 1 13 8584 1 6 596 1 5 4817 1 10 711 1 3 9624 1 7 191 1 8 311 1 9 3...
output:
result:
Test #4:
score: 0
Dangerous Syscalls
input:
1000 1 145 694 9490 246 666 127 504 549 4214 126 4 467 716 398 5147 843 268 2822 965 737 1708 125 77...
output:
result:
Test #5:
score: 0
Dangerous Syscalls
input:
30000 1 1 9319 27 1 20542 64 1 29635 3088 1 18236 233 1 12141 393 1 11636 9741 1 29826 5941 1 12433 ...
output:
result:
Test #6:
score: 0
Dangerous Syscalls
input:
30000 1 20986 4998 3777 16151 4721 4339 4998 13650 3630 4998 5496 9957 1050 23653 4955 9064 5154 712...
output:
result:
Test #7:
score: 0
Dangerous Syscalls
input:
30000 12153 1 24986 418 1 27937 8452 1 26948 8046 1 19051 4923 1 16197 8992 1 25873 764 1 27163 3369...
output:
result:
Test #8:
score: 0
Dangerous Syscalls
input:
50000 25004 1 35561 2992 1 39668 9779 1 16057 6973 1 38469 9544 1 10158 2579 1 47550 7327 1 23210 23...
output:
result:
Test #9:
score: 0
Dangerous Syscalls
input:
1000 126 702 703 8245 797 798 6588 145 146 3252 223 224 939 898 899 7664 440 441 335 774 775 2432 42...
output:
result:
Test #10:
score: 0
Dangerous Syscalls
input:
30000 14488 6526 6527 2384 16239 16240 9407 29265 29266 6277 8387 8388 8359 20147 20148 2733 23095 2...
output:
result:
Test #11:
score: 0
Dangerous Syscalls
input:
50000 11961 30635 30636 2836 32537 32538 1146 18668 18669 4217 38774 38775 9576 15517 15518 5419 494...
output:
result:
Test #12:
score: 0
Dangerous Syscalls
input:
50 7 5 36 6378 50 15 963 34 1 9975 18 12 3049 11 28 2752 48 42 2056 23 14 1912 29 30 4068 3 29 6339 ...
output:
result:
Test #13:
score: 0
Dangerous Syscalls
input:
50 14 14 21 486 35 20 679 50 13 4050 23 22 2791 5 26 5047 21 16 719 9 16 2191 15 24 1913 19 15 85 13...
output:
result:
Test #14:
score: 0
Dangerous Syscalls
input:
200 46 150 60 8027 147 61 7025 84 42 1403 192 99 440 83 40 4493 117 5 720 17 198 1368 98 45 451 67 8...
output:
result:
Test #15:
score: 0
Dangerous Syscalls
input:
200 67 41 108 1555 129 189 3119 141 126 8840 164 194 6607 81 120 5421 187 58 3196 199 1 5910 121 7 7...
output:
result:
Test #16:
score: 0
Dangerous Syscalls
input:
1000 326 43 37 183 760 571 207 729 521 195 368 470 7077 812 959 241 595 154 9584 894 417 4367 22 745...
output:
result:
Test #17:
score: 0
Dangerous Syscalls
input:
1000 337 815 28 2724 289 887 8908 991 24 4924 169 328 5923 24 624 4560 403 97 1838 388 126 904 664 6...
output:
result:
Test #18:
score: 0
Dangerous Syscalls
input:
30000 2963 18227 3118 9758 26780 1975 127 8725 17234 1940 18480 3240 2737 29837 17150 236 9057 21401...
output:
result:
Test #19:
score: 0
Dangerous Syscalls
input:
30000 10538 19555 430 254 12377 13079 1036 22092 2834 9245 15496 15474 8290 13079 17304 9160 13079 9...
output:
result:
Test #20:
score: 0
Dangerous Syscalls
input:
50000 8650 44867 4444 588 28695 10409 5802 13247 10533 3738 40938 45080 1735 27089 37453 3588 4263 7...