結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー Yoichiro Nishimura
提出日時 2020-02-06 10:56:49
言語 PHP
(843.2)
結果
WA  
実行時間 -
コード長 1,688 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 32,148 KB
実行使用メモリ 39,252 KB
最終ジャッジ日時 2024-09-25 05:32:34
合計ジャッジ時間 3,149 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24 WA * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
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";
}else{
  echo "Alice";
}


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