Language: C++

package main

import (
    "fmt"
    "os"
    "sort"
)

// {{{
// source: https://github.com/raahii/algo
func MaxInts(nums ...int) int {
    var maxVal int
    for i, e := range nums {
        if i == 0 || e > maxVal {
            maxVal = e
        }
    }
    return maxVal
}

func MaxIntsWithIdx(nums ...int) (int, int) {
    var maxIdx, maxVal int
    for i, e := range nums {
        if i == 0 || e > maxVal {
            maxIdx = i
            maxVal = e
        }
    }
    return maxVal, maxIdx
}

func MinInts(nums ...int) int {
    var minVal int
    for i, e := range nums {
        if i == 0 || e < minVal {
            minVal = e
        }
    }
    return minVal
}

func MinIntsWithIdx(nums ...int) (int, int) {
    var minIdx, minVal int
    for i, e := range nums {
        if i == 0 || e < minVal {
            minIdx = i
            minVal = e
        }
    }
    return minVal, minIdx
}

func SumInts(nums []int) int {
    s := 0
    for _, v := range nums {
        s += v
    }
    return s
}

func SumInts2d(mat [][]int) int {
    s := 0
    for _, nums := range mat {
        for _, v := range nums {
            s += v
        }
    }
    return s
}

func ReverseInts(nums []int) []int {
    rnums := make([]int, len(nums))
    for i := 0; i < len(nums); i++ {
        rnums[len(nums)-1-i] = nums[i]
    }
    return rnums
}

func ExtendInts(nums1 []int, nums2 []int) []int {
    for _, v := range nums2 {
        nums1 = append(nums1, v)
    }
    return nums1
}

func Ints2d(n_rows, n_cols int) [][]int {
    ints := make([][]int, n_rows)
    for i := 0; i < n_rows; i++ {
        ints[i] = make([]int, n_cols)
    }
    return ints
}

func StrInts2d(mat [][]int) string {
    str := ""
    for i := 0; i < len(mat); i++ {
        str += fmt.Sprintf("[%d", mat[i][0])
        for j := 1; j < len(mat[0]); j++ {
            str += fmt.Sprintf(" %d", mat[i][j])
        }
        str += "]"
        if i != len(mat)-1 {
            str += "\n"
        }
    }
    return str
}

func MemsetInts1d(nums []int, val int) {
    for i := range nums {
        nums[i] = val
    }
}

func MemsetInts2d(nums [][]int, val int) {
    for i := range nums {
        for j := range nums[i] {
            nums[i][j] = val
        }
    }
}

func ContainsInt(nums []int, num int) bool {
    sort.Ints(nums)

    s, e := 0, len(nums)
    for {
        if s >= e {
            break
        }

        m := (e + s) / 2
        if nums[m] == num {
            return true
        } else if num > nums[m] {
            s = m + 1
        } else {
            e = m
        }
    }

    return false
}

func ReadInt(f *os.File) int {
    var n int
    fmt.Fscanf(f, "%d", &n)
    return n
}

func ReadInts(f *os.File, n int) []int {
    nums := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Fscanf(f, "%d", &nums[i])
    }
    return nums
}

func ScanInts(f *os.File, vars ...*int) {
    for i := 0; i < len(vars); i++ {
        fmt.Fscanf(f, "%d", vars[i])
    }
}

func ReadLine(f *os.File) string {
    var str string
    fmt.Fscanf(f, "%s", &str)

    return str
}

func ReadLines(f *os.File, n int) []string {
    lines := make([]string, n)
    for i := 0; i < n; i++ {
        fmt.Fscanf(f, "%s", &lines[i])
    }

    return lines
}

func ReadWords(f *os.File, n int) []string {
    strs := make([]string, n)
    for i := 0; i < n; i++ {
        fmt.Fscanf(f, "%s", &strs[i])
    }
    return strs
}

func Pow(a, n int) int {
    c := 1
    for i := 0; i < n; i++ {
        c *= a
    }
    return c
}

func Abs(n int) int {
    if n < 0 {
        return -n
    }

    return n
}

// }}}

