結果

問題 No.1266 7 Colors
ユーザー ccppjsrbccppjsrb
提出日時 2020-10-23 23:01:28
言語 Go
(1.22.1)
結果
AC  
実行時間 221 ms / 3,000 ms
コード長 4,080 bytes
コンパイル時間 14,591 ms
コンパイル使用メモリ 213,676 KB
実行使用メモリ 37,456 KB
最終ジャッジ日時 2023-09-28 17:32:58
合計ジャッジ時間 17,622 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 116 ms
16,544 KB
testcase_04 AC 203 ms
27,072 KB
testcase_05 AC 135 ms
16,536 KB
testcase_06 AC 201 ms
29,180 KB
testcase_07 AC 192 ms
31,292 KB
testcase_08 AC 187 ms
27,080 KB
testcase_09 AC 194 ms
22,988 KB
testcase_10 AC 181 ms
22,972 KB
testcase_11 AC 146 ms
18,640 KB
testcase_12 AC 161 ms
18,644 KB
testcase_13 AC 175 ms
20,852 KB
testcase_14 AC 132 ms
16,528 KB
testcase_15 AC 221 ms
31,304 KB
testcase_16 AC 167 ms
18,632 KB
testcase_17 AC 214 ms
31,276 KB
testcase_18 AC 115 ms
37,456 KB
testcase_19 AC 110 ms
31,272 KB
testcase_20 AC 110 ms
31,280 KB
testcase_21 AC 46 ms
10,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	m := getNextInt(scanner)
	q := getNextInt(scanner)
	ss := make([][]string, n)
	d := NewDsu(n * 7)
	g := newGraph(n)
	for i := 0; i < n; i++ {
		ss[i] = strings.Split(getNextString(scanner), "")
		for j := 0; j < 7; j++ {
			if ss[i][j] == "1" && ss[i][(j+1)%7] == "1" {
				d.Merge(i*7+j, i*7+(j+1)%7)
			}
		}
	}
	for i := 0; i < m; i++ {
		u := getNextInt(scanner) - 1
		v := getNextInt(scanner) - 1
		g.AppendEdge(u, v, i)
		g.AppendEdge(v, u, i)
		for j := 0; j < 7; j++ {
			if ss[u][j] == "1" && ss[v][j] == "1" {
				d.Merge(u*7+j, v*7+j)
			}
		}
	}
	for q > 0 {
		q--
		func() {
			t := getNextInt(scanner)
			x := getNextInt(scanner) - 1
			y := getNextInt(scanner) - 1
			if t == 1 {
				ss[x][y] = "1"
				if ss[x][(y+7-1)%7] == "1" {
					d.Merge(x*7+y, x*7+(y+7-1)%7)
				}
				if ss[x][(y+7+1)%7] == "1" {
					d.Merge(x*7+y, x*7+(y+7+1)%7)
				}
				for _, e := range g.e[x] {
					if ss[e.to][y] == "1" {
						d.Merge(x*7+y, e.to*7+y)
					}
				}
				return
			}
			fmt.Fprintln(writer, d.Size(x*7))
		}()
	}
}

type vertex struct {
}
type edge struct {
	to, id int
}
type graph struct {
	v []vertex
	e [][]edge
}

func newGraph(n int) graph {
	return graph{
		v: make([]vertex, n),
		e: make([][]edge, n),
	}
}
func (g *graph) AppendEdge(from, to, id int) {
	g.e[from] = append(g.e[from], edge{
		to: to,
		id: id,
	})
}

// Dsu Data structures and algorithms for disjoint set union problems
type Dsu struct {
	n            int
	parentOrSize []int
}

// NewDsu Constructor
func NewDsu(n int) *Dsu {
	p := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = -1
	}
	return &Dsu{parentOrSize: p, n: n}
}

// Merge adds an edge (a, b).
func (d *Dsu) Merge(a, b int) int {
	x := d.Leader(a)
	y := d.Leader(b)
	if x == y {
		return x
	}
	if d.parentOrSize[x] > d.parentOrSize[y] {
		x, y = y, x
	}
	d.parentOrSize[x] += d.parentOrSize[y]
	d.parentOrSize[y] = x
	return x
}

// Same returns whether the vertices a and b are in the same connected component.
func (d *Dsu) Same(a, b int) bool {
	return d.Leader(a) == d.Leader(b)
}

// Leader returns the representative of the connected component that contains the vertex a.
func (d *Dsu) Leader(a int) int {
	if d.parentOrSize[a] < 0 {
		return a
	}
	d.parentOrSize[a] = d.Leader(d.parentOrSize[a])
	return d.parentOrSize[a]
}

// Size returns the size of the connected component that contains the vertex a.
func (d *Dsu) Size(a int) int {
	return -d.parentOrSize[d.Leader(a)]
}

// Groups divides the graph into connected components and returns the list of them.
func (d *Dsu) Groups() [][]int {
	result := make([][]int, d.n)
	groups := make([][]int, 0)
	for i := 0; i < d.n; i++ {
		l := d.Leader(i)
		result[l] = append(result[l], i)
	}
	for i := 0; i < d.n; i++ {
		if result[i] == nil {
			continue
		}
		groups = append(groups, result[i])
	}
	return groups
}
0