Language: Java

#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
struct UnionFind {
  int *data;
  UnionFind(int *_data, int size, bool reset = true) : data(_data) {
    if(reset) for(int i = 0; i < size; i++) data[i] = -1;
  }
  bool unionSet(int x, int y) {
    x = find(x); y = find(y);
    if (x != y) {
      if (data[y] < data[x]) std::swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  int find(int x) {
    return data[x] < 0 ? x : data[x] = find(data[x]);
  }
  int size(int x) {
    return -data[find(x)];
  }
};
int main(int argc, char *argv[])
{
  // kruskal with x being unused.
  // assumption: es is sorted
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
    UnionFind uf(ufdata, n, false);
    int sum = 0;
    for(int i = st; i < es.size(); i++) {
      const int u = es[i].second.first;
      const int v = es[i].second.second;
      if(uf.find(u) != uf.find(v)) {
        uf.unionSet(u, v);
        sum += es[i].first;
      }
    }
    return sum;
  };
  int n, m;
  vector<pair<int,pair<int,int>>> es;
  //cin >> n >> m;
  scanf("%d %d", &n, &m);
  for(int i = 0; i < m; i++) {
    int s, d, c;
    scanf("%d %d %d", &s, &d, &c);
    // cin >> s >> d >> c;
    es.push_back(make_pair(c, make_pair(s-1, d-1)));
  }
  sort(es.begin(), es.end());
  int mn = -1;
  int sum = 0;
  int cnt = 0;
  vector<int> ufdata(n);
  UnionFind uf(&(ufdata[0]), n);
  {
    vector<int> ufdata2(n);
    for(int j = 0; j < n; j++) ufdata2[j] = ufdata[j];
    mn = kruskal(n, es, &(ufdata2[0]), 0);
  }
  int ksum = 0;
  for(int i = 0; i < m; i++) {
    vector<int> ufdata2(n);
    for(int j = 0; j < n; j++) ufdata2[j] = ufdata[j];
    int mni = ksum + kruskal(n, es, &(ufdata2[0]), i+1);
    if(mni != mn) {
      cnt ++;
      sum += es[i].first;
    }
    const int u = es[i].second.first;
    const int v = es[i].second.second;
    if(uf.find(u) != uf.find(v)) {
      uf.unionSet(u, v);
      ksum += es[i].first;
    }
  }
  cout << cnt << " " << sum << endl;
  return 0;
}




Compiler output:
Main.java:1: error: illegal character: '#'
#include<iostream>
^
Main.java:2: error: illegal character: '#'
#include<vector>
^
Main.java:3: error: illegal character: '#'
#include<utility>
^
Main.java:4: error: illegal character: '#'
#include<algorithm>
^
Main.java:6: error: class, interface, enum, or record expected
struct UnionFind {
^
Main.java:8: error: class, interface, enum, or record expected
  UnionFind(int *_data, int size, bool reset = true) : data(_data) {
  ^
Main.java:9: error: class, interface, enum, or record expected
    if(reset) for(int i = 0; i < size; i++) data[i] = -1;
                             ^
Main.java:9: error: class, interface, enum, or record expected
    if(reset) for(int i = 0; i < size; i++) data[i] = -1;
                                       ^
Main.java:10: error: class, interface, enum, or record expected
  }
  ^
Main.java:12: error: class, interface, enum, or record expected
    x = find(x); y = find(y);
                 ^
Main.java:13: error: class, interface, enum, or record expected
    if (x != y) {
    ^
Main.java:15: error: class, interface, enum, or record expected
      data[x] += data[y]; data[y] = x;
      ^
Main.java:15: error: class, interface, enum, or record expected
      data[x] += data[y]; data[y] = x;
                          ^
Main.java:16: error: class, interface, enum, or record expected
    }
    ^
Main.java:18: error: class, interface, enum, or record expected
  }
  ^
Main.java:21: error: class, interface, enum, or record expected
  }
  ^
Main.java:24: error: class, interface, enum, or record expected
  }
  ^
