結果

問題 No.416 旅行会社
ユーザー fmhrfmhr
提出日時 2016-08-27 04:13:53
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,656 bytes
コンパイル時間 11,196 ms
コンパイル使用メモリ 239,948 KB
実行使用メモリ 12,476 KB
最終ジャッジ日時 2024-04-26 05:59:31
合計ジャッジ時間 14,891 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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() - 1
		bridge[i].b = nextInt() - 1
	}
	crashed := make(map[int]bool)
	crash := make([]nodes, q)

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

	//uf := makeUnionFind(n)
	//// 壊れない橋を追加する
	//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)
	//// すべて壊してもつながっている島
	//for i:=1; i<n; i++ {
	//	if uf.isConnect(1, i) {
	//		ans[i] = -1
	//	}
	//}
	////fmt.Println("-1ケース, ", ans)
	//// 壊れる橋を逆順に追加しながら、つながっているかチェックする
	//for i := q - 1; i >= 0; i-- {
	//	//fmt.Println("追加逆順1:",i,crash[i].a, crash[i].b)
	//	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]==0{
	//			ans[h] = i + 1
	//		}
	//	}
	//	//fmt.Println("毎回解答:", i, ans)
	//}
	for i := 1; i < n; i++ {
		fmt.Println(ans[i])
	}
}

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