# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537579 | tqbfjotld | Paths (RMI21_paths) | C++14 | 184 ms | 35916 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 int long long
int K;
map<int,int > memo[100005];
vector<pair<int,int> > adjl[100005];
int dfs(int node, int pa, bool ig){
if (memo[node].count(pa)) return memo[node][pa];
int ans = 0;
int ans2 = -999999999;
int from1 = node;
for (auto x : adjl[node]){
if (ig && x.first==pa) continue;
auto res = dfs(x.first,node,ig)+x.second;
if (res>=ans){
from1 = x.first;
ans2 = ans;
ans = res;
}
else if (res>=ans2){
ans2 = res;
}
}
if (ig){
return memo[node][pa] = ans;
}
for (auto x : adjl[node]){
if (x.first==from1) memo[node][x.first] = ans2;
else memo[node][x.first] = ans;
}
memo[node][0] = ans;
return memo[node][pa];
}
main(){
int n;
scanf("%lld%lld",&n,&K);
for (int x = 0; x<n-1; x++){
int a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
adjl[a].push_back({b,c});
adjl[b].push_back({a,c});
}
for (int x = 1; x<=n; x++){
auto res = dfs(x,0,x==1);
int ans = res;
printf("%lld\n",ans);
}
}
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... |