結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-31 00:38:12
言語 Go
(1.22.1)
結果
AC  
実行時間 708 ms / 4,000 ms
コード長 5,683 bytes
コンパイル時間 12,465 ms
コンパイル使用メモリ 210,968 KB
実行使用メモリ 134,256 KB
最終ジャッジ日時 2023-10-22 07:34:10
合計ジャッジ時間 30,651 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 10 ms
4,348 KB
testcase_09 AC 14 ms
4,348 KB
testcase_10 AC 26 ms
7,784 KB
testcase_11 AC 25 ms
7,752 KB
testcase_12 AC 13 ms
4,348 KB
testcase_13 AC 366 ms
37,712 KB
testcase_14 AC 445 ms
49,892 KB
testcase_15 AC 466 ms
48,316 KB
testcase_16 AC 201 ms
33,080 KB
testcase_17 AC 459 ms
54,120 KB
testcase_18 AC 399 ms
50,240 KB
testcase_19 AC 530 ms
75,328 KB
testcase_20 AC 415 ms
47,116 KB
testcase_21 AC 454 ms
56,264 KB
testcase_22 AC 529 ms
67,036 KB
testcase_23 AC 696 ms
79,420 KB
testcase_24 AC 696 ms
79,604 KB
testcase_25 AC 698 ms
79,576 KB
testcase_26 AC 697 ms
79,592 KB
testcase_27 AC 708 ms
79,380 KB
testcase_28 AC 706 ms
79,612 KB
testcase_29 AC 704 ms
73,164 KB
testcase_30 AC 697 ms
79,588 KB
testcase_31 AC 695 ms
79,596 KB
testcase_32 AC 692 ms
79,328 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 248 ms
22,732 KB
testcase_35 AC 545 ms
134,220 KB
testcase_36 AC 462 ms
41,944 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 136 ms
5,656 KB
testcase_39 AC 637 ms
134,256 KB
testcase_40 AC 528 ms
89,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	yuki1983()
}

func yuki1983() {
	// https://yukicoder.me/problems/no/1983
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n, m, q int
	fmt.Fscan(in, &n, &m, &q)
	graph := make([][]Edge, n)
	edges := make([][2]int, m)
	for i := 0; i < m; i++ {
		var u, v int
		fmt.Fscan(in, &u, &v)
		u--
		v--
		graph[u] = append(graph[u], Edge{u, v, 1, i})
		graph[v] = append(graph[v], Edge{v, u, 1, i})
		edges[i] = [2]int{u, v}
	}
	tec := NewTwoEdgeConnectedComponents(graph)
	tec.Build()

	uf := NewUnionFindArray(n)
	for _, e := range edges {
		from_, to := e[0], e[1]
		if tec.Get(from_) != tec.Get(to) {
			uf.Union(from_, to)
		}
	}

	for i := 0; i < q; i++ {
		var u, v int
		fmt.Fscan(in, &u, &v)
		u--
		v--
		if uf.IsConnected(u, v) {
			fmt.Fprintln(out, "Yes")
		} else {
			fmt.Fprintln(out, "No")
		}
	}
}

type Edge = struct{ from, to, cost, index int }
type TwoEdgeConnectedComponents struct {
	Tree    [][]Edge // 缩点后各个顶点形成的树
	CompId  []int    // 每个点所属的边双连通分量的编号
	Group   [][]int  // 每个边双连通分量里的点
	g       [][]Edge
	lowLink *LowLink
	k       int
}

func NewTwoEdgeConnectedComponents(g [][]Edge) *TwoEdgeConnectedComponents {
	return &TwoEdgeConnectedComponents{
		g:       g,
		lowLink: NewLowLink(g),
	}
}

func (tec *TwoEdgeConnectedComponents) Build() {
	tec.lowLink.Build()
	tec.CompId = make([]int, len(tec.g))
	for i := 0; i < len(tec.g); i++ {
		tec.CompId[i] = -1
	}
	for i := 0; i < len(tec.g); i++ {
		if tec.CompId[i] == -1 {
			tec.dfs(i, -1)
		}
	}
	tec.Group = make([][]int, tec.k)
	for i := 0; i < len(tec.g); i++ {
		tec.Group[tec.CompId[i]] = append(tec.Group[tec.CompId[i]], i)
	}
	tec.Tree = make([][]Edge, tec.k)
	for _, e := range tec.lowLink.Bridge {
		tec.Tree[tec.CompId[e.from]] = append(tec.Tree[tec.CompId[e.from]], Edge{tec.CompId[e.from], tec.CompId[e.to], e.cost, e.index})
		tec.Tree[tec.CompId[e.to]] = append(tec.Tree[tec.CompId[e.to]], Edge{tec.CompId[e.to], tec.CompId[e.from], e.cost, e.index})
	}
}

