Approach
Stack-based processing
For Codeforces 449B — Jzzhu and Cities, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 66 lines of Go from the credited upstream file 449B.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 "container/heap"6 . "fmt"7 "io"8)9 1011type pr49 struct {12 v int13 d int6414 isT bool15}16type hp49 []pr4917 18func (h hp49) Len() int { return len(h) }19func (h hp49) Less(i, j int) bool { return h[i].d < h[j].d || h[i].d == h[j].d && !h[i].isT } 20func (h hp49) Swap(i, j int) { h[i], h[j] = h[j], h[i] }21func (h *hp49) Push(v interface{}) { *h = append(*h, v.(pr49)) }22func (h *hp49) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }23func (h *hp49) push(v pr49) { heap.Push(h, v) }24func (h *hp49) pop() pr49 { return heap.Pop(h).(pr49) }25 26func CF449B(_r io.Reader, out io.Writer) {27 in := bufio.NewReader(_r)28 var n, m, k, v, w, wt, ans int29 Fscan(in, &n, &m, &k)30 type nb struct {31 to, wt int32 isT bool33 }34 g := make([][]nb, n)35 for ; m > 0; m-- {36 Fscan(in, &v, &w, &wt)37 v--38 w--39 g[v] = append(g[v], nb{w, wt, false})40 g[w] = append(g[w], nb{v, wt, false})41 }42 for ; k > 0; k-- {43 Fscan(in, &w, &wt)44 g[0] = append(g[0], nb{w - 1, wt, true})45 }46 vis := make([]bool, n)47 q := hp49{{}}48 for len(q) > 0 {49 p := q.pop()50 v := p.v51 if vis[v] {52 if p.isT {53 ans++54 }55 continue56 }57 vis[v] = true58 for _, e := range g[v] {59 q.push(pr49{e.to, p.d + int64(e.wt), e.isT})60 }61 }62 Fprint(out, ans)63}64 6566