結果

問題 No.1451 集団登校
ユーザー 👑 kakel-sankakel-san
提出日時 2024-05-26 00:53:32
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 3,179 bytes
コンパイル時間 11,249 ms
コンパイル使用メモリ 167,632 KB
実行使用メモリ 211,560 KB
最終ジャッジ日時 2024-05-26 00:53:49
合計ジャッジ時間 12,232 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
31,232 KB
testcase_01 AC 55 ms
31,360 KB
testcase_02 AC 56 ms
31,092 KB
testcase_03 AC 54 ms
31,092 KB
testcase_04 AC 55 ms
31,104 KB
testcase_05 AC 54 ms
31,232 KB
testcase_06 AC 56 ms
31,052 KB
testcase_07 AC 58 ms
31,488 KB
testcase_08 AC 171 ms
69,528 KB
testcase_09 AC 64 ms
31,232 KB
testcase_10 AC 103 ms
50,364 KB
testcase_11 AC 75 ms
41,128 KB
testcase_12 AC 102 ms
50,224 KB
testcase_13 AC 150 ms
65,324 KB
testcase_14 AC 194 ms
69,200 KB
testcase_15 AC 103 ms
48,384 KB
testcase_16 AC 139 ms
62,820 KB
testcase_17 AC 164 ms
60,920 KB
testcase_18 AC 84 ms
41,344 KB
testcase_19 AC 63 ms
35,968 KB
testcase_20 AC 187 ms
67,092 KB
testcase_21 AC 101 ms
48,512 KB
testcase_22 AC 82 ms
41,088 KB
testcase_23 AC 70 ms
35,840 KB
testcase_24 AC 100 ms
48,980 KB
testcase_25 AC 107 ms
48,884 KB
testcase_26 AC 138 ms
62,220 KB
testcase_27 AC 191 ms
67,300 KB
testcase_28 AC 84 ms
42,752 KB
testcase_29 AC 107 ms
211,560 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (93 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.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 half = 500_000_004;
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NArr(m);
        var uf = new UnionFindTree(n);
        var tree = new List<(int to, int len)>[n + m];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int len)>();
        var addcnt = 0;
        var adds = new int[n];
        for (var i = 0; i < n; ++i) adds[i] = i;
        var refs = new int[n + m];
        for (var i = 0; i < m; ++i)
        {
            var a = map[i][0] - 1;
            var b = map[i][1] - 1;
            if (!uf.IsSameTree(a, b))
            {
                var pa = adds[uf.GetRoot(a)];
                var pb = adds[uf.GetRoot(b)];
                var nc = addcnt + n;
                var la = 0;
                var lb = 0;
                if (uf.GetSize(a) > uf.GetSize(b)) la = 1;
                else if (uf.GetSize(b) > uf.GetSize(a)) lb = 1;
                else
                {
                    la = half;
                    lb = half;
                }
                tree[nc].Add((pa, la));
                tree[nc].Add((pb, lb));
                uf.Unite(a, b);
                adds[uf.GetRoot(a)] = nc;
                ++addcnt;
                ++refs[pa];
                ++refs[pb];
            }
        }
        var len = new long[n];
        for (var i = 0; i < tree.Length; ++i)
        {
            if (refs[i] == 0) DFS(i, 1, tree, len);
        }
        WriteLine(string.Join("\n", len));
    }
    static int mod = 1_000_000_007;
    static void DFS(int cur, long val, List<(int to, int len)>[] tree, long[] len)
    {
        if (cur < len.Length) len[cur] = val;
        foreach (var next in tree[cur])
        {
            DFS(next.to, val * next.len % mod, tree, len);
        }
    }
    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