結果

問題 No.1054 Union add query
ユーザー aru aruaru aru
提出日時 2020-10-28 23:12:11
言語 Go
(1.22.1)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 2,410 bytes
コンパイル時間 14,295 ms
コンパイル使用メモリ 213,140 KB
実行使用メモリ 10,540 KB
最終ジャッジ日時 2023-09-29 03:51:38
合計ジャッジ時間 14,721 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,356 KB
testcase_03 AC 151 ms
4,460 KB
testcase_04 AC 185 ms
10,376 KB
testcase_05 AC 135 ms
5,564 KB
testcase_06 AC 141 ms
7,800 KB
testcase_07 AC 106 ms
7,800 KB
testcase_08 AC 126 ms
7,796 KB
testcase_09 AC 172 ms
10,540 KB
testcase_10 AC 67 ms
7,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

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

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

// UnionFind高速
type UnionFind struct {
	d []int
	v []int
}

func newUnionFind(N int) *UnionFind {
	u := new(UnionFind)
	u.d = make([]int, N)
	u.v = make([]int, N)
	for i := 0; i < N; i++ {
		u.d[i] = -1
	}
	return u
}

func (p *UnionFind) root(x int) int {
	if p.d[x] < 0 {
		return x
	}
	//p.d[x] = p.root(p.d[x])
	return p.root(p.d[x])
}

func (p *UnionFind) unite(x, y int) bool {
	x = p.root(x)
	y = p.root(y)
	if x == y {
		return false
	}
	if p.d[x] > p.d[y] {
		x, y = y, x
	}
	p.d[x] += p.d[y]
	p.d[y] = x
	p.v[y] -= p.v[x]
	return true
}

func (p *UnionFind) same(x, y int) bool {
	return p.root(x) == p.root(y)
}

func (p *UnionFind) size(x int) int {
	return -p.d[p.root(x)]
}

func (p *UnionFind) add(x, v int) {
	x = p.root(x)
	p.v[x] += v
}

func (p *UnionFind) getvalue(x int) int {
	if p.d[x] < 0 {
		return p.v[x]
	}
	ret := p.getvalue(p.d[x]) + p.v[x]
	return ret
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N, Q := getI(), getI()
	uf := newUnionFind(N + 1)
	for i := 0; i < Q; i++ {
		T, a, b := getI(), getI(), getI()
		switch T {
		case 1:
			uf.unite(a, b)
		case 2:
			uf.add(a, b)
		case 3:
			out(uf.getvalue(a))
		}
	}
}
0