Language: Java

class Bridge {
    int start, end;

    public Bridge(int start, int end) {
        this.start = start;
        this.end = end;
    }

    public Set<Integer> toSet() {
        Set<Integer> s = new HashSet<>();
        s.add(this.start);
        s.add(this.end);
        return s;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        ArrayDeque<Bridge> stack = new ArrayDeque<>();
        ArrayDeque<Integer> result = new ArrayDeque<>();

        int initNum = n * (n - 1) / 2;
        result.push(initNum);

        for (int i = 0; i < m; i++) {
            stack.push(new Bridge(sc.nextInt(), sc.nextInt()));
        }

        LinkedList<Set<Integer>> islands = new LinkedList<>();
        Set<Integer> islandNums = new HashSet<>();

        while (stack.size() != 1) {
            Bridge b = stack.pop();
            int tmpSum = 0;
            Set<Integer> tmp = new HashSet<>();
            if (!islandNums.contains(b.start) && !islandNums.contains(b.end)) {
                islandNums.addAll(b.toSet());
                islands.add(b.toSet());
            } else {
                Set<Set<Integer>> removeList = new HashSet<>();
                for (Set<Integer> s : islands) {
                    if (s.contains(b.start) || s.contains(b.end)) {
                        tmp.addAll(s);
                        removeList.add(s);
                    }
                }
                islands.removeAll(removeList);
                tmp.addAll(b.toSet());
                islands.add(tmp);
            }

            for (Set<Integer> s : islands) {
                int ss = s.size();
                tmpSum += ss * (ss - 1) / 2;
            }
            result.push(Math.max(initNum - tmpSum, 0));
        }

        for (int i : result) {
            System.out.println(i);
        }
    }
}


Compiler output:
Main.java:9: error: cannot find symbol
    public Set<Integer> toSet() {
           ^
  symbol:   class Set
  location: class Bridge
Main.java:10: error: cannot find symbol
        Set<Integer> s = new HashSet<>();
        ^
  symbol:   class Set
  location: class Bridge
Main.java:10: error: cannot find symbol
        Set<Integer> s = new HashSet<>();
                             ^
  symbol:   class HashSet
  location: class Bridge
Main.java:19: error: cannot find symbol
        Scanner sc = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:19: error: cannot find symbol
        Scanner sc = new Scanner(System.in);
                         ^
  symbol:   class Scanner
  location: class Main
Main.java:23: error: cannot find symbol
        ArrayDeque<Bridge> stack = new ArrayDeque<>();
        ^
  symbol:   class ArrayDeque
  location: class Main
Main.java:23: error: cannot find symbol
        ArrayDeque<Bridge> stack = new ArrayDeque<>();
                                       ^
  symbol:   class ArrayDeque
  location: class Main
Main.java:24: error: cannot find symbol
        ArrayDeque<Integer> result = new ArrayDeque<>();
        ^
  symbol:   class ArrayDeque
  location: class Main
Main.java:24: error: cannot find symbol
        ArrayDeque<Integer> result = new ArrayDeque<>();
                                         ^
  symbol:   class ArrayDeque
  location: class Main
Main.java:33: error: cannot find symbol
        LinkedList<Set<Integer>> islands = new LinkedList<>();
        ^
  symbol:   class LinkedList
  location: class Main
Main.java:33: error: cannot find symbol
        LinkedList<Set<Integer>> islands = new LinkedList<>();
                   ^
  symbol:   class Set
  location: class Main
Main.java:33: error: cannot find symbol
        LinkedList<Set<Integer>> islands = new LinkedList<>();
                                               ^
  symbol:   class LinkedList
  location: class Main
Main.java:34: error: cannot find symbol
        Set<Integer> islandNums = new HashSet<>();
        ^
  symbol:   class Set
  location: class Main
Main.java:34: error: cannot find symbol
        Set<Integer> islandNums = new HashSet<>();
                                      ^
  symbol:   class HashSet
  location: class Main
Main.java:39: error: cannot find symbol
            Set<Integer> tmp = new HashSet<>();
            ^
  symbol:   class Set
  location: class Main
Main.java:39: error: cannot find symbol
            Set<Integer> tmp = new HashSet<>();
                                   ^
  symbol:   class HashSet
  location: class Main
Main.java:44: error: cannot find symbol
                Set<Set<Integer>> removeList = new HashSet<>();
                ^
  symbol:   class Set
  location: class Main
Main.java:44: error: cannot find symbol
                Set<Set<Integer>> removeList = new HashSet<>();
                    ^
  symbol:   class Set
  location: class Main
Main.java:44: error: cannot find symbol
                Set<Set<Integer>> removeList = new HashSet<>();
                                                   ^
  symbol:   class HashSet
  location: class Main
Main.java:45: error: cannot find symbol
                for (Set<Integer> s : islands) {
                     ^
  symbol:   class Set
  location: class Main
Main.java:56: error: cannot find symbol
            for (Set<Integer> s : islands) {
                 ^
  symbol:   class Set
  location: class Main
21 errors