-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
WangSenyuan
committed
Jun 9, 2024
1 parent
07b1d45
commit d301524
Showing
15 changed files
with
835 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Ron and Hermione are playing a game on a tree of 𝑛 | ||
nodes that are initially inactive. This tree is special because it has exactly two leaves. It can thus be seen as an array. The game consists of 𝑡 | ||
rounds, each of which starts with a stone on exactly one node, which is considered as activated. A move consists of picking an inactive neighbor of the node with a stone on it and moving the stone there (thus activating this neighbor). Ron makes the first move, after which he alternates with Hermione until no valid move is available. The player that cannot make a move loses the round. If both players play optimally, who wins each round of this game? | ||
|
||
Note that all the rounds are played with the same tree; only the starting node changes. Moreover, after each round, all active nodes are considered inactive again. | ||
|
164 changes: 164 additions & 0 deletions
164
src/codeforces/set1/set19/set197/set1970/c1/solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
|
||
n, m := readTwoNums(reader) | ||
edges := make([][]int, n-1) | ||
for i := 0; i < n-1; i++ { | ||
edges[i] = readNNums(reader, 2) | ||
} | ||
|
||
rounds := make([]int, m) | ||
for i := 0; i < m; i++ { | ||
rounds[i] = readNum(reader) | ||
} | ||
ans := solve(n, edges, rounds) | ||
|
||
var buf bytes.Buffer | ||
|
||
for _, x := range ans { | ||
buf.WriteString(x) | ||
buf.WriteByte('\n') | ||
} | ||
|
||
fmt.Print(buf.String()) | ||
} | ||
func readString(reader *bufio.Reader) string { | ||
s, _ := reader.ReadString('\n') | ||
for i := 0; i < len(s); i++ { | ||
if s[i] == '\n' || s[i] == '\r' { | ||
return s[:i] | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func readInt(bytes []byte, from int, val *int) int { | ||
i := from | ||
sign := 1 | ||
if bytes[i] == '-' { | ||
sign = -1 | ||
i++ | ||
} | ||
tmp := 0 | ||
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { | ||
tmp = tmp*10 + int(bytes[i]-'0') | ||
i++ | ||
} | ||
*val = tmp * sign | ||
return i | ||
} | ||
|
||
func readNum(reader *bufio.Reader) (a int) { | ||
bs, _ := reader.ReadBytes('\n') | ||
readInt(bs, 0, &a) | ||
return | ||
} | ||
|
||
func readTwoNums(reader *bufio.Reader) (a int, b int) { | ||
res := readNNums(reader, 2) | ||
a, b = res[0], res[1] | ||
return | ||
} | ||
|
||
func readThreeNums(reader *bufio.Reader) (a int, b int, c int) { | ||
res := readNNums(reader, 3) | ||
a, b, c = res[0], res[1], res[2] | ||
return | ||
} | ||
|
||
func readNNums(reader *bufio.Reader, n int) []int { | ||
res := make([]int, n) | ||
x := 0 | ||
bs, _ := reader.ReadBytes('\n') | ||
for i := 0; i < n; i++ { | ||
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' { | ||
x++ | ||
} | ||
x = readInt(bs, x, &res[i]) | ||
} | ||
return res | ||
} | ||
|
||
const Ron = "Ron" | ||
const Hermione = "Hermione" | ||
|
||
func solve(n int, edges [][]int, rounds []int) []string { | ||
deg := make([]int, n) | ||
g := NewGraph(n, n*2) | ||
for _, cur := range edges { | ||
u, v := cur[0]-1, cur[1]-1 | ||
deg[u]++ | ||
deg[v]++ | ||
g.AddEdge(u, v) | ||
g.AddEdge(v, u) | ||
} | ||
|
||
first := 0 | ||
for i := 0; i < n; i++ { | ||
if deg[i] == 1 { | ||
first = i | ||
break | ||
} | ||
} | ||
|
||
pos := make([]int, n) | ||
|
||
var dfs func(p int, u int) | ||
|
||
dfs = func(p int, u int) { | ||
for i := g.nodes[u]; i > 0; i = g.next[i] { | ||
v := g.to[i] | ||
if p != v { | ||
pos[v] = pos[u] + 1 | ||
dfs(u, v) | ||
} | ||
} | ||
} | ||
|
||
dfs(-1, first) | ||
|
||
ans := make([]string, len(rounds)) | ||
|
||
for i, u := range rounds { | ||
u-- | ||
x := pos[u] | ||
y := n - 1 - x | ||
if x&1 == 1 || y&1 == 1 { | ||
ans[i] = Ron | ||
} else { | ||
ans[i] = Hermione | ||
} | ||
} | ||
|
||
return ans | ||
} | ||
|
||
type Graph struct { | ||
nodes []int | ||
next []int | ||
to []int | ||
cur int | ||
} | ||
|
||
func NewGraph(n int, e int) *Graph { | ||
nodes := make([]int, n) | ||
next := make([]int, e) | ||
to := make([]int, e) | ||
return &Graph{nodes, next, to, 0} | ||
} | ||
|
||
func (g *Graph) AddEdge(u, v int) { | ||
g.cur++ | ||
g.next[g.cur] = g.nodes[u] | ||
g.nodes[u] = g.cur | ||
g.to[g.cur] = v | ||
} |
40 changes: 40 additions & 0 deletions
40
src/codeforces/set1/set19/set197/set1970/c1/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func runSample(t *testing.T, n int, edges [][]int, rounds []int, expect []string) { | ||
res := solve(n, edges, rounds) | ||
|
||
if !reflect.DeepEqual(res, expect) { | ||
t.Fatalf("Sample expect %v, but got %v", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
n := 3 | ||
edges := [][]int{ | ||
{2, 3}, | ||
{3, 1}, | ||
} | ||
rounds := []int{3} | ||
expect := []string{Ron} | ||
|
||
runSample(t, n, edges, rounds, expect) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
n := 5 | ||
edges := [][]int{ | ||
{1, 2}, | ||
{2, 3}, | ||
{3, 4}, | ||
{4, 5}, | ||
} | ||
rounds := []int{5} | ||
expect := []string{Hermione} | ||
|
||
runSample(t, n, edges, rounds, expect) | ||
} |
Oops, something went wrong.