結果

問題 No.1266 7 Colors
ユーザー keymoonkeymoon
提出日時 2020-10-23 22:19:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 832 ms / 3,000 ms
コード長 3,440 bytes
コンパイル時間 4,281 ms
コンパイル使用メモリ 112,748 KB
実行使用メモリ 49,428 KB
最終ジャッジ日時 2023-09-28 16:11:36
合計ジャッジ時間 18,698 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
21,896 KB
testcase_01 AC 68 ms
21,832 KB
testcase_02 AC 69 ms
23,884 KB
testcase_03 AC 683 ms
33,228 KB
testcase_04 AC 774 ms
40,408 KB
testcase_05 AC 675 ms
34,012 KB
testcase_06 AC 800 ms
45,272 KB
testcase_07 AC 812 ms
46,664 KB
testcase_08 AC 789 ms
40,776 KB
testcase_09 AC 741 ms
37,620 KB
testcase_10 AC 740 ms
38,548 KB
testcase_11 AC 697 ms
33,320 KB
testcase_12 AC 727 ms
36,416 KB
testcase_13 AC 729 ms
37,504 KB
testcase_14 AC 691 ms
33,976 KB
testcase_15 AC 832 ms
45,068 KB
testcase_16 AC 728 ms
34,776 KB
testcase_17 AC 808 ms
46,288 KB
testcase_18 AC 805 ms
49,428 KB
testcase_19 AC 434 ms
46,316 KB
testcase_20 AC 434 ms
46,328 KB
testcase_21 AC 655 ms
29,496 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.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
public static class P
{
    public static void Main()
    {
        var nmq = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n = nmq[0];
        var m = nmq[1];
        var q = nmq[2];
        UnionFind uf = new UnionFind(n * 7);
        bool[][] colors = new bool[n][];
        for (int i = 0; i < n; i++)
        {
            var s = Console.ReadLine();
            colors[i] = s.Select(x => x == '1').ToArray();
            for (int j = 0; j < 7; j++)
            {
                var next = (j + 1) % 7;
                if (colors[i][j] && colors[i][next]) uf.TryUnite(j * n + i, next * n + i);
            }
        }
        List<int>[] edges = Enumerable.Repeat(0, n).Select(_ => new List<int>()).ToArray();
        for (int i = 0; i < m; i++)
        {
            var uv = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int u = uv[0] - 1, v = uv[1] - 1;
            edges[u].Add(v);
            edges[v].Add(u);
            for (int j = 0; j < 7; j++)
            {
                if (colors[u][j] && colors[v][j]) uf.TryUnite(j * n + v, j * n + u);
            }
        }
        for (int i = 0; i < q; i++)
        {
            var txy = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var t = txy[0];
            var x = txy[1] - 1;
            var y = txy[2] - 1;
            if (t == 1)
            {
                colors[x][y] = true;
                var prev = (y + 6) % 7;
                var next = (y + 1) % 7;
                if (colors[x][prev] && colors[x][y]) uf.TryUnite(prev * n + x, y * n + x);
                if (colors[x][y] && colors[x][next]) uf.TryUnite(y * n + x, next * n + x);
                foreach (var adj in edges[x])
                {
                    if (colors[adj][y]) uf.TryUnite(y * n + x, y * n + adj);
                }
            }
            else
            {
                Console.WriteLine(uf.GetSize(x));
            }
        }
    }
}

class UnionFind
{
    public int Size { get; private set; }
    public int GroupCount { get; private set; }
    public IEnumerable<int> AllRepresents => Parent.Where((x, y) => x == y);
    int[] Sizes;
    int[] Parent;
    public UnionFind(int count)
    {
        Size = count;
        GroupCount = count;
        Parent = new int[count];
        Sizes = new int[count];
        for (int i = 0; i < count; i++) Sizes[Parent[i] = i] = 1;
    }
    public bool TryUnite(int x, int y)
    {
        int xp = Find(x);
        int yp = Find(y);
        if (yp == xp) return false;
        if (Sizes[xp] < Sizes[yp]) { var tmp = xp; xp = yp; yp = tmp; }
        GroupCount--;
        Parent[yp] = xp;
        Sizes[xp] += Sizes[yp];
        return true;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int GetSize(int x) => Sizes[Find(x)];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int Find(int x)
    {
        while (x != Parent[x]) x = (Parent[x] = Parent[Parent[x]]);
        return x;
    }
}
0