結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー snow39snow39
提出日時 2020-01-31 21:50:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,647 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 99,436 KB
実行使用メモリ 5,712 KB
最終ジャッジ日時 2023-10-17 09:23:31
合計ジャッジ時間 2,264 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 19 ms
4,396 KB
testcase_19 AC 19 ms
4,396 KB
testcase_20 AC 30 ms
4,440 KB
testcase_21 AC 47 ms
5,184 KB
testcase_22 AC 60 ms
5,712 KB
testcase_23 AC 62 ms
5,712 KB
testcase_24 AC 62 ms
5,712 KB
testcase_25 AC 63 ms
5,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};

int main(){
  int N;
  cin>>N;
  vector<int> u(N-1),v(N-1);
  rep(i,N-1) cin>>u[i]>>v[i];

  DisjointSet us(N);
  vector<int> deg(N,0);
  rep(i,N-1){
    deg[u[i]]++;
    deg[v[i]]++;
    if(us.same(u[i],v[i]));
    else us.unite(u[i],v[i]);
  }

  int com=0;
  rep(i,N) if(us.findSet(i)==i) com++;

  if(com==1) cout<<"Bob"<<endl;
  else if(com==2){
    bool ok=false;
    rep(i,N) if(deg[i]==1) ok=true;
    if(ok) cout<<"Alice"<<endl;
    else cout<<"Bob"<<endl;
  }else cout<<"Alice"<<endl;

  return 0;
}
0