結果

問題 No.1266 7 Colors
ユーザー terry_u16terry_u16
提出日時 2020-10-23 22:40:06
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 599 ms / 3,000 ms
コード長 12,703 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 124,696 KB
実行使用メモリ 53,700 KB
最終ジャッジ日時 2024-07-21 11:33:22
合計ジャッジ時間 13,232 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
25,856 KB
testcase_01 AC 37 ms
25,648 KB
testcase_02 AC 38 ms
27,644 KB
testcase_03 AC 414 ms
33,860 KB
testcase_04 AC 546 ms
39,708 KB
testcase_05 AC 437 ms
35,080 KB
testcase_06 AC 571 ms
43,092 KB
testcase_07 AC 583 ms
43,028 KB
testcase_08 AC 568 ms
44,404 KB
testcase_09 AC 537 ms
41,424 KB
testcase_10 AC 524 ms
38,624 KB
testcase_11 AC 488 ms
36,220 KB
testcase_12 AC 486 ms
38,768 KB
testcase_13 AC 514 ms
39,488 KB
testcase_14 AC 445 ms
34,808 KB
testcase_15 AC 599 ms
48,752 KB
testcase_16 AC 502 ms
37,304 KB
testcase_17 AC 571 ms
46,504 KB
testcase_18 AC 431 ms
48,068 KB
testcase_19 AC 432 ms
51,912 KB
testcase_20 AC 447 ms
53,700 KB
testcase_21 AC 272 ms
30,768 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 YukicoderContest271.Extensions;
using YukicoderContest271.Questions;

namespace YukicoderContest271.Questions
{
    public class QuestionC : AtCoderQuestionBase
    {
        const int MaxColor = 7;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var (cities, roads, queries) = inputStream.ReadValue<int, int, int>();

            var n = cities * MaxColor;
            var colors = new bool[cities][];
            var uf = new UnionFind(n);

            for (int i = 0; i < colors.Length; i++)
            {
                colors[i] = inputStream.ReadLine().Select(c => c == '1').ToArray();

                for (int c = 0; c < MaxColor; c++)
                {
                    if (colors[i][c] && colors[i][(c + 1) % MaxColor])
                    {
                        uf.Unite(GetIndex(i, c), GetIndex(i, (c + 1) % MaxColor));
                    }
                }
            }

            var edgeList = Enumerable.Repeat(0, cities).Select(_ => new List<int>()).ToArray();

            for (int i = 0; i < roads; i++)
            {
                var (u, v) = inputStream.ReadValue<int, int>();
                u--;
                v--;
                edgeList[u].Add(v);
                edgeList[v].Add(u);

                for (int c = 0; c < MaxColor; c++)
                {
                    if (colors[u][c] && colors[v][c])
                    {
                        uf.Unite(GetIndex(u, c), GetIndex(v, c));
                    }
                }
            }

            for (int q = 0; q < queries; q++)
            {
                var (type, city, color) = inputStream.ReadValue<int, int, int>();
                city--;
                color--;

                if (type == 1)
                {
                    colors[city][color] = true;

                    for (int c = 0; c < MaxColor; c++)
                    {
                        if (colors[city][c] && colors[city][(c + 1) % MaxColor])
                        {
                            uf.Unite(GetIndex(city, c), GetIndex(city, (c + 1) % MaxColor));
                        }
                    }

                    foreach (var next in edgeList[city])
                    {
                        if (colors[next][color])
                        {
                            uf.Unite(GetIndex(city, color), GetIndex(next, color));
                        }
                    }
                }
                else
                {
                    yield return uf.GetGroupSizeOf(GetIndex(city, 0));
                }
            }
        }

        int GetIndex(int city, int color) => city * MaxColor + color;

        public class UnionFind
        {
            private int[] _parentsOrSizes;
            public int Count => _parentsOrSizes.Length;
            public int Groups { get; private set; }

            public UnionFind(int count)
            {
                _parentsOrSizes = new int[count];
                _parentsOrSizes.AsSpan().Fill(-1);
                Groups = _parentsOrSizes.Length;
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public bool Unite(int a, int b)
            {
                var x = FindRoot(a);
                var y = FindRoot(b);

                if (x == y)
                {
                    return false;
                }
                else
                {
                    if (-_parentsOrSizes[x] < _parentsOrSizes[y])
                    {
                        (x, y) = (y, x);
                    }

                    _parentsOrSizes[x] += _parentsOrSizes[y];
                    _parentsOrSizes[y] = x;
                    Groups--;
                    return true;
                }
            }

            public bool IsInSameGroup(int a, int b) => FindRoot(a) == FindRoot(b);

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public int FindRoot(int index)
            {
                if (unchecked((uint)index >= (uint)_parentsOrSizes.Length))
                {
                    ThrowArgumentException();
                }

                return FindRecursive(index);

                int FindRecursive(int index)
                {
                    if (_parentsOrSizes[index] < 0)
                    {
                        return index;
                    }
                    else
                    {
                        return _parentsOrSizes[index] = FindRecursive(_parentsOrSizes[index]);
                    }
                }
            }

            public int GetGroupSizeOf(int index) => -_parentsOrSizes[FindRoot(index)];

            public int[][] GetAllGroups()
            {
                var resultIndices = new int[_parentsOrSizes.Length];
                var index = 0;
                var results = new int[Groups][];

                for (int i = 0; i < _parentsOrSizes.Length; i++)
                {
                    if (_parentsOrSizes[i] < 0)
                    {
                        results[index] = new int[-_parentsOrSizes[i]];
                        resultIndices[i] = index++;
                    }
                }

                var counts = new int[results.Length];

                for (int i = 0; i < _parentsOrSizes.Length; i++)
                {
                    var group = resultIndices[FindRoot(i)];
                    results[group][counts[group]++] = i;
                }

                return results;
            }

            private void ThrowArgumentException() => throw new ArgumentException();
        }
    }
}

namespace YukicoderContest271
{
    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 YukicoderContest271.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 YukicoderContest271.Extensions
{
    public static class StringExtensions
    {
        public static string Join<T>(this IEnumerable<T> source) => string.Concat(source);
        public static string Join<T>(this IEnumerable<T> source, char separator) => string.Join(separator, source);
        public static string Join<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source);
    }

    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