Language: Java

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// parameter "input" gets all data
function Equals(input) {
    // the first line is assigned to input[0], the second line is assigned to input[1] similarly.
    var _a = parseInput(input), N = _a.N, p = _a.p, xy = _a.xy;
    var sets = DisjointSets.buildFrom(N, xy);
    //output
    var count = p.filter(function (num, i) { return sets.areInSameSet(num, i + 1); }).length;
    console.log(count);
    return count;
}
exports.Equals = Equals;
var DisjointSets = /** @class */ (function () {
    function DisjointSets(trees) {
        this.trees = trees;
    }
    DisjointSets.buildFrom = function (N, xy) {
        var trees = Array.apply(this, Array(N + 1)).map(function () { return new DisjointSetTree(); });
        xy.forEach(function (_a) {
            var x = _a.x, y = _a.y;
            return trees[x].union(trees[y]);
        });
        return new DisjointSets(trees);
    };
    DisjointSets.prototype.areInSameSet = function (num1, num2) {
        return this.trees[num1].isSameSetAs(this.trees[num2]);
    };
    return DisjointSets;
}());
var DisjointSetTree = /** @class */ (function () {
    function DisjointSetTree() {
        this.parent = this;
        this.rank = 0;
    }
    DisjointSetTree.prototype.isSameSetAs = function (other) {
        return this.find() === other.find();
    };
    // union by rank
    DisjointSetTree.prototype.union = function (other) {
        var _a;
        var xRoot = this.find();
        var yRoot = other.find();
        if (xRoot !== yRoot) {
            if (xRoot.rank < yRoot.rank) {
                _a = [yRoot, xRoot], xRoot = _a[0], yRoot = _a[1];
            }
            yRoot.parent = xRoot;
            if (yRoot.rank === xRoot.rank) {
                xRoot.rank = xRoot.rank + 1;
            }
        }
    };
    DisjointSetTree.prototype.find = function () {
        if (this.parent !== this) {
            this.parent = this.parent.find();
        }
        return this.parent;
    };
    return DisjointSetTree;
}());
function parseInput(input) {
    var _a = input.split("\n"), line0 = _a[0], line1 = _a[1], lines = _a.slice(2);
    var _b = parseSpaceSeparatedInts(line0), N = _b[0], M = _b[1];
    var p = parseSpaceSeparatedInts(line1);
    var xy = lines.map(function (line) {
        var _a = parseSpaceSeparatedInts(line), x = _a[0], y = _a[1];
        return { x: x, y: y };
    });
    return { N: N, M: M, p: p, xy: xy };
}
function parseSpaceSeparatedInts(line) {
    return line.split(" ").map(Number);
}

// Don't edit this line!
Equals(require("fs").readFileSync("/dev/stdin", "utf8"));

//# sourceMappingURL=Equals.js.map

Compiler output:
Main.java:1: error: class, interface, enum, or record expected
"use strict";
^
Main.java:2: error: class, interface, enum, or record expected
Object.defineProperty(exports, "__esModule", { value: true });
^
Main.java:9: error: not a statement
    var count = p.filter(function (num, i) { return sets.areInSameSet(num, i + 1); }).length;
                                                                                      ^
Main.java:4: error: unnamed classes are a preview feature and are disabled by default.
function Equals(input) {
^
  (use --enable-preview to enable unnamed classes)
Main.java:4: error: <identifier> expected
function Equals(input) {
                     ^
Main.java:6: error: 'var' is not allowed in a compound declaration
    var _a = parseInput(input), N = _a.N, p = _a.p, xy = _a.xy;
    ^
Main.java:9: error: ')' or ',' expected
    var count = p.filter(function (num, i) { return sets.areInSameSet(num, i + 1); }).length;
                                          ^
Main.java:9: error: illegal start of expression
    var count = p.filter(function (num, i) { return sets.areInSameSet(num, i + 1); }).length;
                                                                                    ^
Main.java:13: error: class, interface, enum, or record expected
exports.Equals = Equals;
^
Main.java:14: error: ')' expected
var DisjointSets = /** @class */ (function () {
                                             ^
Main.java:17: error: class, interface, enum, or record expected
    }
    ^
Main.java:19: error: class, interface, enum, or record expected
        var trees = Array.apply(this, Array(N + 1)).map(function () { return new DisjointSetTree(); });
                                                                                                    ^
Main.java:20: error: class, interface, enum, or record expected
        xy.forEach(function (_a) {
        ^
Main.java:22: error: class, interface, enum, or record expected
            return trees[x].union(trees[y]);
            ^
Main.java:23: error: class, interface, enum, or record expected
        });
        ^
Main.java:24: error: class, interface, enum, or record expected
        return new DisjointSets(trees);
        ^
Main.java:25: error: class, interface, enum, or record expected
    };
    ^
Main.java:26: error: class, interface, enum, or record expected
    DisjointSets.prototype.areInSameSet = function (num1, num2) {
    ^
Main.java:28: error: class, interface, enum, or record expected
    };
    ^
Main.java:29: error: class, interface, enum, or record expected
    return DisjointSets;
    ^
Main.java:30: error: class, interface, enum, or record expected
}());
^
Main.java:31: error: ')' expected
var DisjointSetTree = /** @class */ (function () {
                                                ^
Main.java:34: error: class, interface, enum, or record expected
        this.rank = 0;
        ^
Main.java:35: error: class, interface, enum, or record expected
    }
    ^
Main.java:38: error: class, interface, enum, or record expected
    };
    ^
