結果

問題 No.386 貪欲な領主
ユーザー kuuso1kuuso1
提出日時 2016-07-01 23:37:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 602 ms / 2,000 ms
コード長 2,994 bytes
コンパイル時間 3,094 ms
コンパイル使用メモリ 111,048 KB
実行使用メモリ 51,400 KB
最終ジャッジ日時 2023-08-02 20:57:14
合計ジャッジ時間 7,724 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
20,796 KB
testcase_01 AC 61 ms
20,752 KB
testcase_02 AC 62 ms
20,876 KB
testcase_03 AC 62 ms
20,748 KB
testcase_04 AC 602 ms
46,500 KB
testcase_05 AC 581 ms
47,548 KB
testcase_06 AC 600 ms
47,284 KB
testcase_07 AC 66 ms
20,740 KB
testcase_08 AC 124 ms
28,776 KB
testcase_09 AC 69 ms
20,932 KB
testcase_10 AC 61 ms
20,720 KB
testcase_11 AC 62 ms
20,912 KB
testcase_12 AC 62 ms
20,716 KB
testcase_13 AC 72 ms
24,900 KB
testcase_14 AC 571 ms
51,400 KB
testcase_15 AC 498 ms
48,520 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		long[] sum = new long[N];
		for(int i=0;i<N;i++)sum[i] = U[i];
		Queue<int> Q = new Queue<int>();
		Q.Enqueue(root);
		
		while(Q.Count>0){
			var now = Q.Dequeue();
			foreach(var nxt in E[now]){
				if(nxt == parent[0][now])continue;
				sum[nxt] += sum[now];
				Q.Enqueue(nxt);
			}
		}
		
		long ans = 0;
		int from,to;
		long w;
		for(;M>0;M--){
			var d = ria();
			from = d[0];
			to = d[1];
			w = (long) d[2];
			
			var p = lca(from,to);
			long weight = w * (sum[from] + sum[to] - 2*sum[p] + U[p]);
			ans += weight;
		}
		Console.WriteLine(ans);
	}

	public Sol(){
		N=ri();
		E=new List<int>[N];
		for(int i=0;i<N;i++)E[i]=new List<int>();
		for(int i=0;i<N-1;i++){
			var d=ria();
			E[d[0]].Add(d[1]);
			E[d[1]].Add(d[0]);
		}
		U = new long[N];
		for(int i=0;i<N;i++){
			U[i] = rl();
		}
		LCAInit();
		M =ri();
	}
	
	int M;
	long[] U;
	int N;
	List<int>[] E;
	int[][] parent;
	int[] depth;
	static int lmax=24;
	int root;

	int lca(int u,int v){
		if(depth[u]>depth[v])return lca(v,u);
		for(int i=0;i<lmax;i++){
			if( (((depth[v]-depth[u])>>i)&1) >0 ){
				v=parent[i][v];
			}
		}
		if(u==v)return u;
		for(int i=lmax-1;i>=0;i--){
			if(parent[i][u]!=parent[i][v]){
				u=parent[i][u];
				v=parent[i][v];
			}
		}
		return parent[0][u];
	}
	
	void LCAInit(){
		parent=new int[lmax][];
		for(int i=0;i<lmax;i++){
			parent[i]=new int[N];
		}
		depth=new int[N];
		
		root=0;
		//dfs(root,-1,0);//深さを数えるdfs
		bfs(root,0);//深さを数えるbfs
		
		for(int i=0;i<lmax-1;i++){
			for(int j=0;j<N;j++){
				if(parent[i][j]<0){
					parent[i+1][j]=-1;
				}else{
					parent[i+1][j]=parent[i][parent[i][j]];
				}
			}
		}
		
	}
	
	void dfs(int now,int par,int dep){
		parent[0][now]=par;
		depth[now]=dep;
		foreach(int v in E[now]){
			if(v!=par){
				dfs(v,now,dep+1);
			}
		}
	}
	
	void bfs(int strt,int dep_strt){
		Queue<int> Q=new Queue<int>();
		parent[0][strt]=-1;depth[strt]=0;
		Q.Enqueue(strt);
		while(Q.Count>0){
			int now=Q.Dequeue();
			int par=parent[0][now];
			foreach(int v in E[now]){
				if(v!=par){
					parent[0][v]=now;depth[v]=depth[now]+1;
					Q.Enqueue(v);
				}
			}
		}
	}
	
	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}
0