NC17474. 插排树
描述
输入描述
第一行一个整数n表示有n个节点
然后n-1行,每行三个整数a,b,c,表示插排a是接在插排b上的,插排a的长度为c
输出描述
一个整数n表示最远距离
示例1
输入:
9 2 1 2 3 1 2 4 1 1 5 2 3 6 2 1 7 3 1 8 3 4 9 7 5
输出:
8
说明:
1=>3=>7=>9C(clang11) 解法, 执行用时: 15ms, 内存消耗: 380K, 提交时间: 2021-02-10 21:17:34
#include<stdio.h> #include<math.h> int main() { int n,t,s[100001]; scanf("%d",&n); while(n--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); s[a] = s[b]+c; t=fmax(t,s[a]); } printf("%d",t); return 0; }
Python(2.7.3) 解法, 执行用时: 125ms, 内存消耗: 11588K, 提交时间: 2018-08-10 20:19:43
import sys n = int(sys.stdin.readline()) l = [] for _ in range(n-1): l.append([int(x) for x in sys.stdin.readline().split()]) d = [0] * (n + 1) for a, b, c in l: d[a] = d[b] + c print sorted(d)[-1]
C++11(clang++ 3.9) 解法, 执行用时: 42ms, 内存消耗: 732K, 提交时间: 2018-08-11 17:56:39
#include<bits/stdc++.h> using namespace std;int n,a,b,c,i,M=0,s[50050];int main(){cin>>n;for(i=1;i<n;i++)cin>>a>>b>>c,s[a]=s[b]+c,M=max(M,s[i]);cout<<M;}