結果

問題 No.386 貪欲な領主
ユーザー 14番14番
提出日時 2016-07-12 02:49:03
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 4,230 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 109,824 KB
実行使用メモリ 355,932 KB
最終ジャッジ日時 2024-04-22 15:05:41
合計ジャッジ時間 4,717 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
24,704 KB
testcase_01 AC 37 ms
19,200 KB
testcase_02 AC 38 ms
19,584 KB
testcase_03 AC 37 ms
19,584 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Generic;
using System.Text;
using System.Linq;



class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int muraCount = int.Parse(Reader.ReadLine());
        this.MuraList = new TreeNode[muraCount];
        for(int i=0; i<muraCount; i++) {
            this.MuraList[i] = new TreeNode(i);
        }
        Dictionary<int, Dictionary<int, bool>> road = new Dictionary<int, Dictionary<int, bool>>();
        for(int i=0; i<muraCount-1; i++) {
            int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
            if(!road.ContainsKey(inpt[0])) {
                road.Add(inpt[0], new Dictionary<int, bool>());
            }
            road[inpt[0]].Add(inpt[1], true);
            if(!road.ContainsKey(inpt[1])) {
                road.Add(inpt[1], new Dictionary<int, bool>());
            }
            road[inpt[1]].Add(inpt[0], true);
        }

        for(int i=0; i<muraCount; i++) {
            int tax = int.Parse(Reader.ReadLine());
            this.MuraList[i].Tax = tax;
        }

        this.SetTree(0, road);

        int gyosyoCount = int.Parse(Reader.ReadLine());

        long ans = 0;
        for(int i=0; i<gyosyoCount; i++) {
            int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
            long tax = this.GetTax(inpt[0], inpt[1]);
            tax *= inpt[2];
            ans += tax;
        }

        Console.WriteLine(ans);
    }

    private Dictionary<int, Dictionary<int, int>> taxDic = new Dictionary<int, Dictionary<int, int>>();
    private int GetTax(int fromId, int toId) {
        if(!this.taxDic.ContainsKey(fromId)) {
            this.taxDic.Add(fromId, new Dictionary<int, int>());
        }
        if(!this.taxDic.ContainsKey(toId)) {
            this.taxDic.Add(toId, new Dictionary<int, int>());
        }
        if(this.taxDic[fromId].ContainsKey(toId)) {
            return this.taxDic[fromId][toId];
        }


        int ans = 0;

        if(fromId == toId) {
            ans = this.MuraList[fromId].Tax;
        } else {
            TreeNode fromMura = this.MuraList[fromId];
            TreeNode toMura = this.MuraList[toId];

            if(fromMura.Depth > toMura.Depth) {
                ans = this.GetTax(fromMura.Parent.NodeId, toId) + fromMura.Tax;
            } else {
                ans = this.GetTax(fromId, toMura.Parent.NodeId) + toMura.Tax;
            }
        }

        this.taxDic[fromId].Add(toId, ans);
        if(fromId != toId) {
            this.taxDic[toId].Add(fromId, ans);
        }
        return ans;
    }

    private void SetTree(int current, Dictionary<int, Dictionary<int, bool>> dic) {
        TreeNode node = this.MuraList[current];
        dic[current].Keys.ToList().ForEach(a=>{
            if(node.Parent == null || node.Parent.NodeId != a) {
                node.AddItem(this.MuraList[a]);
            }
        });
        node.Items.ForEach(a=>SetTree(a.NodeId, dic));
    }

    private TreeNode[] MuraList;

    public class TreeNode {
        public int NodeId = 0;

        public int Depth = 0;
        public TreeNode Parent;
        public List<TreeNode> Items = new List<TreeNode>();

        public int Tax = 0;

        public TreeNode AddItem(TreeNode newChild) {
            newChild.Parent = this;
            this.Items.Add(newChild);
            newChild.Depth = this.Depth + 1;
            return newChild;
        }
        public TreeNode(int id) {
            this.NodeId = id;
        }
    }


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


2
0 1
11
7
2
0 1 3
1 0 2



";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0