結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー merom686merom686
提出日時 2020-01-31 22:06:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,884 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 79,080 KB
実行使用メモリ 5,548 KB
最終ジャッジ日時 2023-10-17 09:55:49
合計ジャッジ時間 1,606 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 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 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 8 ms
4,492 KB
testcase_20 AC 12 ms
4,492 KB
testcase_21 AC 17 ms
5,020 KB
testcase_22 AC 23 ms
5,548 KB
testcase_23 AC 23 ms
5,548 KB
testcase_24 AC 21 ms
5,548 KB
testcase_25 AC 24 ms
5,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    struct Vertex { int n, f; };
    struct Edge { int i, n; };
    Graph(int n, int m) : v(n, { -1 }), e(m), n(n), m(0) {}
    void add_edge(int i, int j) {
        e[m] = { j, v[i].n };
        v[i].n = m;
        m++;
    }
    void dfs(int i, int p, int d) {
        if (v[i].f) return; else v[i].f = 1;
        int k = 0;
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge &o = e[j];
            k++;
            if (o.i == p) continue;
            dfs(o.i, i, d + 1);
        }
        l = min(l, k);
    }
    void solve() {
        int x = 0;
        int p[2];
        for (int i = 0; i < n; i++) {
            if (v[i].f) continue;
            l = 2;
            dfs(i, -1, 0);
            if (x < 2) {
                p[x] = l;
            }
            x++;
        }
        int b;
        if (x < 2) {
            b = 1;
        } else if (x > 2) {
            b = 0;
        } else {
            if (p[0] == 1 || p[1] == 1) b = 0; else b = 1;
        }
        cout << (b == 0 ? "Alice" : "Bob") << endl;
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
    int l;
};


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    Graph g(n, (n - 1) * 2);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;

        g.add_edge(a, b);
        g.add_edge(b, a);
    }
    g.solve();

    //連結成分が1個のときは爆破したところを直せばいい
    //3個以上のときは無理
    //2個のとき、連結成分を増やせるかが勝負 常になもりと木か?
    //ただし木の側が辺を持ってないと ただの閉路というのもある

    return 0;
}
0