結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー Shun TakaseShun Takase
提出日時 2020-02-01 02:53:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,555 bytes
コンパイル時間 5,381 ms
コンパイル使用メモリ 195,808 KB
実行使用メモリ 23,080 KB
最終ジャッジ日時 2023-10-18 22:14:52
合計ジャッジ時間 5,230 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 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 WA -
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 13 ms
5,440 KB
testcase_14 AC 13 ms
5,440 KB
testcase_15 AC 13 ms
5,440 KB
testcase_16 AC 13 ms
5,440 KB
testcase_17 AC 12 ms
6,496 KB
testcase_18 AC 41 ms
9,376 KB
testcase_19 WA -
testcase_20 AC 66 ms
13,624 KB
testcase_21 AC 106 ms
17,320 KB
testcase_22 AC 139 ms
21,280 KB
testcase_23 AC 159 ms
23,080 KB
testcase_24 AC 155 ms
23,080 KB
testcase_25 AC 159 ms
23,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9+7))
#define REP(i, n) for(int i=0;i<int(n);++i)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define ALL(v) v.begin(),v.end()
#define FIND(v,x) (binary_search(ALL(v),(x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v));reverse(ALL(v))
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define Yes(n) cout<<((n)?"Yes":"No")<<endl
#define YES(n) cout<<((n)?"YES":"NO")<<endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template<class A>void pr(A a){cout << (a) << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " "  ;pr(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);}
template<class T> inline bool chmin(T& a, T b){return a>b ? a=b, true : false;}
template<class T> inline bool chmax(T& a, T b){return a<b ? a=b, true : false;}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

struct union_find {
    vector<int> parent; //親の番号を格納する。親だった場合は-(その集合のサイズ)
    union_find(int N) {
        parent = vector<int>(N, -1); //parentの値を-1にする(すべてバラバラ)
     }
    int root(int A) { //Aがどのグループに属しているか調べる
        if (parent[A] < 0) return A;
        return parent[A] = root(parent[A]);
    }
    int size(int A) { //自分のいるグループの頂点数を調べる
        return -parent[root(A)];//親をとってきたい
    }
    bool connect(int A, int B) { //AとBをくっ付ける.すでにつながっていた場合false
        //AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        A = root(A);
        B = root(B);
        if (A == B) return false; //すでにくっついてるからくっ付けない
        if (size(A) < size(B)) swap(A, B); //大小が逆だったらひっくり返しちゃう。
        parent[A] += parent[B]; //Aのサイズを更新する
        parent[B] = A; //Bの親をAに変更する
        return true;
    }
};

// Low Linkを計算して橋と関節点を見つける
// ref : http://kagamiz.hatenablog.com/entry/2013/10/05/005213
struct low_link
{
    int n, m;
    vector<vector<int>> G;
    vector<pair<int, int>> bridge;
    vector<int> articulation;
    vector<int> ord, low;
    vector<bool> vis;

    low_link(vector<vector<int>> G, int n, int m) : G(G), n(n), m(m)
    {
        bridge.resize(n);
        articulation.resize(n);
        ord.resize(n);
        low.resize(n);
        vis.resize(n);

        int k = 0;
        REP(i, n){
            if(!vis[i]) dfs(i, -1, k);
        }

        SORT(bridge);
        SORT(articulation);
    }

    vector<pair<int, int>> get_bridge(){
        return bridge;
    }

    vector<int> get_articulation(){
        return articulation;
    }

private:
    void dfs(int v, int p, int &k)
    {
        vis[v] = true;
        
        ord[v] = k++;
        low[v] = ord[v];
        
        bool isArticulation = false;
        int ct = 0;
        
        // for (int i = 0; i < G[v].size(); i++){
        for (auto&& c : G[v]){
            if (!vis[c]){
                ct++;
                dfs(c, v, k);
                low[v] = min(low[v], low[c]);
                if (~p && ord[v] <= low[c]) isArticulation = true;
                if (ord[v] < low[c]) bridge.push_back(make_pair(min(v, c), max(v, c)));
            }
            else if (c != p){
                low[v] = min(low[v], ord[c]);
            }
        }
        
        if (p == -1 && ct > 1) isArticulation = true;
        if (isArticulation) articulation.push_back(v);
    }
};


int main(void)
{
    int N;
    cin >> N;
    union_find uf(N);
    vector<vector<int>> G(N);
    REP(i, N-1){
        int a,b;
        cin >> a >> b;
        uf.connect(a,b);
        G[a].pb(b);
        G[b].pb(a);
    }

    low_link low(G, N, N-1);

    map<int, int> m;
    REP(i, N){
        m[(uf.root(i)<0?i:uf.root(i))]++;
    }

    if(m.size()==1){
        pr("Bob");
    }else if(m.size()==2){
        auto b = low.get_bridge();
        if(b.size() == 0){
            pr("Bob");
        }else{
            pr("Alice");
        }
    }else{
        pr("Alice");
    }
}
0