# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537544 | jamezzz | Paths (RMI21_paths) | C++17 | 129 ms | 12024 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
#define maxn 100005
int n,k,x[maxn],y[maxn],c[maxn],p[maxn];
ll d[maxn],ans[maxn];
vector<ii> AL[maxn];
void dfs(int u){
for(ii pr:AL[u]){
int i=pr.fi,v=pr.se;
if(v==p[u])continue;
p[v]=u;
d[v]=d[u]+c[i];
dfs(v);
}
}
int main(){
sf("%d%d",&n,&k);
for(int i=0;i<n-1;++i){
sf("%d%d%d",&x[i],&y[i],&c[i]);
AL[x[i]].pb({i,y[i]});
AL[y[i]].pb({i,x[i]});
}
d[1]=0;p[1]=0;dfs(1);
int mx=1;
for(int i=1;i<=n;++i)if(d[i]>d[mx])mx=i;
d[mx]=0;p[mx]=0;dfs(mx);
for(int i=1;i<=n;++i){
ans[i]=d[i];
if(d[i]>d[mx])mx=i;
}
d[mx]=0;p[mx]=0;dfs(mx);
for(int i=1;i<=n;++i){
ans[i]=max(ans[i],d[i]);
pf("%lld\n",ans[i]);
}
}
/*
11 3
1 2 5
2 3 3
2 6 5
3 4 4
3 5 2
1 7 6
7 8 4
7 9 5
1 10 1
10 11 1
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |