結果

問題 No.1605 Matrix Shape
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-12 23:22:54
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 2,386 bytes
コンパイル時間 7,115 ms
コンパイル使用メモリ 158,660 KB
実行使用メモリ 206,060 KB
最終ジャッジ日時 2024-02-12 23:23:10
合計ジャッジ時間 14,563 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
34,084 KB
testcase_01 AC 83 ms
34,084 KB
testcase_02 AC 81 ms
33,956 KB
testcase_03 AC 82 ms
34,084 KB
testcase_04 AC 81 ms
34,084 KB
testcase_05 AC 84 ms
33,956 KB
testcase_06 AC 82 ms
34,084 KB
testcase_07 AC 82 ms
34,084 KB
testcase_08 AC 83 ms
34,084 KB
testcase_09 AC 83 ms
34,084 KB
testcase_10 AC 82 ms
34,084 KB
testcase_11 AC 81 ms
34,084 KB
testcase_12 AC 83 ms
34,084 KB
testcase_13 AC 82 ms
34,084 KB
testcase_14 AC 82 ms
34,084 KB
testcase_15 AC 82 ms
34,084 KB
testcase_16 AC 82 ms
34,084 KB
testcase_17 AC 81 ms
34,084 KB
testcase_18 AC 81 ms
34,084 KB
testcase_19 AC 289 ms
75,732 KB
testcase_20 AC 272 ms
69,784 KB
testcase_21 AC 254 ms
66,072 KB
testcase_22 AC 265 ms
66,712 KB
testcase_23 AC 292 ms
75,736 KB
testcase_24 AC 262 ms
66,840 KB
testcase_25 AC 251 ms
65,652 KB
testcase_26 AC 249 ms
65,664 KB
testcase_27 AC 236 ms
65,664 KB
testcase_28 AC 241 ms
65,788 KB
testcase_29 AC 251 ms
65,784 KB
testcase_30 AC 284 ms
69,780 KB
testcase_31 AC 249 ms
66,072 KB
testcase_32 AC 265 ms
67,344 KB
testcase_33 AC 246 ms
65,680 KB
testcase_34 AC 219 ms
63,004 KB
testcase_35 AC 210 ms
63,724 KB
testcase_36 AC 166 ms
206,060 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (101 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var map = NArr(n);
        var cin = new int[200001];
        var cout = new int[200001];
        var uf = new UnionFindTree(200001);
        foreach (var arr in map) if (arr[0] != arr[1])
        {
            ++cin[arr[0]];
            ++cout[arr[1]];
            uf.Unite(arr[0], arr[1]);
        }
        var scount = 0;
        for (var i = 0; i < cin.Length; ++i)
        {
            if (cin[i] != cout[i])
            {
                scount += Math.Max(0, cin[i] - cout[i]);
            }
        }
        var roots = new HashSet<int>();
        foreach (var arr in map) roots.Add(uf.GetRoot(arr[0]));
        if (roots.Count > 1)
        {
            WriteLine(0);
        }
        else if (scount == 0)
        {
            var set = new HashSet<int>(map.Select(mi => mi[0]));
            WriteLine(set.Count);
        }
        else if (scount == 1)
        {
            WriteLine(1);
        }
        else
        {
            WriteLine(0);
        }
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0