Skip to content

Instantly share code, notes, and snippets.

@gitschaub
Created August 18, 2016 03:10
Show Gist options
  • Save gitschaub/1c65a75fe20ef0d8b77a6341d16cdd90 to your computer and use it in GitHub Desktop.
Save gitschaub/1c65a75fe20ef0d8b77a6341d16cdd90 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"bufio"
"os"
"strconv"
"strings"
)
type Node struct {
ID int
Degree int
Neighbors []*Node
}
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
in := bufio.NewScanner(os.Stdin)
in.Scan()
tcs, _ := strconv.Atoi(in.Text())
for tc := 0; tc < tcs; tc++ {
in.Scan()
nCities, _ := strconv.Atoi(in.Text())
cities := make([]Node, nCities)
for r := 0; r < nCities-1; r++ {
in.Scan()
es := strings.Split(in.Text(), " ")
c1, _ := strconv.Atoi(es[0])
c2, _ := strconv.Atoi(es[1])
// Append to each other's neighbors, add one to each node's degree
cities[c1].Neighbors = append(cities[c1].Neighbors, &(cities[c2]))
cities[c2].Neighbors = append(cities[c2].Neighbors, &(cities[c1]))
cities[c1].Degree++
cities[c2].Degree++
}
probEdges := 0
for _, c := range cities {
if c.Degree == 1 {
for _, nb := range c.Neighbors {
if nb.Degree == 2 {
probEdges++
}
}
}
}
numBots := probEdges/2
if probEdges % 2 == 1 {
numBots += 1
}
if numBots == 0 {
numBots = 1
}
fmt.Println(numBots)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment