結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
ccppjsrb
|
| 提出日時 | 2020-10-23 23:01:28 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 241 ms / 3,000 ms |
| コード長 | 4,080 bytes |
| コンパイル時間 | 17,004 ms |
| コンパイル使用メモリ | 227,292 KB |
| 実行使用メモリ | 36,480 KB |
| 最終ジャッジ日時 | 2024-07-21 12:11:31 |
| 合計ジャッジ時間 | 21,653 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
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
}
ccppjsrb