Submission #1690854


Source Code Expand

package main
 
import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
 
const MAX_N = 100010
 
type key struct {
	a, b int
}
 
var edges [MAX_N][]int
var cost map[key]int
var N, Q, K int
var result map[key]int64
 
func dfs(pos, pre int, sum int64) {
	result[key{K, pos}] = sum
	result[key{pos, K}] = sum
	for _, next := range edges[pos] {
		if next == pre {
			continue
		}
		dfs(next, pos, sum+int64(cost[key{pos, next}]))
	}
 
}
func main() {
	fsc := NewFastScanner()
	N = fsc.NextInt()
	cost = make(map[key]int)
	result = make(map[key]int64)
	for i := 0; i < N-1; i++ {
		a, b, c := fsc.NextInt(), fsc.NextInt(), fsc.NextInt()
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
		cost[key{a, b}] = c
		cost[key{b, a}] = c
	}
	Q, K = fsc.NextInt(), fsc.NextInt()
	K--
	dfs(K, -1, 0)
	for i := 0; i < Q; i++ {
		a, b := fsc.NextInt(), fsc.NextInt()
		a--
		b--
		fmt.Println(result[key{a, K}] + result[key{K, b}])
	}
}
 
//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}
 
func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}
 
func (s *FastScanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *FastScanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
 
//Max,Min
func IntMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}
 
func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}
 
func IntMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}
 
func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}
 
//Gcd
func IntGcd(a, b int) int {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return IntGcd(b, a%b)
}
func Int64Gcd(a, b int64) int64 {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return Int64Gcd(b, a%b)
}
 
func IntAbs(a int) int {
	if a < 0 {
		a *= -1
	}
	return a
}
 
func Int64Abs(a int64) int64 {
	if a < 0 {
		a *= -1
	}
	return a
}

Submission Info

Submission Time
Task D - Transit Tree Path
User togatoga
Language Go (1.6)
Score 400
Code Size 2896 Byte
Status AC
Exec Time 577 ms
Memory 61696 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 31
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 640 KB
sample_02.txt AC 1 ms 640 KB
sample_03.txt AC 1 ms 640 KB
subtask_1_1.txt AC 2 ms 640 KB
subtask_1_10.txt AC 190 ms 2944 KB
subtask_1_11.txt AC 3 ms 640 KB
subtask_1_12.txt AC 18 ms 896 KB
subtask_1_13.txt AC 62 ms 1408 KB
subtask_1_14.txt AC 1 ms 640 KB
subtask_1_15.txt AC 11 ms 3200 KB
subtask_1_16.txt AC 532 ms 33664 KB
subtask_1_17.txt AC 533 ms 34944 KB
subtask_1_18.txt AC 539 ms 33536 KB
subtask_1_19.txt AC 523 ms 34816 KB
subtask_1_2.txt AC 4 ms 1280 KB
subtask_1_20.txt AC 568 ms 59648 KB
subtask_1_21.txt AC 577 ms 61696 KB
subtask_1_22.txt AC 562 ms 59904 KB
subtask_1_23.txt AC 540 ms 44416 KB
subtask_1_24.txt AC 485 ms 32000 KB
subtask_1_25.txt AC 483 ms 32128 KB
subtask_1_26.txt AC 497 ms 33152 KB
subtask_1_27.txt AC 486 ms 32000 KB
subtask_1_28.txt AC 534 ms 60672 KB
subtask_1_3.txt AC 323 ms 28672 KB
subtask_1_4.txt AC 2 ms 640 KB
subtask_1_5.txt AC 250 ms 4096 KB
subtask_1_6.txt AC 50 ms 5632 KB
subtask_1_7.txt AC 212 ms 3584 KB
subtask_1_8.txt AC 2 ms 640 KB
subtask_1_9.txt AC 276 ms 32512 KB