結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー Yoichiro NishimuraYoichiro Nishimura
提出日時 2020-02-06 10:57:39
言語 PHP
(8.3.4)
結果
WA  
実行時間 -
コード長 1,692 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 32,604 KB
実行使用メモリ 42,684 KB
最終ジャッジ日時 2023-10-25 10:33:57
合計ジャッジ時間 3,440 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
32,536 KB
testcase_01 AC 44 ms
32,536 KB
testcase_02 AC 43 ms
32,536 KB
testcase_03 AC 44 ms
32,536 KB
testcase_04 WA -
testcase_05 AC 44 ms
32,536 KB
testcase_06 AC 44 ms
32,536 KB
testcase_07 AC 45 ms
32,536 KB
testcase_08 AC 44 ms
32,536 KB
testcase_09 AC 44 ms
32,536 KB
testcase_10 AC 44 ms
32,536 KB
testcase_11 AC 43 ms
32,536 KB
testcase_12 AC 44 ms
32,536 KB
testcase_13 AC 53 ms
32,536 KB
testcase_14 AC 52 ms
32,536 KB
testcase_15 AC 54 ms
32,536 KB
testcase_16 AC 54 ms
32,536 KB
testcase_17 AC 54 ms
32,536 KB
testcase_18 AC 74 ms
34,492 KB
testcase_19 WA -
testcase_20 AC 102 ms
38,588 KB
testcase_21 AC 129 ms
42,684 KB
testcase_22 AC 168 ms
42,684 KB
testcase_23 AC 168 ms
42,684 KB
testcase_24 AC 167 ms
42,684 KB
testcase_25 AC 167 ms
42,684 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = intval(fgets(STDIN));
$tree = new UFT($n);
for($i = 0; $i < $n; $i++){
  fscanf(STDIN, "%d%d", $a, $b);
  $tree->unite($a+1, $b+1);
}
for($i = 1; $i <= $n-1; $i++){
  $roots[] = $tree->getRoot($i);
}
if(max($roots) == min($roots)){
  echo "Bob\n";
}else{
  echo "Alice\n";
}


class UFT{
    public $parent;
    public $size;
    public $rank;
    public function __construct($size){
        for($i = 1; $i <= $size; $i++){
            $this->size[$i] = 1;
            $this->rank[$i] = 1;
            $this->parent[$i] = 0;
        }
    }
    public function dump(){
        o("--------------");
        o($this->parent);
        o($this->size);
        o($this->rank);
    }
    public function getRoot($i){
        if($this->parent[$i] == 0){
            return $i;
        }else{
            return $this->getRoot($this->parent[$i]);
        }
    }
    public function unite($i, $j){
        $rootI = $this->getRoot($j);
        $rootJ = $this->getRoot($i);
        if($rootJ == $rootI)return false;//元から同じグループ
        if($this->rank[$rootI] > $this->rank[$rootJ])list($rootI, $rootJ) = [$rootJ, $rootI];//Rank(J)>Rank(I)に揃えておく
        $this->parent[$rootI] = $rootJ;
        if($this->rank[$rootI] == $this->rank[$rootJ]){
            $this->rank[$rootJ]++;
        }
        $this->size[$rootJ]+=$this->size[$rootI];
        $this->size[$rootI] = '*';//不要な情報となるので潰す/デバッグ出力向けに*を入れている
        $this->rank[$rootI] = '*';//同上
    }
    public function size($i){return $this->size[$this->getRoot($i)];}
    public function isUnion($i, $j){return $this->getRoot($i) == $this->getRoot($j);}
}
0