結果

問題 No.872 All Tree Path
ユーザー k_6101k_6101
提出日時 2019-12-07 15:08:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,696 ms / 3,000 ms
コード長 3,476 bytes
コンパイル時間 2,985 ms
コンパイル使用メモリ 77,076 KB
実行使用メモリ 149,880 KB
最終ジャッジ日時 2023-08-26 19:00:20
合計ジャッジ時間 20,175 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,394 ms
118,792 KB
testcase_01 AC 1,597 ms
118,848 KB
testcase_02 AC 1,407 ms
120,532 KB
testcase_03 AC 1,696 ms
149,880 KB
testcase_04 AC 108 ms
56,328 KB
testcase_05 AC 1,419 ms
119,804 KB
testcase_06 AC 1,401 ms
119,256 KB
testcase_07 AC 1,376 ms
120,028 KB
testcase_08 AC 359 ms
63,024 KB
testcase_09 AC 355 ms
63,036 KB
testcase_10 AC 359 ms
62,980 KB
testcase_11 AC 355 ms
63,036 KB
testcase_12 AC 370 ms
65,072 KB
testcase_13 AC 117 ms
55,652 KB
testcase_14 AC 111 ms
55,496 KB
testcase_15 AC 118 ms
55,688 KB
testcase_16 AC 117 ms
55,880 KB
testcase_17 AC 122 ms
55,740 KB
testcase_18 AC 119 ms
55,708 KB
testcase_19 AC 117 ms
55,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;

import static java.util.Comparator.*;

public class Main {
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        Solver solver = new Solver(System.in, out);
        solver.solve();
        out.close();
    }
}
class Solver {
	Scanner sc;
	PrintWriter out;
    public Solver(InputStream in, PrintWriter out) {
    	sc = new Scanner(in);
    	this.out = out;
    }
    // ==================================================================
    GraphWith G = new GraphWith();
    long total = 0;
    int N;
    int dfs(int now, int pre) {
    	int ans = 1, cnt;
    	for(PP p : G.get(now)) {
    		if(p.key == pre)	continue;
    		cnt= dfs(p.key, now);
    		total += (long)p.val * ((long)cnt * (N - cnt)) * 2;
//    		out.println("total = " + total + "  <-- val[" + p.val + "] * (" 
//    			+ cnt + " * (" + N + " - " + cnt + ")) * 2 を足した結果");
    		ans += cnt;
    	}
//    	out.println("now = " + now + "  pre = " + pre + "  -->  ans = " + ans);
    	return ans;
    }
    public void solve() {
    	N = Integer.parseInt(sc.next());
    	int u, v, w;
    	for (int i = 0; i < N - 1; i++) {
        	u = Integer.parseInt(sc.next()) - 1;
        	v = Integer.parseInt(sc.next()) - 1;
        	w = Integer.parseInt(sc.next());
        	G.add(u, new PP(v,w));
        	G.add(v, new PP(u,w));
		}
    	dfs(0, -1);
    	out.println(total);
    }
    // 重さを持ったグラフのリンクリスト
    class GraphWith {
    	// キーに紐づくリストに、頂点番号と重さのペアを持つ
        private Map<Integer, List<PP>> data = new HashMap<Integer, List<PP>>();
        // 指定された頂点に紐づく、頂点と重さのペアを追加する
        void add(int key, PP p) {
        	List<PP> list = data.get(key);
        	if(list == null) {
        		list = new ArrayList<PP>();
        		data.put(key, list);
        	}
        	list.add(p);
        }
        // 頂点に紐づく、頂点と重さのペアのリストを返す
        List<PP> get(int key) {
        	return data.get(key);
        }
        // 頂点 key が登録されているか?
        boolean contains(int key) {
        	return data.containsKey(key);
        }
        
        // 頂点のセットを返す
        Set<Integer> keySet() {
        	return data.keySet();
        }
        // 頂点 key_1 と 頂点 key_2 がつながっていれば true を返す
        boolean isConnect(int key_1, int key_2) {
        	List<PP> list = data.get(key_1);
        	if(list == null)	return false;
        	boolean ans = false;
        	for(PP p : list) {
        		if(p.getKey() == key_2) {
        			ans = true;
        			break;
        		}
        	}
        	return ans;
        }
    }
    class PP{
    	public int key, val;
    	public PP(int key, int val) {
    		this.key = key;
    		this.val = val;
    	}
		public int getKey() {
			return key;
		}
		public void setKey(int key) {
			this.key = key;
		}
		public int getVal() {
			return val;
		}
		public void setVal(int val) {
			this.val = val;
		}
    }
}
0