結果

問題 No.1078 I love Matrix Construction
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-21 11:41:23
言語 Go
(1.22.1)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 4,388 bytes
コンパイル時間 12,273 ms
コンパイル使用メモリ 201,196 KB
実行使用メモリ 92,248 KB
最終ジャッジ日時 2023-09-29 05:30:55
合計ジャッジ時間 19,050 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 50 ms
16,484 KB
testcase_03 AC 150 ms
37,392 KB
testcase_04 AC 208 ms
52,304 KB
testcase_05 AC 174 ms
41,896 KB
testcase_06 AC 48 ms
16,300 KB
testcase_07 AC 15 ms
7,912 KB
testcase_08 AC 173 ms
41,816 KB
testcase_09 AC 5 ms
5,632 KB
testcase_10 AC 416 ms
92,248 KB
testcase_11 AC 220 ms
52,296 KB
testcase_12 AC 341 ms
77,492 KB
testcase_13 AC 381 ms
90,152 KB
testcase_14 AC 258 ms
62,732 KB
testcase_15 AC 357 ms
81,784 KB
testcase_16 AC 13 ms
7,820 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 42 ms
13,064 KB
testcase_19 AC 95 ms
24,852 KB
testcase_20 AC 95 ms
24,960 KB
testcase_21 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	S := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &S[i])
		S[i]--
	}
	T := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &T[i])
		T[i]--
	}
	U := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &U[i])
	}

	// 条件i为A[i][j]取0
	ts := NewTwoSat(n * n)
	for i := 0; i < n; i++ {
		si := S[i]
		ti := T[i]
		for j := 0; j < n; j++ {
			pos1 := si*n + j
			pos2 := j*n + ti

			if U[i] == 0 {
				ts.AddNand(pos1, pos2) // 0,0
			} else if U[i] == 1 {
				ts.AddNand(ts.Rev(pos1), pos2) //1,0
			} else if U[i] == 2 {
				ts.AddNand(pos1, ts.Rev(pos2)) //0,1
			} else if U[i] == 3 {
				ts.AddNand(ts.Rev(pos1), ts.Rev(pos2)) //1,1
			}

		}
	}

	res, ok := ts.Solve()
	if !ok {
		fmt.Fprintln(out, -1)
		return
	}

	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
	}

	for i, v := range res {
		if !v {
			matrix[i/n][i%n] = 1
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Fprint(out, matrix[i][j], " ")
		}
		fmt.Fprintln(out)
	}
}

type TwoSat struct {
	sz  int
	scc *StronglyConnectedComponents
}

func NewTwoSat(n int) *TwoSat {
	return &TwoSat{sz: n, scc: NewStronglyConnectedComponents(n + n)}
}

// u -> v <=> !v -> !u
func (ts *TwoSat) AddIf(u, v int) {
	ts.scc.AddEdge(u, v)
	ts.scc.AddEdge(ts.Rev(v), ts.Rev(u))
}

// u or v <=> !u -> v
func (ts *TwoSat) AddOr(u, v int) {
	ts.AddIf(ts.Rev(u), v)
}

// u nand v <=> u -> !v
func (ts *TwoSat) AddNand(u, v int) {
	ts.AddIf(u, ts.Rev(v))
}

// u <=> !u -> u
func (ts *TwoSat) SetTrue(u int) {
	ts.scc.AddEdge(ts.Rev(u), u)
}

// !u <=> u -> !u
func (ts *TwoSat) SetFalse(u int) {
	ts.scc.AddEdge(u, ts.Rev(u))
}

func (ts *TwoSat) Rev(u int) int {
	if u >= ts.sz {
		return u - ts.sz
	}
	return u + ts.sz
}

func (ts *TwoSat) Solve() (res []bool, ok bool) {
	ts.scc.Build()
	res = make([]bool, ts.sz)
	for i := 0; i < ts.sz; i++ {
		if ts.scc.Comp[i] == ts.scc.Comp[ts.Rev(i)] {
			return
		}
		res[i] = ts.scc.Comp[i] > ts.scc.Comp[ts.Rev(i)]
	}
	ok = true
	return
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

type Edge struct{ from, to int }
type StronglyConnectedComponents struct {
	G     [][]Edge // 原图
	Dag   [][]Edge // 强连通分量缩点后的顶点和边组成的DAG
	Comp  []int    //每个顶点所属的强连通分量的编号
	Group [][]int  // 每个强连通分量所包含的顶点
	rg    [][]Edge
	order []int
	used  []bool
}

func NewStronglyConnectedComponents(n int) *StronglyConnectedComponents {
	return &StronglyConnectedComponents{G: make([][]Edge, n)}
}

func (scc *StronglyConnectedComponents) AddEdge(from, to int) {
	scc.G[from] = append(scc.G[from], Edge{from, to})
}

func (scc *StronglyConnectedComponents) Build() {
	scc.rg = make([][]Edge, len(scc.G))
	for i := range scc.G {
		for _, e := range scc.G[i] {
			scc.rg[e.to] = append(scc.rg[e.to], Edge{e.to, e.from})
		}
	}

	scc.Comp = make([]int, len(scc.G))
	for i := range scc.Comp {
		scc.Comp[i] = -1
	}
	scc.used = make([]bool, len(scc.G))
	for i := range scc.G {
		scc.dfs(i)
	}
	for i, j := 0, len(scc.order)-1; i < j; i, j = i+1, j-1 {
		scc.order[i], scc.order[j] = scc.order[j], scc.order[i]
	}

	ptr := 0
	for _, v := range scc.order {
		if scc.Comp[v] == -1 {
			scc.rdfs(v, ptr)
			ptr++
		}
	}

	dag := make([][]Edge, ptr)
	for i := range scc.G {
		for _, e := range scc.G[i] {
			x, y := scc.Comp[e.from], scc.Comp[e.to]
			if x == y {
				continue
			}
			dag[x] = append(dag[x], Edge{x, y})
		}
	}
	scc.Dag = dag

	scc.Group = make([][]int, ptr)
	for i := range scc.G {
		scc.Group[scc.Comp[i]] = append(scc.Group[scc.Comp[i]], i)
	}
}

// 获取顶点k所属的强连通分量的编号
func (scc *StronglyConnectedComponents) Get(k int) int {
	return scc.Comp[k]
}

func (scc *StronglyConnectedComponents) dfs(idx int) {
	tmp := scc.used[idx]
	scc.used[idx] = true
	if tmp {
		return
	}
	for _, e := range scc.G[idx] {
		scc.dfs(e.to)
	}
	scc.order = append(scc.order, idx)
}

func (scc *StronglyConnectedComponents) rdfs(idx int, cnt int) {
	if scc.Comp[idx] != -1 {
		return
	}
	scc.Comp[idx] = cnt
	for _, e := range scc.rg[idx] {
		scc.rdfs(e.to, cnt)
	}
}
0