結果
問題 | No.1054 Union add query |
ユーザー | c-yan |
提出日時 | 2020-05-16 00:28:05 |
言語 | Go (1.22.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,125 bytes |
コンパイル時間 | 16,091 ms |
コンパイル使用メモリ | 241,780 KB |
実行使用メモリ | 66,952 KB |
最終ジャッジ日時 | 2024-09-19 15:20:50 |
合計ジャッジ時間 | 18,037 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,624 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | TLE | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
ソースコード
package main import ( "bufio" "fmt" "os" "strconv" ) func find(parent []int, i int) int { if parent[i] < 0 { return i } parent[i] = find(parent, parent[i]) return parent[i] } func unite(parent []int, i, j int) { i = find(parent, i) j = find(parent, j) if i == j { return } parent[j] += parent[i] parent[i] = j } func main() { defer flush() N := readInt() Q := readInt() T := make([]int, Q+1) A := make([]int, Q+1) B := make([]int, Q+1) for i := 0; i < Q; i++ { T[i] = readInt() A[i] = readInt() B[i] = readInt() } t := map[int]int{} for i := 0; i < Q; i++ { if T[i] == 3 { t[A[i]] = 0 } } u := map[int][]int{} acc := map[int]int{} for k := range t { u[k] = []int{k} acc[k] = 0 } parent := make([]int, N+1) for i := 0; i < N+1; i++ { parent[i] = -1 } for i := 0; i < Q; i++ { if T[i] == 1 { a := find(parent, A[i]) b := find(parent, B[i]) if a == b { continue } as := u[a] bs := u[b] if acc[a] != 0 { for _, v := range as { t[v] += acc[a] } acc[a] = 0 } if acc[b] != 0 { for _, v := range bs { t[v] += acc[b] } acc[b] = 0 } delete(u, a) delete(u, b) unite(parent, A[i], B[i]) u[find(parent, A[i])] = append(as, bs...) } else if T[i] == 2 { acc[find(parent, A[i])] += B[i] } else if T[i] == 3 { println(t[A[i]] + acc[find(parent, A[i])]) } } } const ( ioBufferSize = 1 * 1024 * 1024 // 1 MB ) var stdinScanner = func() *bufio.Scanner { result := bufio.NewScanner(os.Stdin) result.Buffer(make([]byte, ioBufferSize), ioBufferSize) result.Split(bufio.ScanWords) return result }() func readString() string { stdinScanner.Scan() return stdinScanner.Text() } func readInt() int { result, err := strconv.Atoi(readString()) if err != nil { panic(err) } return result } var stdoutWriter = bufio.NewWriter(os.Stdout) func flush() { stdoutWriter.Flush() } func printf(f string, args ...interface{}) (int, error) { return fmt.Fprintf(stdoutWriter, f, args...) } func println(args ...interface{}) (int, error) { return fmt.Fprintln(stdoutWriter, args...) }