Main.java:30: error: not a statement
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                       ^
Main.java:30: error: not a statement
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                  ^
Main.java:30: error: not a statement
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                      ^
Main.java:30: error: not a statement
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                           ^
Main.java:31: error: not a statement
    UnionFind uf(ufdata, n, false);
                 ^
Main.java:31: error: not a statement
    UnionFind uf(ufdata, n, false);
                         ^
Main.java:57: error: not a statement
  vector<int> ufdata(n);
                     ^
Main.java:58: error: not a statement
  UnionFind uf(&(ufdata[0]), n);
                       ^
Main.java:58: error: not a statement
  UnionFind uf(&(ufdata[0]), n);
                             ^
Main.java:60: error: not a statement
    vector<int> ufdata2(n);
                        ^
Main.java:66: error: not a statement
    vector<int> ufdata2(n);
                        ^
Main.java:80: error: not a statement
  cout << cnt << " " << sum << endl;
                            ^
Main.java:26: error: unnamed classes are a preview feature and are disabled by default.
int main(int argc, char *argv[])
^
  (use --enable-preview to enable unnamed classes)
Main.java:26: error: <identifier> expected
int main(int argc, char *argv[])
                       ^
Main.java:30: error: illegal start of expression
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                 ^
Main.java:30: error: <identifier> expected
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                          ^
Main.java:30: error: ';' expected
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                ^
Main.java:30: error: ';' expected
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                    ^
Main.java:30: error: ';' expected
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                         ^
Main.java:30: error: ';' expected
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                                 ^
Main.java:30: error: ';' expected
  auto kruskal = [](int n, const vector<pair<int,pair<int,int>>> &es, int *ufdata, int st) {
                                                                                         ^
Main.java:31: error: ';' expected
    UnionFind uf(ufdata, n, false);
                ^
Main.java:31: error: ';' expected
    UnionFind uf(ufdata, n, false);
                       ^
Main.java:31: error: ';' expected
    UnionFind uf(ufdata, n, false);
                          ^
Main.java:34: error: illegal start of expression
      const int u = es[i].second.first;
      ^
Main.java:35: error: illegal start of expression
      const int v = es[i].second.second;
      ^
Main.java:46: error: illegal start of expression
  scanf("%d %d", &n, &m);
                 ^
Main.java:46: error: illegal start of expression
  scanf("%d %d", &n, &m);
                     ^
Main.java:49: error: illegal start of expression
    scanf("%d %d %d", &s, &d, &c);
                      ^
Main.java:49: error: illegal start of expression
    scanf("%d %d %d", &s, &d, &c);
                          ^
Main.java:49: error: illegal start of expression
    scanf("%d %d %d", &s, &d, &c);
                              ^
Main.java:57: error: ';' expected
  vector<int> ufdata(n);
                    ^
Main.java:57: error: ';' expected
  vector<int> ufdata(n);
                      ^
Main.java:58: error: ';' expected
  UnionFind uf(&(ufdata[0]), n);
              ^
Main.java:58: error: ';' expected
  UnionFind uf(&(ufdata[0]), n);
                          ^
Main.java:58: error: ';' expected
  UnionFind uf(&(ufdata[0]), n);
                              ^
Main.java:60: error: ';' expected
    vector<int> ufdata2(n);
                       ^
Main.java:60: error: ';' expected
    vector<int> ufdata2(n);
                         ^
Main.java:62: error: illegal start of expression
    mn = kruskal(n, es, &(ufdata2[0]), 0);
                        ^
Main.java:66: error: ';' expected
    vector<int> ufdata2(n);
                       ^
Main.java:66: error: ';' expected
    vector<int> ufdata2(n);
                         ^
Main.java:68: error: illegal start of expression
    int mni = ksum + kruskal(n, es, &(ufdata2[0]), i+1);
                                    ^
Main.java:73: error: illegal start of expression
    const int u = es[i].second.first;
    ^
Main.java:74: error: illegal start of expression
    const int v = es[i].second.second;
    ^
61 errors