結果

問題 No.386 貪欲な領主
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2016-07-02 17:59:13
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 10,343 bytes
コンパイル時間 3,138 ms
コンパイル使用メモリ 111,872 KB
実行使用メモリ 65,056 KB
最終ジャッジ日時 2024-10-12 19:44:31
合計ジャッジ時間 3,663 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
18,816 KB
testcase_01 AC 29 ms
18,304 KB
testcase_02 AC 29 ms
18,688 KB
testcase_03 AC 28 ms
18,688 KB
testcase_04 AC 231 ms
63,864 KB
testcase_05 AC 315 ms
65,056 KB
testcase_06 AC 309 ms
64,600 KB
testcase_07 AC 29 ms
18,944 KB
testcase_08 AC 46 ms
22,528 KB
testcase_09 AC 30 ms
19,072 KB
testcase_10 AC 28 ms
18,560 KB
testcase_11 AC 28 ms
18,688 KB
testcase_12 AC 29 ms
18,688 KB
testcase_13 AC 33 ms
19,412 KB
testcase_14 AC 313 ms
64,592 KB
testcase_15 AC 218 ms
63,848 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.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
//using System.Numerics;
using Number = System.Int64;
namespace Program
{
    public class Solver
    {
        public void Solve()
        {
            var n = sc.Integer();
            var G = Enumerate(n, x => new List<int>());
            var tree = new TreeGraph(n);
            for (int i = 0; i < n - 1; i++)
            {
                var f = sc.Integer();
                var t = sc.Integer();
                G[f].Add(t);
                G[t].Add(f);
                tree.AddEdge(f, t);
            }
            tree.Build(0);
            var v = sc.Long(n);
            var sum = v.ToArray();
            {
                var q = new Queue<int>();
                var used = new bool[n];
                q.Enqueue(0);
                while (q.Count > 0)
                {
                    var p = q.Dequeue();
                    used[p] = true;
                    foreach (var to in G[p])
                    {
                        if (used[to]) continue;
                        q.Enqueue(to);
                        sum[to] += sum[p];
                    }
                }
            }
            long ans = 0;
            var m = sc.Integer();
            for (int i = 0; i < m; i++)
            {
                var f = sc.Integer();
                var t = sc.Integer();
                var c = sc.Integer();
                var lca = tree.GetLCA(f, t);
                ans += c * (sum[f] + sum[t] - 2 * sum[lca] + v[lca]);

            }
            IO.Printer.Out.WriteLine(ans);

        }
        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }


    }
}
#region main
static class Ex
{
    static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main()
    {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer: StreamWriter
    {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
#endregion
#region Treegraph
public class TreeGraph
{
    public int n;
    public List<Edge>[] G;
    public int[] par;
    public int[] d;
    public int[] ord, ordl, ordr;
    public TreeGraph(int N)
    {
        n = N;
        G = new List<Edge>[N];
        for (int i = 0; i < N; i++)
            G[i] = new List<Edge>();
        par = new int[N];
        d = new int[N];
        ord = new int[2 * N - 1];
        ordl = new int[N];
        ordr = new int[N];
    }
    public void AddEdge(int f, int t)
    {
        G[f].Add(new Edge(f, t));
        G[t].Add(new Edge(t, f));
    }
    void Walk(int root)
    {
        var stack = new Stack<int>();
        var iter = new int[n];
        stack.Push(root);
        par[root] = -1;
        d[root] = 0;
        var ptr = 0;

        while (stack.Any())
        {
            var p = stack.Peek();
            ord[ptr] = p;
            ptr++;
            if (iter[p] < G[p].Count && G[p][iter[p]].to == par[p]) iter[p]++;
            if (iter[p] == G[p].Count)
            {
                stack.Pop();
                ordr[p] = ptr;
            }
            else
            {
                var to = G[p][iter[p]].to;
                iter[p]++;
                stack.Push(to);
                par[to] = p;
                d[to] = d[p] + 1;
                ordl[to] = ptr;
            }
        }
    }
    sparseTable rmq;
    void BuildLCA()
    {
        var dep = new int[2 * n - 1];
        for (int i = 0; i < 2 * n - 1; i++)
            dep[i] = d[ord[i]];
        rmq = new sparseTable(dep);
    }
    public void Build(int root)
    {
        Walk(root);
        BuildLCA();
    }

    public int GetLCA(int u, int v)
    {
        //ordl: u<v
        if (ordl[u] > ordl[v]) { var tmp = u; u = v; v = tmp; }
        return ord[rmq.Query(ordl[u], ordl[v])];
    }

    /// <summary>
    /// 木を縮約してLCAを作って返す
    /// </summary>
    static public TreeGraph Compress(int[] v, TreeGraph G, List<int> ord = null)
    {
        if (ord == null) ord = new List<int>();
        for (int i = 0; i < v.Length; i++)
            ord.Add(G.ordl[v[i]]);
        ord.Sort();
        for (int i = 0; i < v.Length - 1; i++)
            ord.Add(G.ordl[G.GetLCA(G.ord[ord[i]], G.ord[ord[i + 1]])]);

        {
            var next = ord.Distinct().ToArray();
            ord.Clear();
            foreach (var x in next) ord.Add(x);
        }
        ord.Sort();
        var n = ord.Count;
        var ret = new TreeGraph(n);
        var stack = new Stack<int>();
        stack.Push(0);
        var ptr = 1;
        while (ptr < n)
        {
            var cur = stack.Peek();
            var a = G.ord[ord[cur]];
            if (G.ordr[a] > ord[ptr])
            {
                ret.AddEdge(cur, ptr);
                stack.Push(ptr++);
            }
            else stack.Pop();
        }

        return ret;
    }
    #region SparseTable
    class sparseTable
    {
        int n;
        int[] A;
        int[] id;
        public sparseTable(int[] a)
        {
            var k = 1;
            n = a.Length;
            for (int i = 1; i < n; i <<= 1) k++;
            A = a;
            id = new int[n * k];
            for (int i = 0; i < n; i++)
                id[i] = i;
            var d = 0;
            for (int i = 1; i < n; i <<= 1)
            {
                for (int j = 0; j < n; j++)
                    id[d + n + j] = id[d + j];
                d += n;
                for (int j = 0; j < n - i; j++)
                    if (A[id[d + j]] > A[id[d + j + i]]) id[d + j] = id[d + j + i];
            }
        }
        /// <summary>
        /// value of [l,r)
        /// </summary>
        public int Query(int l, int r)
        {
            int z = r - l, k = 0, e = 1, s;
            s = ((z & 0xffff0000) != 0 ? 1 : 0) << 4; z >>= s; e <<= s; k |= s;
            s = ((z & 0x0000ff00) != 0 ? 1 : 0) << 3; z >>= s; e <<= s; k |= s;
            s = ((z & 0x000000f0) != 0 ? 1 : 0) << 2; z >>= s; e <<= s; k |= s;
            s = ((z & 0x0000000c) != 0 ? 1 : 0) << 1; z >>= s; e <<= s; k |= s;
            s = ((z & 0x00000002) != 0 ? 1 : 0) << 0; z >>= s; e <<= s; k |= s;
            var x = id[l + n * k];
            var y = id[r - e + 1 + (n * k)];

            if (A[x] <= A[y])
                return x;
            else return y;
        }
    }
    #endregion
}
#endregion
#region TreeEdge
public struct Edge
{
    public int from, to;
    public Edge(int f, int t)
    {
        from = f; to = t;
    }
    public override string ToString()
    {
        return string.Format("{0}->{1}", from, to);
    }
}
#endregion
0