Approach
Stack-based processing
For Codeforces 715B — Complete The Graph, 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
- 100 lines of Go from the credited upstream file 715B.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 pair15 struct {12 v int13 d int6414}15type hp15 []pair1516 17func (h hp15) Len() int { return len(h) }18func (h hp15) Less(i, j int) bool { return h[i].d < h[j].d }19func (h hp15) Swap(i, j int) { h[i], h[j] = h[j], h[i] }20func (h *hp15) Push(v interface{}) { *h = append(*h, v.(pair15)) }21func (h *hp15) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }22func (h *hp15) push(v pair15) { heap.Push(h, v) }23func (h *hp15) pop() pair15 { return heap.Pop(h).(pair15) }24 25func CF715B(_r io.Reader, _w io.Writer) {26 in := bufio.NewReader(_r)27 out := bufio.NewWriter(_w)28 defer out.Flush()29 30 var n, m, s, t, v, w int31 var l, wt, diff int6432 Fscan(in, &n, &m, &l, &s, &t)33 type nb struct {34 to, rid int35 wt int6436 del bool37 }38 g := make([][]nb, n)39 for ; m > 0; m-- {40 Fscan(in, &v, &w, &wt)41 del := wt == 042 if del {43 wt = 144 }45 g[v] = append(g[v], nb{w, len(g[w]), wt, del})46 g[w] = append(g[w], nb{v, len(g[v]) - 1, wt, del})47 }48 49 dis := make([][2]int64, n)50 for i := range dis {51 dis[i] = [2]int64{1e18, 1e18}52 }53 dis[s] = [2]int64{}54 dij := func(k int) {55 h := hp15{{s, 0}}56 for len(h) > 0 {57 p := h.pop()58 v := p.v59 if dis[v][k] < p.d {60 continue61 }62 for i, e := range g[v] {63 w, wt := e.to, e.wt64 if e.del && k > 0 {65 if newD := dis[w][0] + diff - dis[v][1]; newD > wt {66 wt = newD67 g[v][i].wt = newD68 g[w][e.rid].wt = newD69 }70 }71 if newD := dis[v][k] + wt; newD < dis[w][k] {72 dis[w][k] = newD73 h.push(pair15{w, newD})74 }75 }76 }77 }78 dij(0)79 diff = l - dis[t][0]80 if diff < 0 {81 Fprint(out, "NO")82 return83 }84 dij(1)85 if dis[t][1] < l {86 Fprint(out, "NO")87 return88 }89 Fprintln(out, "YES")90 for v, es := range g {91 for _, e := range es {92 if e.to > v {93 Fprintln(out, v, e.to, e.wt)94 }95 }96 }97}98 99100