結果

問題 No.1640 簡単な色塗り
ユーザー 小野寺健小野寺健
提出日時 2021-12-18 11:52:35
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 1,849 bytes
コンパイル時間 13,641 ms
コンパイル使用メモリ 203,528 KB
実行使用メモリ 198,768 KB
最終ジャッジ日時 2023-10-13 16:09:28
合計ジャッジ時間 17,970 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
198,768 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

type Cell struct {
	item int32
	next *Cell
}

type CellMgr struct {
	top, cp, last *Cell
}

func (this *CellMgr) append(item int32) {
	ncp := new(Cell)
	ncp.item = item
	if this.top == nil {
		this.top = ncp
		this.cp = ncp
		this.last = ncp
	} else {
		this.last.next = ncp
		this.last = ncp
	}
}

func (this *CellMgr) isNext() bool {
	return this.cp != this.last
}

func (this *CellMgr) next() (int32, bool) {
	cp := this.cp
	if cp != nil {
		this.cp = cp.next
		return cp.item, true
	} else {
		return 0, false
	}
}

var N int32
var G []*CellMgr
var match []int32
var used []bool

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	sc.Scan()
	_N, _ := strconv.Atoi(sc.Text())
	N = int32(_N)
	G = make([]*CellMgr, 2*N)
	match = make([]int32, 2*N)
	var i int32
	for i = 0; i < 2*N; i++ {
		G[i] = new(CellMgr)
		match[i] = -1
	}
	used = make([]bool, 2*N)
	for i = 0; i < N; i++ {
		var a, b int32
		sc.Scan()
		_a, _ := strconv.Atoi(sc.Text())
		a = int32(_a)
		sc.Scan()
		_b, _ := strconv.Atoi(sc.Text())
		b = int32(_b)
		add_edge(i, a + N - 1)
		add_edge(i, b + N - 1)
	}
	n := bipartite_matching()
	if n == N {
		fmt.Fprintln(w, "Yes")
		for i = 0; i < N; i++ {
			fmt.Fprintln(w, match[i] - N + 1)
		}
	} else {
		fmt.Fprintln(w, "No")
	}
}

func add_edge(u int32, v int32) {
	G[u].append(v)
	G[v].append(u)
}

func dfs(v int32) bool {
	used[v] = true
	for G[v].isNext() {
		u, _ := G[v].next()
		w := match[u]
		if w < 0 || (!used[w] && dfs(w)) {
			match[v] = u
			match[u] = v
			return true
		}
	}
	return false
}

func bipartite_matching() int32 {
	var res, v int32
	res = 0
	for v = 0; v < 2 * N; v++ {
		if match[v] < 0 {
			used = make([]bool, 2*N)
			if dfs(v) {
				res++;
			}
		}
	}
	return res
}
0