比赛 |
2024暑假C班集训B |
评测结果 |
C |
题目名称 |
赛道修建 |
最终得分 |
0 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2024-07-11 11:32:47 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+10;
const ll mod = 998244353;
ll read(){
ll x = 0,f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
return x * f;
}
struct made{
int ver,nx;ll ed;
}e[N<<1];
int hd[N],tot;
void link(int x,int y,ll z){e[++tot] = {y,hd[x],z},hd[x] = tot;}
int n,m;
ll s = 0,d[N];
void dp(int x,int fa){
for(int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;ll z = e[i].ed;
if(y == fa)continue;
dp(y,x);//先遍历儿子
s = max(s,d[x] + d[y] + z);
d[x] = max(d[x],d[y] + z);
}
}
solve1(){
dp(1,0);
printf("%lld\n",s);
}
int main(){
freopen("2018track.in","r",stdin);
freopen("2018track.out","w",stdout);
n = read(),m = read();
for(int i = 1;i < n;i++){
int x = read(),y = read();ll z = read();
link(x,y,z),link(y,x,z);
}
if(m == 1)solve1();
return 0;
}