// 每个点所属的边双连通分量的编号.
func (tec *TwoEdgeConnectedComponents) Get(k int) int { return tec.CompId[k] }

func (tec *TwoEdgeConnectedComponents) dfs(idx, par int) {
	if par >= 0 && tec.lowLink.ord[par] >= tec.lowLink.low[idx] {
		tec.CompId[idx] = tec.CompId[par]
	} else {
		tec.CompId[idx] = tec.k
		tec.k++
	}
	for _, e := range tec.g[idx] {
		if tec.CompId[e.to] == -1 {
			tec.dfs(e.to, idx)
		}
	}
}

type LowLink struct {
	Articulation []int  // 関節点
	Bridge       []Edge // 橋
	g            [][]Edge
	ord, low     []int
	used         []bool
}

func NewLowLink(g [][]Edge) *LowLink {
	return &LowLink{g: g}
}

func (ll *LowLink) Build() {
	ll.used = make([]bool, len(ll.g))
	ll.ord = make([]int, len(ll.g))
	ll.low = make([]int, len(ll.g))
	k := 0
	for i := 0; i < len(ll.g); i++ {
		if !ll.used[i] {
			k = ll.dfs(i, k, -1)
		}
	}
}

func (ll *LowLink) dfs(idx, k, par int) int {
	ll.used[idx] = true
	ll.ord[idx] = k
	k++
	ll.low[idx] = ll.ord[idx]
	isArticulation := false
	beet := false
	cnt := 0
	for _, e := range ll.g[idx] {
		if e.to == par {
			tmp := beet
			beet = true
			if !tmp {
				continue
			}
		}
		if !ll.used[e.to] {
			cnt++
			k = ll.dfs(e.to, k, idx)
			ll.low[idx] = min(ll.low[idx], ll.low[e.to])
			if par >= 0 && ll.low[e.to] >= ll.ord[idx] {
				isArticulation = true
			}
			if ll.ord[idx] < ll.low[e.to] {
				ll.Bridge = append(ll.Bridge, e)
			}
		} else {
			ll.low[idx] = min(ll.low[idx], ll.ord[e.to])
		}
	}

	if par == -1 && cnt > 1 {
		isArticulation = true
	}
	if isArticulation {
		ll.Articulation = append(ll.Articulation, idx)
	}
	return k
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func NewUnionFindArray(n int) *UnionFindArray {
	parent, rank := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		parent[i] = i
		rank[i] = 1
	}

	return &UnionFindArray{
		Part:   n,
		rank:   rank,
		n:      n,
		parent: parent,
	}
}

type UnionFindArray struct {
	// 连通分量的个数
	Part int

	rank   []int
	n      int
	parent []int
}

func (ufa *UnionFindArray) Union(key1, key2 int) bool {
	root1, root2 := ufa.Find(key1), ufa.Find(key2)
	if root1 == root2 {
		return false
	}

	if ufa.rank[root1] > ufa.rank[root2] {
		root1, root2 = root2, root1
	}
	ufa.parent[root1] = root2
	ufa.rank[root2] += ufa.rank[root1]
	ufa.Part--
	return true
}

func (ufa *UnionFindArray) UnionWithCallback(key1, key2 int, cb func(big, small int)) bool {
	root1, root2 := ufa.Find(key1), ufa.Find(key2)
	if root1 == root2 {
		return false
	}
	if ufa.rank[root1] > ufa.rank[root2] {
		root1, root2 = root2, root1
	}
	ufa.parent[root1] = root2
	ufa.rank[root2] += ufa.rank[root1]
	ufa.Part--
	cb(root2, root1)
	return true
}

func (ufa *UnionFindArray) Find(key int) int {
	for ufa.parent[key] != key {
		ufa.parent[key] = ufa.parent[ufa.parent[key]]
		key = ufa.parent[key]
	}
	return key
}

func (ufa *UnionFindArray) IsConnected(key1, key2 int) bool {
	return ufa.Find(key1) == ufa.Find(key2)
}

func (ufa *UnionFindArray) GetGroups() map[int][]int {
	groups := make(map[int][]int)
	for i := 0; i < ufa.n; i++ {
		root := ufa.Find(i)
		groups[root] = append(groups[root], i)
	}
	return groups
}

func (ufa *UnionFindArray) Size(key int) int {
	return ufa.rank[ufa.Find(key)]
}

func (ufa *UnionFindArray) String() string {
	sb := []string{"UnionFindArray:"}
	for root, member := range ufa.GetGroups() {
		cur := fmt.Sprintf("%d: %v", root, member)
		sb = append(sb, cur)
	}
	sb = append(sb, fmt.Sprintf("Part: %d", ufa.Part))
	return strings.Join(sb, "\n")
}
0