結果

問題 No.1103 Directed Length Sum
ユーザー 👑 terry_u16terry_u16
提出日時 2020-07-03 23:44:27
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,526 ms / 3,000 ms
コード長 12,357 bytes
コンパイル時間 1,359 ms
コンパイル使用メモリ 121,612 KB
実行使用メモリ 319,576 KB
最終ジャッジ日時 2023-10-17 06:28:03
合計ジャッジ時間 24,010 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
25,700 KB
testcase_01 AC 35 ms
25,700 KB
testcase_02 AC 2,526 ms
319,576 KB
testcase_03 AC 1,265 ms
101,596 KB
testcase_04 AC 1,334 ms
77,216 KB
testcase_05 AC 2,493 ms
121,332 KB
testcase_06 AC 828 ms
60,696 KB
testcase_07 AC 191 ms
36,824 KB
testcase_08 AC 288 ms
40,624 KB
testcase_09 AC 129 ms
34,084 KB
testcase_10 AC 402 ms
44,892 KB
testcase_11 AC 1,436 ms
81,596 KB
testcase_12 AC 844 ms
61,076 KB
testcase_13 AC 424 ms
48,296 KB
testcase_14 AC 102 ms
32,912 KB
testcase_15 AC 652 ms
54,200 KB
testcase_16 AC 1,691 ms
95,356 KB
testcase_17 AC 1,768 ms
96,108 KB
testcase_18 AC 400 ms
44,828 KB
testcase_19 AC 1,483 ms
82,840 KB
testcase_20 AC 148 ms
35,092 KB
testcase_21 AC 257 ms
39,688 KB
testcase_22 AC 1,198 ms
73,968 KB
testcase_23 AC 702 ms
55,896 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.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using YukicoderContest255.Extensions;
using YukicoderContest255.Questions;

namespace YukicoderContest255.Questions
{
    public class QuestionC : AtCoderQuestionBase
    {
        BasicGraph tree;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var nodes = inputStream.ReadInt();
            tree = new BasicGraph(nodes);
            var isRoot = Enumerable.Repeat(true, nodes).ToArray();

            for (int i = 0; i < nodes - 1; i++)
            {
                var (a, b) = inputStream.ReadValue<int, int>();
                a--;
                b--;
                tree.AddEdge(new BasicEdge(a, b));
                isRoot[b] = false;
            }

            var root = -1;
            for (int i = 0; i < isRoot.Length; i++)
            {
                if (isRoot[i])
                {
                    root = i;
                    break;
                }
            }

            yield return Dfs(root, 0, 0) % 1000000007;
        }

        long Dfs(int node, int parents, long sum)
        {
            sum += parents;
            long childrenSum = 0;
            foreach (var edge in tree[node])
            {
                childrenSum += Dfs(edge.To.Index, parents + 1, sum);
            }
            return sum + childrenSum;
        }

        public interface INode
        {
            public int Index { get; }
        }

        public interface IEdge<TNode> where TNode : INode
        {
            TNode From { get; }
            TNode To { get; }
        }

        public interface IWeightedEdge<TNode> : IEdge<TNode> where TNode : INode
        {
            long Weight { get; }
        }

        public interface IGraph<TNode, TEdge> where TEdge : IEdge<TNode> where TNode : INode
        {
            IEnumerable<TEdge> this[TNode node] { get; }
            IEnumerable<TEdge> Edges { get; }
            IEnumerable<TNode> Nodes { get; }
            int NodeCount { get; }
        }

        public interface IWeightedGraph<TNode, TEdge> : IGraph<TNode, TEdge> where TEdge : IWeightedEdge<TNode> where TNode : INode { }

        [StructLayout(LayoutKind.Auto)]
        public readonly struct BasicNode : INode, IEquatable<BasicNode>
        {
            public int Index { get; }

            public BasicNode(int index)
            {
                Index = index;
            }

            public override string ToString() => Index.ToString();
            public override bool Equals(object obj) => obj is BasicNode node && Equals(node);
            public bool Equals(BasicNode other) => Index == other.Index;
            public override int GetHashCode() => HashCode.Combine(Index);
            public static bool operator ==(BasicNode left, BasicNode right) => left.Equals(right);
            public static bool operator !=(BasicNode left, BasicNode right) => !(left == right);
            public static implicit operator BasicNode(int value) => new BasicNode(value);
        }

