結果

問題 No.872 All Tree Path
ユーザー k_6101k_6101
提出日時 2019-12-07 15:06:11
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,470 bytes
コンパイル時間 3,646 ms
コンパイル使用メモリ 77,224 KB
実行使用メモリ 146,596 KB
最終ジャッジ日時 2023-08-26 18:56:40
合計ジャッジ時間 20,376 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 115 ms
55,456 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 380 ms
63,020 KB
testcase_09 AC 377 ms
63,228 KB
testcase_10 AC 372 ms
63,216 KB
testcase_11 AC 380 ms
62,900 KB
testcase_12 AC 372 ms
62,640 KB
testcase_13 AC 114 ms
55,772 KB
testcase_14 AC 114 ms
55,692 KB
testcase_15 AC 122 ms
55,528 KB
testcase_16 AC 122 ms
56,252 KB
testcase_17 AC 121 ms
55,820 KB
testcase_18 AC 120 ms
55,756 KB
testcase_19 AC 122 ms
55,544 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 * (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