結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
keymoon
|
| 提出日時 | 2020-01-31 21:30:40 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 187 ms / 2,000 ms |
| コード長 | 2,115 bytes |
| コンパイル時間 | 1,246 ms |
| コンパイル使用メモリ | 117,200 KB |
| 実行使用メモリ | 24,960 KB |
| 最終ジャッジ日時 | 2024-09-17 07:14:35 |
| 合計ジャッジ時間 | 3,503 ms |
|
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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;
}
}
keymoon