        [StructLayout(LayoutKind.Auto)]
        public readonly struct BasicEdge : IEdge<BasicNode>
        {
            public BasicNode From { get; }
            public BasicNode To { get; }

            public BasicEdge(int from, int to)
            {
                From = from;
                To = to;
            }

            public override string ToString() => $"{From}-->{To}";
        }

        [StructLayout(LayoutKind.Auto)]
        public readonly struct WeightedEdge : IWeightedEdge<BasicNode>
        {
            public BasicNode From { get; }
            public BasicNode To { get; }
            public long Weight { get; }

            public WeightedEdge(int from, int to) : this(from, to, 1) { }

            public WeightedEdge(int from, int to, long weight)
            {
                From = from;
                To = to;
                Weight = weight;
            }

            public override string ToString() => $"{From}--[{Weight}]-->{To}";
        }

        public class BasicGraph : IGraph<BasicNode, BasicEdge>
        {
            private readonly List<BasicEdge>[] _edges;
            public IEnumerable<BasicEdge> this[BasicNode node] => _edges[node.Index];
            public IEnumerable<BasicEdge> Edges => Nodes.SelectMany(node => this[node]);
            public IEnumerable<BasicNode> Nodes => Enumerable.Range(0, NodeCount).Select(i => new BasicNode(i));
            public int NodeCount { get; }

            public BasicGraph(int nodeCount) : this(nodeCount, Enumerable.Empty<BasicEdge>()) { }

            public BasicGraph(int nodeCount, IEnumerable<BasicEdge> edges)
            {
                _edges = Enumerable.Repeat(0, nodeCount).Select(_ => new List<BasicEdge>()).ToArray();
                NodeCount = nodeCount;
                foreach (var edge in edges)
                {
                    AddEdge(edge);
                }
            }

            public BasicGraph(int nodeCount, IEnumerable<IEnumerable<int>> distances)
            {
                _edges = new List<BasicEdge>[nodeCount];

                int i = 0;
                foreach (var row in distances)
                {
                    _edges[i] = new List<BasicEdge>(nodeCount);
                    int j = 0;
                    foreach (var distance in row)
                    {
                        if (distance == 1)
                        {
                            _edges[i].Add(new BasicEdge(i, j++));
                        }
                    }
                    i++;
                }
            }

            public void AddEdge(BasicEdge edge) => _edges[edge.From.Index].Add(edge);
        }

    }
}

namespace YukicoderContest255
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionC();
            var answers = question.Solve(Console.In);

            var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(writer);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            Console.Out.Flush();
        }
    }
}

#region Base Class

namespace YukicoderContest255.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }
}

#endregion

#region Extensions

namespace YukicoderContest255.Extensions
{
    public static class TextReaderExtensions
    {
        public static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        public static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        public static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        public static string ReadString(this TextReader reader) => reader.ReadLine();

        public static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        public static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        public static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        public static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);

        // Supports primitive type only.
        public static T1 ReadValue<T1>(this TextReader reader) => (T1)Convert.ChangeType(reader.ReadLine(), typeof(T1));

        public static (T1, T2) ReadValue<T1, T2>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            return (v1, v2);
        }

        public static (T1, T2, T3) ReadValue<T1, T2, T3>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            return (v1, v2, v3);
        }

        public static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            return (v1, v2, v3, v4);
        }

        public static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            return (v1, v2, v3, v4, v5);
        }

        public static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            return (v1, v2, v3, v4, v5, v6);
        }

        public static (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            return (v1, v2, v3, v4, v5, v6, v7);
        }

        public static (T1, T2, T3, T4, T5, T6, T7, T8) ReadValue<T1, T2, T3, T4, T5, T6, T7, T8>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            var v8 = (T8)Convert.ChangeType(inputs[7], typeof(T8));
            return (v1, v2, v3, v4, v5, v6, v7, v8);
        }
    }
}

#endregion
0