結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-05 10:46:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 980 ms / 2,000 ms
コード長 4,485 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 87,856 KB
実行使用メモリ 80,908 KB
最終ジャッジ日時 2024-04-22 02:11:03
合計ジャッジ時間 12,222 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
40,760 KB
testcase_01 AC 105 ms
41,124 KB
testcase_02 AC 114 ms
41,024 KB
testcase_03 AC 104 ms
41,084 KB
testcase_04 AC 100 ms
39,864 KB
testcase_05 AC 106 ms
41,148 KB
testcase_06 AC 113 ms
41,180 KB
testcase_07 AC 130 ms
41,548 KB
testcase_08 AC 128 ms
41,272 KB
testcase_09 AC 130 ms
41,492 KB
testcase_10 AC 126 ms
41,252 KB
testcase_11 AC 126 ms
41,392 KB
testcase_12 AC 127 ms
41,248 KB
testcase_13 AC 285 ms
49,108 KB
testcase_14 AC 303 ms
48,976 KB
testcase_15 AC 283 ms
47,716 KB
testcase_16 AC 300 ms
48,848 KB
testcase_17 AC 307 ms
49,328 KB
testcase_18 AC 499 ms
53,084 KB
testcase_19 AC 503 ms
53,152 KB
testcase_20 AC 530 ms
56,120 KB
testcase_21 AC 746 ms
67,680 KB
testcase_22 AC 869 ms
76,960 KB
testcase_23 AC 944 ms
80,344 KB
testcase_24 AC 917 ms
80,908 KB
testcase_25 AC 980 ms
78,608 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