結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-05 10:46:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,066 ms / 2,000 ms
コード長 4,485 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 81,004 KB
実行使用メモリ 81,164 KB
最終ジャッジ日時 2024-10-14 00:51:34
合計ジャッジ時間 13,319 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
41,292 KB
testcase_01 AC 115 ms
40,068 KB
testcase_02 AC 111 ms
40,204 KB
testcase_03 AC 117 ms
41,276 KB
testcase_04 AC 109 ms
39,828 KB
testcase_05 AC 124 ms
41,288 KB
testcase_06 AC 113 ms
40,252 KB
testcase_07 AC 142 ms
41,620 KB
testcase_08 AC 136 ms
41,248 KB
testcase_09 AC 140 ms
41,692 KB
testcase_10 AC 135 ms
41,700 KB
testcase_11 AC 136 ms
41,668 KB
testcase_12 AC 132 ms
41,708 KB
testcase_13 AC 307 ms
49,004 KB
testcase_14 AC 334 ms
49,140 KB
testcase_15 AC 315 ms
48,612 KB
testcase_16 AC 332 ms
48,856 KB
testcase_17 AC 311 ms
47,768 KB
testcase_18 AC 484 ms
52,932 KB
testcase_19 AC 533 ms
53,016 KB
testcase_20 AC 695 ms
56,172 KB
testcase_21 AC 860 ms
67,416 KB
testcase_22 AC 941 ms
76,952 KB
testcase_23 AC 1,017 ms
80,864 KB
testcase_24 AC 996 ms
81,164 KB
testcase_25 AC 1,066 ms
77,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        aliceMatenro solver = new aliceMatenro();
        solver.solve(1, in, out);
        out.close();
    }

    static class aliceMatenro {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            UnionFindTree uf = new UnionFindTree(n);
            Graph g = new Graph(n);
            for (int i = 0; i < n - 1; i++) {
                int a = in.nextInt();
                int b = in.nextInt();
                uf.union(a, b);
                g.addUnDirectedEdge(new Edge(a, b, 1));
            }

            int cnt = 0;
            for (int i = 0; i < n; i++) {
                if (!uf.same(0, i)) {
                    cnt++;
                    if (cnt > 1) {
                        out.println("Alice");
                        return;
                    } else {
                        for (int j = 0; j < n; j++) {
                            if (j != i && g.getAdj()[j].size() < 2) {
                                out.println("Alice");
                                return;
                            }
                        }
                    }
                }
            }

            out.println("Bob");
        }

    }

    static class Edge {
        int from;
        int to;
        long cost;

        public Edge(int to) {
            this.to = to;
        }

        public Edge(int to, long cost) {
            this.to = to;
            this.cost = cost;
        }

        public Edge(int from, int to, long cost) {
            this.from = from;
            this.to = to;
            this.cost = cost;
        }

        public Edge inv() {
            return new Edge(to, from, cost);
        }

        public String toString() {
            return "Edge{" +
                    "from=" + from +
                    ", to=" + to +
                    ", cost=" + cost +
                    '}';
        }

    }

    static class UnionFindTree {
        private int[] parent;
        private int[] height;
        private int[] size;

        public UnionFindTree(int size) {
            this.parent = new int[size];
            this.height = new int[size];
            this.size = new int[size];

            for (int i = 0; i < size; i++) {
                makeSet(i);
            }
        }

        private void makeSet(int i) {
            parent[i] = i;
            height[i] = 0;
            size[i] = 1;
        }

        public int find(int x) {
            if (x != parent[x]) {
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }

        public void union(int x, int y) {

            int rootX = find(x);
            int rootY = find(y);

            if (rootX == rootY) { //既に結合済み
                return;
            }

            if (height[rootX] > height[rootY]) {
                parent[rootY] = rootX;
                size[rootX] += size[rootY];

            } else if (height[rootX] < height[rootY]) {
                parent[rootX] = rootY;
                size[rootY] += size[rootX];

            } else {
                parent[rootY] = rootX;
                size[rootX] += size[rootY];
                height[rootX]++;
            }
        }

        public boolean same(int x, int y) {
            return find(x) == find(y);
        }

    }

    static class Graph {
        int n;
        ArrayList<Edge>[] adj;
        long[] vertexCost;

        public Graph(int size) {
            n = size;
            adj = new ArrayList[n];
            for (int i = 0; i < n; i++) {
                adj[i] = new ArrayList<>();
            }
            vertexCost = new long[n];
        }

        public void addEdge(Edge e) {
            adj[e.from].add(e);
        }

        public void addUnDirectedEdge(Edge e) {
            addEdge(e);
            addEdge(e.inv());
        }

        public ArrayList<Edge>[] getAdj() {
            return adj;
        }

    }
}

0