結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
neko_the_shadow
|
| 提出日時 | 2020-02-04 17:18:01 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 28 ms / 2,000 ms |
| コード長 | 1,986 bytes |
| コンパイル時間 | 14,585 ms |
| コンパイル使用メモリ | 241,372 KB |
| 実行使用メモリ | 7,628 KB |
| 最終ジャッジ日時 | 2024-09-21 07:33:10 |
| 合計ジャッジ時間 | 15,674 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
)
func exec(stdin *Stdin, stdout *Stdout) {
n := stdin.ReadInt()
uf := NewUnionFind(n)
d := make([]int, n)
for i := 0; i < n-1; i++ {
u := stdin.ReadInt()
v := stdin.ReadInt()
uf.Union(u, v)
d[u]++
d[v]++
}
m := map[int]int{}
for i := 0; i < n; i++ {
k := uf.Find(i)
if _, ok := m[k]; ok {
m[k]++
} else {
m[k] = 1
}
}
if len(m) == 1 {
stdout.Println("Bob")
} else if len(m) == 2 {
isAlice := false
for _, v := range d {
if v == 1 {
isAlice = true
}
}
if isAlice {
stdout.Println("Alice")
} else {
stdout.Println("Bob")
}
} else {
stdout.Println("Alice")
}
}
func main() {
stdout := NewStdout()
defer stdout.Flush()
exec(NewStdin(bufio.ScanWords), stdout)
}
type Stdin struct {
stdin *bufio.Scanner
}
func NewStdin(split bufio.SplitFunc) *Stdin {
s := Stdin{bufio.NewScanner(os.Stdin)}
s.stdin.Split(split)
s.stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), int(math.MaxInt32))
return &s
}
func (s *Stdin) Read() string {
s.stdin.Scan()
return s.stdin.Text()
}
func (s *Stdin) ReadInt() int {
n, _ := strconv.Atoi(s.Read())
return n
}
func (s *Stdin) ReadFloat64() float64 {
n, _ := strconv.ParseFloat(s.Read(), 64)
return n
}
type Stdout struct {
stdout *bufio.Writer
}
func NewStdout() *Stdout {
return &Stdout{bufio.NewWriter(os.Stdout)}
}
func (s *Stdout) Flush() {
s.stdout.Flush()
}
func (s *Stdout) Println(a ...interface{}) {
fmt.Fprintln(s.stdout, a...)
}
type UnionFind struct {
parents []int
}
func NewUnionFind(n int) *UnionFind {
uf := UnionFind{}
uf.parents = []int{}
for i := 0; i < n; i++ {
uf.parents = append(uf.parents, i)
}
return &uf
}
func (uf *UnionFind) Find(x int) int {
if uf.parents[x] == x {
return x
}
uf.parents[x] = uf.Find(uf.parents[x])
return uf.parents[x]
}
func (uf *UnionFind) Union(x, y int) {
x = uf.Find(x)
y = uf.Find(y)
if x != y {
uf.parents[y] = x
}
}
neko_the_shadow