結果
問題 | No.1640 簡単な色塗り |
ユーザー | Hima |
提出日時 | 2022-07-09 20:39:25 |
言語 | Go (1.23.4) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,145 bytes |
コンパイル時間 | 12,027 ms |
コンパイル使用メモリ | 219,932 KB |
実行使用メモリ | 32,604 KB |
最終ジャッジ日時 | 2024-12-31 13:45:38 |
合計ジャッジ時間 | 20,781 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 64 ms
20,144 KB |
testcase_05 | AC | 76 ms
32,580 KB |
testcase_06 | AC | 1 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,820 KB |
testcase_08 | AC | 2 ms
6,816 KB |
testcase_09 | AC | 2 ms
6,820 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | AC | 11 ms
6,864 KB |
testcase_31 | AC | 62 ms
24,388 KB |
testcase_32 | AC | 52 ms
22,332 KB |
testcase_33 | AC | 44 ms
20,168 KB |
testcase_34 | AC | 63 ms
30,444 KB |
testcase_35 | AC | 44 ms
20,156 KB |
testcase_36 | AC | 13 ms
9,328 KB |
testcase_37 | AC | 16 ms
11,796 KB |
testcase_38 | AC | 68 ms
24,380 KB |
testcase_39 | AC | 28 ms
13,908 KB |
testcase_40 | AC | 30 ms
13,904 KB |
testcase_41 | AC | 54 ms
22,256 KB |
testcase_42 | AC | 25 ms
12,044 KB |
testcase_43 | AC | 28 ms
13,964 KB |
testcase_44 | AC | 27 ms
13,964 KB |
testcase_45 | AC | 24 ms
11,736 KB |
testcase_46 | AC | 12 ms
9,732 KB |
testcase_47 | AC | 7 ms
7,536 KB |
testcase_48 | AC | 69 ms
32,588 KB |
testcase_49 | AC | 3 ms
6,816 KB |
testcase_50 | AC | 2 ms
6,820 KB |
testcase_51 | AC | 1 ms
6,816 KB |
testcase_52 | WA | - |
testcase_53 | WA | - |
07_evil_01.txt | WA | - |
07_evil_02.txt | WA | - |
ソースコード
package main import ( "bufio" "errors" "fmt" "os" "strconv" ) var sc = bufio.NewScanner(os.Stdin) var out = bufio.NewWriter(os.Stdout) func solve(n int, a, b []int) ([]int, error) { type edge struct { i, t int } e := make([][]edge, n+1) uf := NewUnionFind(n) for i := range a { //e = append(e, edge{i, a[i], b[i]}) e[a[i]] = append(e[a[i]], edge{i, b[i]}) e[b[i]] = append(e[b[i]], edge{i, a[i]}) uf.Unite(a[i], b[i]) } visited := make([]bool, n+1) visited[0] = true ans := make([]int, n) var dfs func(cur, par int) dfs = func(cur, par int) { if visited[cur] { return } visited[cur] = true for _, next := range e[cur] { if ans[next.i] == 0 { ans[next.i] = next.t } if next.t == par { continue } dfs(next.t, cur) } } for i := 1; i <= n; i++ { if visited[i] { continue } dfs(i, 0) } ok := true for _, b := range visited { ok = ok && b } m := make(map[int]struct{}) for _, v := range ans { m[v] = struct{}{} } if ok && len(m) == n { return ans, nil } else { return nil, errors.New("No") } } func main() { buf := make([]byte, 1024*1024) sc.Buffer(buf, bufio.MaxScanTokenSize) sc.Split(bufio.ScanWords) n := nextInt() var a, b []int for i := 0; i < n; i++ { a = append(a, nextInt()) b = append(b, nextInt()) } ans, err := solve(n, a, b) if err != nil { PrintString("No") return } PrintString("Yes") PrintVertically(ans) } func nextInt() int { sc.Scan() i, _ := strconv.Atoi(sc.Text()) return i } func PrintInt(x int) { defer out.Flush() fmt.Fprintln(out, x) } func PrintString(x string) { defer out.Flush() fmt.Fprintln(out, x) } func PrintVertically(x []int) { defer out.Flush() for _, v := range x { fmt.Fprintln(out, v) } } type UnionFind struct { par []int // parent numbers rank []int // height of tree size []int } func NewUnionFind(n int) *UnionFind { if n <= 0 { return nil } u := new(UnionFind) // for accessing index without minus 1 u.par = make([]int, n+1) u.rank = make([]int, n+1) u.size = make([]int, n+1) for i := 0; i <= n; i++ { u.par[i] = i u.rank[i] = 0 u.size[i] = 1 } return u } func (this *UnionFind) Find(x int) int { if this.par[x] == x { return x } else { // compress path // ex. Find(4) // 1 - 2 - 3 - 4 // 1 - 2 // L-3 // L 4 this.par[x] = this.Find(this.par[x]) return this.par[x] } } func (this *UnionFind) Size(x int) int { return this.size[this.Find(x)] } func (this *UnionFind) ExistSameUnion(x, y int) bool { return this.Find(x) == this.Find(y) } func (this *UnionFind) Unite(x, y int) { x = this.Find(x) y = this.Find(y) if x == y { return } // rank if this.rank[x] < this.rank[y] { //yがrootの木にxがrootの木を結合する this.par[x] = y this.size[y] += this.size[x] } else { // this.rank[x] >= this.rank[y] //xがrootの木にyがrootの木を結合する this.par[y] = x this.size[x] += this.size[y] if this.rank[x] == this.rank[y] { this.rank[x]++ } } } func PrintUnionFind(u *UnionFind) { // for debuging. not optimize. fmt.Println(u.par) fmt.Println(u.rank) fmt.Println(u.size) }