func solve(pairs []Pair, n int) {
    m := len(pairs)
    answers := make([]int, m)
    answers[0] = n * (n - 1) / 2

    links := Ints2d(n, n)
    for i := 0; i < n; i++ {
        links[i][i] = 1
    }

    for i := 0; i < m-1; i++ {
        from, to := pairs[i].First, pairs[i].Second
        if links[from][to] == 1 {
            answers[i+1] = answers[i]
        } else {
            answers[i+1] = answers[i] - SumInts(links[from])*SumInts(links[to])
            for j := 0; j < n; j++ {
                if links[from][j] == 1 {
                    links[to][j] = 1;
                }
                if links[to][j] == 1 {
                    links[from][j] = 1;
                }
            }
        }
    }

    for _, v := range ReverseInts(answers) {
        fmt.Println(v)
    }
}

type Pair struct {
    First  int
    Second int
}

func reverse(pairs []Pair) []Pair {
    n := len(pairs)
    newPairs := make([]Pair, n)
    for i, v := range pairs {
        newPairs[n-i-1] = v
    }
    return newPairs
}

func main() {
    var n, m int
    fmt.Scan(&n, &m)

    pairs := make([]Pair, m)
    var from, to int
    for i := 0; i < m; i++ {
        fmt.Scan(&from, &to)
        pairs[i] = Pair{from - 1, to - 1}
    }

    solve(reverse(pairs), n)
}


Compiler output:
a.cc:1:1: error: 'package' does not name a type
    1 | package main
      | ^~~~~~~
a.cc:21:1: error: 'func' does not name a type
   21 | func MaxIntsWithIdx(nums ...int) (int, int) {
      | ^~~~
a.cc:32:1: error: 'func' does not name a type
   32 | func MinInts(nums ...int) int {
      | ^~~~
a.cc:42:1: error: 'func' does not name a type
   42 | func MinIntsWithIdx(nums ...int) (int, int) {
      | ^~~~
a.cc:53:1: error: 'func' does not name a type
   53 | func SumInts(nums []int) int {
      | ^~~~
a.cc:61:1: error: 'func' does not name a type
   61 | func SumInts2d(mat [][]int) int {
      | ^~~~
a.cc:71:1: error: 'func' does not name a type
   71 | func ReverseInts(nums []int) []int {
      | ^~~~
a.cc:79:1: error: 'func' does not name a type
   79 | func ExtendInts(nums1 []int, nums2 []int) []int {
      | ^~~~
a.cc:86:1: error: 'func' does not name a type
   86 | func Ints2d(n_rows, n_cols int) [][]int {
      | ^~~~
a.cc:94:1: error: 'func' does not name a type
   94 | func StrInts2d(mat [][]int) string {
      | ^~~~
a.cc:109:1: error: 'func' does not name a type
  109 | func MemsetInts1d(nums []int, val int) {
      | ^~~~
a.cc:115:1: error: 'func' does not name a type
  115 | func MemsetInts2d(nums [][]int, val int) {
      | ^~~~
a.cc:123:1: error: 'func' does not name a type
  123 | func ContainsInt(nums []int, num int) bool {
      | ^~~~
a.cc:145:1: error: 'func' does not name a type
  145 | func ReadInt(f *os.File) int {
      | ^~~~
a.cc:151:1: error: 'func' does not name a type
  151 | func ReadInts(f *os.File, n int) []int {
      | ^~~~
a.cc:159:1: error: 'func' does not name a type
  159 | func ScanInts(f *os.File, vars ...*int) {
      | ^~~~
a.cc:165:1: error: 'func' does not name a type
  165 | func ReadLine(f *os.File) string {
      | ^~~~
a.cc:172:1: error: 'func' does not name a type
  172 | func ReadLines(f *os.File, n int) []string {
      | ^~~~
a.cc:181:1: error: 'func' does not name a type
  181 | func ReadWords(f *os.File, n int) []string {
      | ^~~~
a.cc:189:1: error: 'func' does not name a type
  189 | func Pow(a, n int) int {
      | ^~~~
a.cc:197:1: error: 'func' does not name a type
  197 | func Abs(n int) int {
      | ^~~~
a.cc:207:1: error: 'func' does not name a type
  207 | func solve(pairs []Pair, n int) {
      | ^~~~
a.cc:239:1: error: 'type' does not name a type; did you mean 'typeof'?
  239 | type Pair struct {
      | ^~~~
      | typeof
a.cc:244:1: error: 'func' does not name a type
  244 | func reverse(pairs []Pair) []Pair {
      | ^~~~
a.cc:253:1: error: 'func' does not name a type
  253 | func main() {
      | ^~~~