結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
silviasetitech
|
| 提出日時 | 2020-03-05 10:46:09 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
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;
}
}
}
silviasetitech