結果

問題 No.416 旅行会社
ユーザー fmhrfmhr
提出日時 2016-08-27 05:33:25
言語 Go
(1.22.1)
結果
AC  
実行時間 273 ms / 4,000 ms
コード長 2,826 bytes
コンパイル時間 14,717 ms
コンパイル使用メモリ 202,296 KB
実行使用メモリ 29,088 KB
最終ジャッジ日時 2023-08-21 09:49:08
合計ジャッジ時間 19,586 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 204 ms
14,292 KB
testcase_01 AC 1 ms
4,336 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,332 KB
testcase_04 AC 2 ms
4,328 KB
testcase_05 AC 1 ms
4,332 KB
testcase_06 AC 1 ms
4,332 KB
testcase_07 AC 3 ms
4,332 KB
testcase_08 AC 5 ms
4,332 KB
testcase_09 AC 23 ms
5,552 KB
testcase_10 AC 203 ms
16,356 KB
testcase_11 AC 198 ms
16,352 KB
testcase_12 AC 201 ms
16,392 KB
testcase_13 AC 196 ms
14,284 KB
testcase_14 AC 266 ms
27,064 KB
testcase_15 AC 270 ms
27,008 KB
testcase_16 AC 264 ms
26,980 KB
testcase_17 AC 266 ms
27,004 KB
testcase_18 AC 273 ms
29,088 KB
testcase_19 AC 228 ms
20,840 KB
testcase_20 AC 231 ms
20,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	m := nextInt()
	q := nextInt()
	bridge := make([]nodes, m)
	for i := 0; i < m; i++ {
		bridge[i].a = nextInt()
		bridge[i].b = nextInt()
	}
	crashed := make(map[int]bool)
	crash := make([]nodes, q)

	for i := 0; i < q; i++ {
		crash[i].a = nextInt()
		crash[i].b = nextInt()
		crashed[crash[i].a*1000000+crash[i].b] = true
	}

	uf := makeUnionFind(n + 1)
	// 壊れない橋を追加する
	for i := 0; i < m; i++ {
		if crashed[bridge[i].a*1000000+bridge[i].b] {
			continue
		}
		//fmt.Println("追加1: ", bridge[i].a, bridge[i].b)
		uf.Unit(bridge[i].a, bridge[i].b)
	}
	ans := make([]int, n+1)
	for i:=2; i<n+1; i++ {
		if uf.isConnect(1, i) {
			ans[i] = -2
		}else {
			ans[i] = -1
		}
	}
	//fmt.Println("-1ケース, ", ans)
	// 壊れる橋を逆順に追加しながら、つながっているかチェックする
	for i := q - 1; i >= 0; i-- {
		//fmt.Println("追加逆順1:",i,crash[i].a, crash[i].b)


		x := crash[i].a
		y := crash[i].b
		if uf.isConnect(x, y) {continue}
		if uf.isConnect(1, uf.Find(y)) {
			x, y = y, x
		}
		if uf.isConnect(1, x){
			for _, h := range(uf.has[uf.Find(y)]){
				if ans[h]==-1 {
					ans[h] = i
				}
			}
		}

		uf.Unit(crash[i].a, crash[i].b)
		//for _, h := range(uf.has[uf.Find(crash[i].a)]) {
		//	if uf.isConnect(1, h) && ans[h]==-1{
		//		ans[h] = i
		//	}
		//}
		//fmt.Println("毎回解答:", i, ans)
	}
	for i := 2; i < n+1; i++ {
		fmt.Println(ans[i]+1)
	}
}

type nodes struct {
	a, b int
}

type UnionFind struct {
	par    []int // 親
	weight []int // 重み
	has [][]int //
}

func makeUnionFind(count int) *UnionFind {
	par := make([]int, count)
	weight := make([]int, count)
	has := makeDoubleSliceInt(count, 1)
	for i := 0; i < count; i++ {
		par[i] = i
		weight[i] = 1
		has[i][0] = i
	}
	return &UnionFind{par: par, weight: weight, has: has}
}

func (uf *UnionFind) Find(a int) int {
	for uf.par[a] != a {
		uf.par[a] = uf.par[uf.par[a]]
		a = uf.par[a]
	}
	return a
}

func (uf *UnionFind) Unit(a, b int) {
	a = uf.Find(a)
	b = uf.Find(b)
	if a == b {
		return
	}
	if uf.weight[a] > uf.weight[b] {
		a, b = b, a
	}
	uf.has[b] = append(uf.has[uf.Find(b)], uf.has[uf.Find(a)]...)
	uf.par[a] = b
	uf.weight[b] += uf.weight[a]
}


func (uf *UnionFind) isConnect(a, b int) bool {
	return uf.Find(a) == uf.Find(b)
}

func makeDoubleSliceInt(y, x int) (r [][]int) {
	r = make([][]int, y)
	for i := range r {
		r[i] = make([]int, x)
	}
	return
}

func printDoubleSliceInt(z [][]int) {
	for i := range z {
		for j := range z[i] {
			fmt.Print(z[i][j], " ")
		}
		fmt.Println("")
	}
}

var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}
0