Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "sort"8)9 1011func CF243B(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 type edge struct{ v, w int }16 17 var n, m, h, t, v, w int18 Fscan(in, &n, &m, &h, &t)19 g := make([][]int, n+1)20 edges := make([]edge, m)21 for i := range edges {22 Fscan(in, &v, &w)23 g[v] = append(g[v], w)24 g[w] = append(g[w], v)25 edges[i] = edge{v, w}26 }27 for i := range g {28 sort.Ints(g[i])29 }30 for _, e := range edges {31 v, w := e.v, e.w32 vs, ws := g[v], g[w]33 if h < t && len(vs) > len(ws) || h > t && len(vs) < len(ws) {34 vs, ws = ws, vs35 v, w = w, v36 }37 if len(vs)-1 < h || len(ws)-1 < t {38 continue39 }40 var hs, ts, same []interface{}41 i, n := 0, len(vs)42 j, m := 0, len(ws)43 for i < n || j < m {44 if i == n {45 if y := ws[j]; y != v {46 ts = append(ts, y)47 }48 j++49 } else if j == m {50 if x := vs[i]; x != w {51 hs = append(hs, x)52 }53 i++54 } else {55 x, y := vs[i], ws[j]56 if x == w {57 i++58 continue59 }60 if y == v {61 j++62 continue63 }64 if x < y {65 if len(hs) < h {66 hs = append(hs, x)67 }68 i++69 } else if x > y {70 if len(ts) < t {71 ts = append(ts, y)72 }73 j++74 } else {75 same = append(same, x)76 i++77 j++78 }79 }80 if len(hs)+len(ts)+len(same) >= h+t {81 Fprintln(out, "YES")82 Fprintln(out, v, w)83 if len(hs) < h {84 popL := same[:h-len(hs)]85 same = same[h-len(hs):]86 hs = append(hs, popL...)87 }88 Fprintln(out, hs...)89 if len(ts) < t {90 ts = append(ts, same[:t-len(ts)]...)91 }92 Fprintln(out, ts...)93 return94 }95 }96 }97 Fprint(out, "NO")98}99 100101