結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー keymoonkeymoon
提出日時 2020-01-31 21:30:40
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 2,115 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 114,372 KB
実行使用メモリ 31,216 KB
最終ジャッジ日時 2023-10-17 08:46:17
合計ジャッジ時間 3,676 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
25,396 KB
testcase_01 AC 30 ms
25,396 KB
testcase_02 AC 29 ms
25,396 KB
testcase_03 AC 30 ms
25,396 KB
testcase_04 AC 36 ms
25,596 KB
testcase_05 AC 36 ms
25,596 KB
testcase_06 AC 30 ms
25,396 KB
testcase_07 AC 30 ms
25,396 KB
testcase_08 AC 30 ms
25,396 KB
testcase_09 AC 30 ms
25,396 KB
testcase_10 AC 36 ms
25,604 KB
testcase_11 AC 36 ms
25,600 KB
testcase_12 AC 37 ms
25,592 KB
testcase_13 AC 43 ms
27,492 KB
testcase_14 AC 40 ms
27,444 KB
testcase_15 AC 40 ms
27,444 KB
testcase_16 AC 43 ms
27,492 KB
testcase_17 AC 49 ms
27,640 KB
testcase_18 AC 63 ms
29,540 KB
testcase_19 AC 76 ms
29,688 KB
testcase_20 AC 80 ms
29,492 KB
testcase_21 AC 111 ms
29,560 KB
testcase_22 AC 133 ms
29,808 KB
testcase_23 AC 138 ms
29,856 KB
testcase_24 AC 188 ms
31,216 KB
testcase_25 AC 137 ms
29,856 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()
    {
        int n = int.Parse(Console.ReadLine());
        UnionFind uf = new UnionFind(n);
        int[] degs = new int[n];
        for (int i = 0; i < n - 1; i++)
        {
            var st = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            uf.TryUnite(st[0], st[1]);
            degs[st[0]]++;
            degs[st[1]]++;
        }
        Console.WriteLine(
            uf.GroupCount == 1 ||
            (uf.GroupCount == 2 && uf.AllRepresents.Min(x => uf.GetSize(x)) == 1 && degs.OrderBy(x => x).Skip(1).All(x => x == 2))  ? "Bob" : "Alice");
    }
}


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