1 solutions

  • 0
    @ 2025-11-5 15:00:27

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 2e5 + 9, M = 4e5 + 9;
    int n, len;
    int h[N], to[M], wt[M], ne[M], idx;
    void add(int u, int v, int w) {
    	++ idx; to[idx] = v; wt[idx] = w; ne[idx] = h[u]; h[u] = idx;
    }
    
    int d[N];
    void dfs(int u, int fa) {
    	for (int i = h[u]; i; i = ne[i]) {
    		int v = to[i], w = wt[i];
    		if (v == fa) continue;
    		
    		dfs(v, u);
    		len = max(len, d[u] + d[v] + w);
    		d[u] = max(d[u], d[v] + w);
    	}
    }
    
    int main() {
    	scanf("%d", &n);
    	for (int i = 1; i < n; i ++) {
    		int u, v, w;
    		scanf("%d %d %d", &u, &v, &w);
    		add(u, v, w); add(v, u, w);
    	}
    	
    	dfs(1, 0);
    	cout << 10 * len + len * (len + 1) / 2;
    
    	return 0;
    }
    
    

    Java :

    import java.io.*;
    import java.util.Arrays;
    import java.util.LinkedList;
    import java.util.Map;
    import java.util.Queue;
    
    /**
     * @author yale.liu
     * @since 2022/8/31 10:34
     */
    public class Main {
    
        static int MAX_N ;
        static int h[],to[],next[],cost[];
        static int idx;
        static int max;
        static int i1,i2;
        static int[] ans;
    
        public static void main(String[] args) throws IOException {
    
    
            StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    
    //         new BufferedReader(new InputStreamReader(System.in))
            in.nextToken();
            int n = (int)in.nval;
    
            MAX_N = 2*n +1;
            h = new int[MAX_N];
            Arrays.fill(h,-1);
            to = new int[MAX_N];
            next = new int[MAX_N];
            cost = new int[MAX_N];
            ans = new int[MAX_N];
            for (int i = 0; i < n-1; i++) {
                in.nextToken();
                int a =(int)in.nval;
                in.nextToken();
                int b =(int)in.nval;
                in.nextToken();
                int c =(int)in.nval;
                insert(a,b,c);
                insert(b,a,c);
            }
    
            dfs(1,-1,0);
            max=0;
            dfs2(i1,-1,0);
            System.out.println(ans[i2]);
    
        }
    
        private static void dfs(int root, int fa, int dep){
            if (dep>max){
                max = dep;
                i1 = root;
            }
            for (int i = h[root]; i !=-1 ; i=next[i]) {
                if (to[i] == fa) continue;
                dfs(to[i],root,dep+cost[i]);
            }
        }
    
        private static void dfs2(int root, int fa, int dep){
            if (dep>max){
                max = dep;
                i2 = root;
                ans[root] = dep*10+ (int)((1+dep)*(dep/2.0));
            }
            for (int i = h[root]; i !=-1 ; i=next[i]) {
                if (to[i] == fa) continue;
                dfs2(to[i],root,dep+cost[i]);
            }
    
    
        }
    
    
        private static void insert(int a, int b, int c){
            to[idx] = b;
            cost[idx] = c;
            next[idx] = h[a];
            h[a] = idx;
            idx++;
        }
    }
    
    
    • 1

    Information

    ID
    16317
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By