結果

問題 No.1640 簡単な色塗り
ユーザー HimaHima
提出日時 2022-07-09 20:39:25
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 3,145 bytes
コンパイル時間 14,068 ms
コンパイル使用メモリ 213,852 KB
実行使用メモリ 33,052 KB
最終ジャッジ日時 2023-08-30 04:54:40
合計ジャッジ時間 27,455 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 56 ms
18,868 KB
testcase_05 AC 68 ms
33,040 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 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 13 ms
7,188 KB
testcase_31 AC 89 ms
24,844 KB
testcase_32 AC 79 ms
22,696 KB
testcase_33 AC 61 ms
20,484 KB
testcase_34 AC 77 ms
30,812 KB
testcase_35 AC 63 ms
20,484 KB
testcase_36 AC 15 ms
9,624 KB
testcase_37 AC 21 ms
11,960 KB
testcase_38 AC 85 ms
24,756 KB
testcase_39 AC 35 ms
14,200 KB
testcase_40 AC 36 ms
14,208 KB
testcase_41 AC 73 ms
22,656 KB
testcase_42 AC 36 ms
14,300 KB
testcase_43 AC 37 ms
14,296 KB
testcase_44 AC 42 ms
18,396 KB
testcase_45 AC 30 ms
14,068 KB
testcase_46 AC 14 ms
9,948 KB
testcase_47 AC 9 ms
7,516 KB
testcase_48 AC 96 ms
32,928 KB
testcase_49 AC 4 ms
5,524 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
}
0