Main.java:40: error: class, interface, enum, or record expected
    DisjointSetTree.prototype.union = function (other) {
    ^
Main.java:44: error: class, interface, enum, or record expected
        if (xRoot !== yRoot) {
        ^
Main.java:47: error: class, interface, enum, or record expected
            }
            ^
Main.java:49: error: class, interface, enum, or record expected
            if (yRoot.rank === xRoot.rank) {
            ^
Main.java:51: error: class, interface, enum, or record expected
            }
            ^
Main.java:54: error: class, interface, enum, or record expected
    DisjointSetTree.prototype.find = function () {
    ^
Main.java:57: error: class, interface, enum, or record expected
        }
        ^
Main.java:59: error: class, interface, enum, or record expected
    };
    ^
Main.java:60: error: class, interface, enum, or record expected
    return DisjointSetTree;
    ^
Main.java:61: error: class, interface, enum, or record expected
}());
^
Main.java:68: error: not a statement
        return { x: x, y: y };
                    ^
Main.java:68: error: not a statement
        return { x: x, y: y };
                          ^
Main.java:70: error: not a statement
    return { N: N, M: M, p: p, xy: xy };
                ^
Main.java:70: error: not a statement
    return { N: N, M: M, p: p, xy: xy };
                      ^
Main.java:70: error: not a statement
    return { N: N, M: M, p: p, xy: xy };
                            ^
Main.java:70: error: not a statement
    return { N: N, M: M, p: p, xy: xy };
                                   ^
Main.java:62: error: <identifier> expected
function parseInput(input) {
                         ^
Main.java:63: error: 'var' is not allowed in a compound declaration
    var _a = input.split("\n"), line0 = _a[0], line1 = _a[1], lines = _a.slice(2);
    ^
Main.java:64: error: 'var' is not allowed in a compound declaration
    var _b = parseSpaceSeparatedInts(line0), N = _b[0], M = _b[1];
    ^
Main.java:66: error: ')' or ',' expected
    var xy = lines.map(function (line) {
                                      ^
Main.java:67: error: 'var' is not allowed in a compound declaration
        var _a = parseSpaceSeparatedInts(line), x = _a[0], y = _a[1];
        ^
Main.java:68: error: illegal start of expression
        return { x: x, y: y };
               ^
Main.java:68: error: ';' expected
        return { x: x, y: y };
                     ^
Main.java:68: error: ';' expected
        return { x: x, y: y };
                           ^
Main.java:69: error: illegal start of expression
    });
     ^
Main.java:70: error: illegal start of expression
    return { N: N, M: M, p: p, xy: xy };
           ^
Main.java:70: error: ';' expected
    return { N: N, M: M, p: p, xy: xy };
                 ^
Main.java:70: error: ';' expected
    return { N: N, M: M, p: p, xy: xy };
                       ^
Main.java:70: error: ';' expected
    return { N: N, M: M, p: p, xy: xy };
                             ^
Main.java:70: error: ';' expected
    return { N: N, M: M, p: p, xy: xy };
                                     ^
Main.java:72: error: <identifier> expected
function parseSpaceSeparatedInts(line) {
                                     ^
Main.java:77: error: class, interface, enum, or record expected
Equals(require("fs").readFileSync("/dev/stdin", "utf8"));
^
57 errors