puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 044

C++ solution:

Crystal solution: n, q = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
i = 0
q.times do
  t, x, y = read_line.split.map(&.to_i.pred)
  case t
  when 0
    a[(i + x) % n], a[(i + y) % n] = a[(i + y) % n], a[(i + x) % n]
  when 1
    i -= 1
  when 2
    puts a[(i + x) % n]
  end
end





Kuin solution: var offset: int
var n: int

func main()
	do @n :: cui@inputInt()
	var q: int :: cui@inputInt()
	var a: []int :: #[@n]int
	for i(0, @n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	for(1, q)
		var t: int :: cui@inputInt()
		var x: int :: cui@inputInt() - 1
		var y: int :: cui@inputInt() - 1
		switch(t)
		case 1
			var tmp: int :: a[idx(x)]
			do a[idx(x)] :: a[idx(y)]
			do a[idx(y)] :: tmp
		case 2
			do @offset :+ @n - 1
		case 3
			var ans: int :: a[idx(x)]
			do cui@print("\{ans}\n")
		end switch
	end for
	
	func idx(i: int): int
		ret(i + @offset) % @n
	end func
end func
AtCoder problem 044

C++ solution:
#include <iostream>
#include <queue>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
  int n,q;
  cin >> n >> q;
  deque<int> a(n);
  rep(i, n) cin >> a[i];

  rep(i, q){
    int t,x,y;
    cin >> t >> x >> y;
    x--; y--;
    if(t == 1) swap(a[x], a[y]);
    if(t == 2){
      int v = a[n - 1];
      a.erase(a.end() - 1);
      a.push_front(v);
    }
    if(t == 3) cout << a[x] << "\n";
  }
}





Kuin solution: var offset: int
var n: int

func main()
	do @n :: cui@inputInt()
	var q: int :: cui@inputInt()
	var a: []int :: #[@n]int
	for i(0, @n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	for(1, q)
		var t: int :: cui@inputInt()
		var x: int :: cui@inputInt() - 1
		var y: int :: cui@inputInt() - 1
		switch(t)
		case 1
			var tmp: int :: a[idx(x)]
			do a[idx(x)] :: a[idx(y)]
			do a[idx(y)] :: tmp
		case 2
			do @offset :+ @n - 1
		case 3
			var ans: int :: a[idx(x)]
			do cui@print("\{ans}\n")
		end switch
	end for
	
	func idx(i: int): int
		ret(i + @offset) % @n
	end func
end func
AtCoder problem 045

C++ solution:
#include <vector>

#include <iostream>

using namespace std;

int main() {

	cin.tie(0);

	ios_base::sync_with_stdio(false);

	int N, K;

	cin >> N >> K;

	vector<int> X(N), Y(N);

	for (int i = 0; i < N; ++i) {

		cin >> X[i] >> Y[i];

	}

	vector<vector<long long> > d(N, vector<long long>(N));

	for (int i = 0; i < N; ++i) {

		for (int j = 0; j < N; ++j) {

			d[i][j] = (long long)(X[i] - X[j]) * (X[i] - X[j]) + (long long)(Y[i] - Y[j]) * (Y[i] - Y[j]);

		}

	}

	vector<long long> cost(1 << N);

	for (int i = 1; i < 1 << N; ++i) {

		for (int j = 0; j < N; ++j) {

			for (int k = 0; k < j; ++k) {

				if (((i >> j) & 1) == 1 && ((i >> k) & 1) == 1) {

					cost[i] = max(cost[i], d[j][k]);

				}

			}

		}

	}

	vector<vector<long long> > dp(K + 1, vector<long long>(1 << N, 1LL << 62));

	dp[0][0] = 0;

	for (int i = 1; i <= K; ++i) {

		for (int j = 1; j < 1 << N; ++j) {

			for (int k = j; k != 0; k = (k - 1) & j) {

				dp[i][j] = min(dp[i][j], max(dp[i - 1][j - k], cost[k]));

			}

		}

	}

	cout << dp[K][(1 << N) - 1] << endl;

	return 0;

}

Crystal solution: struct Int
  def each_subset(&)
    yield self
    sub = ~-self & self
    loop do
      yield sub
      sub = ~-sub & self
      break if sub == 0
    end
  end
end

n, k = read_line.split.map(&.to_i)
points = (1..n).map { {Int64, Int64}.from read_line.split.map(&.to_i64) }

max_dist = (0...1 << n).map { |set|
  (0...n).select { |i|
    set.bit(i) == 1
  }.combinations(2).max_of? { |(i, j)|
    (points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2
  } || 0i64
}

dp = (0...1 << n).map { [10i64 ** 18] * -~k }
dp[0][0] = 0
(1...1 << n).each do |set|
  (0...k).each do |i|
    set.each_subset do |sub|
      dp[set][i + 1] = {dp[set][i + 1], {dp[set & ~sub][i], max_dist[sub]}.max}.min
    end
  end
end
puts dp[-1][-1]
AtCoder problem 046

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

void print_vector(vector<int> v) {
  for (int i : v) {
    cout << i << " ";
  }
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  vector<ll> buc_a(46), buc_b(46), buc_c(46);
  rep(i, n) { buc_c[c[i] % 46]++; }
  rep(i, n) {
    rep(j, 46) { buc_b[(j + b[i]) % 46] += buc_c[j]; }
  }
  rep(i, n) {
    rep(j, 46) { buc_a[(j + a[i]) % 46] += buc_b[j]; }
  }
  cout << buc_a[0] << endl;
}

Crystal solution: read_line
a = (1..3).map {
  b = read_line.split.map(&.to_i.%(46))
  (0...46).map { |i| b.count(i) }
}
mod46 = (0...46).to_a
puts Array.product(mod46, mod46, mod46).select { |indices|
  indices.sum % 46 == 0
}.sum { |indices|
  a.zip(indices).product { |(b, i)| b[i].to_i64 }
}
AtCoder problem 046

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

void print_vector(vector<int> v) {
  for (int i : v) {
    cout << i << " ";
  }
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  vector<ll> buc_a(46), buc_b(46), buc_c(46);
  rep(i, n) { buc_c[c[i] % 46]++; }
  rep(i, n) {
    rep(j, 46) { buc_b[(j + b[i]) % 46] += buc_c[j]; }
  }
  rep(i, n) {
    rep(j, 46) { buc_a[(j + a[i]) % 46] += buc_b[j]; }
  }
  cout << buc_a[0] << endl;
}


Python solution: import sys, numpy

sys.stdin.readline()

A = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')
B = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')
C = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')

m = 46

a = numpy.bincount(A % m, minlength=m)
b = numpy.bincount(B % m, minlength=m)
c = numpy.bincount(C % m, minlength=m)

print(sum(a[i] * b[j] * c[-(i+j) % m] for i, j in numpy.ndindex(m, m)))
AtCoder problem 046

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

void print_vector(vector<int> v) {
  for (int i : v) {
    cout << i << " ";
  }
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  vector<ll> buc_a(46), buc_b(46), buc_c(46);
  rep(i, n) { buc_c[c[i] % 46]++; }
  rep(i, n) {
    rep(j, 46) { buc_b[(j + b[i]) % 46] += buc_c[j]; }
  }
  rep(i, n) {
    rep(j, 46) { buc_a[(j + a[i]) % 46] += buc_b[j]; }
  }
  cout << buc_a[0] << endl;
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: [][]int :: #[3, n]int
	for i(0, 2)
		for j(0, n - 1)
			do a[i][j] :: cui@inputInt()
		end for
	end for
	
	const mod: int :: 46
	var dp: [][]int :: #[4, mod]int
	do dp[0][0] :: 1
	
	for i(0, 2)
		for j(0, n - 1)
			var v: int :: a[i][j]
			for k(0, mod - 1)
				do dp[i + 1][(k + v)%mod] :+ dp[i][k]
			end for
		end for
	end for
	
	var ans: int :: dp[3][0]
	do cui@print("\{ans}\n")
end func
AtCoder problem 046

C++ solution:

Crystal solution: read_line
a = (1..3).map {
  b = read_line.split.map(&.to_i.%(46))
  (0...46).map { |i| b.count(i) }
}
mod46 = (0...46).to_a
puts Array.product(mod46, mod46, mod46).select { |indices|
  indices.sum % 46 == 0
}.sum { |indices|
  a.zip(indices).product { |(b, i)| b[i].to_i64 }
}


Python solution: import sys, numpy

sys.stdin.readline()

A = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')
B = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')
C = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')

m = 46

a = numpy.bincount(A % m, minlength=m)
b = numpy.bincount(B % m, minlength=m)
c = numpy.bincount(C % m, minlength=m)

print(sum(a[i] * b[j] * c[-(i+j) % m] for i, j in numpy.ndindex(m, m)))
AtCoder problem 046

C++ solution:

Crystal solution: read_line
a = (1..3).map {
  b = read_line.split.map(&.to_i.%(46))
  (0...46).map { |i| b.count(i) }
}
mod46 = (0...46).to_a
puts Array.product(mod46, mod46, mod46).select { |indices|
  indices.sum % 46 == 0
}.sum { |indices|
  a.zip(indices).product { |(b, i)| b[i].to_i64 }
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: [][]int :: #[3, n]int
	for i(0, 2)
		for j(0, n - 1)
			do a[i][j] :: cui@inputInt()
		end for
	end for
	
	const mod: int :: 46
	var dp: [][]int :: #[4, mod]int
	do dp[0][0] :: 1
	
	for i(0, 2)
		for j(0, n - 1)
			var v: int :: a[i][j]
			for k(0, mod - 1)
				do dp[i + 1][(k + v)%mod] :+ dp[i][k]
			end for
		end for
	end for
	
	var ans: int :: dp[3][0]
	do cui@print("\{ans}\n")
end func
AtCoder problem 046

C++ solution:


Python solution: import sys, numpy

sys.stdin.readline()

A = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')
B = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')
C = numpy.fromstring(sys.stdin.readline(), dtype=int, sep=' ')

m = 46

a = numpy.bincount(A % m, minlength=m)
b = numpy.bincount(B % m, minlength=m)
c = numpy.bincount(C % m, minlength=m)

print(sum(a[i] * b[j] * c[-(i+j) % m] for i, j in numpy.ndindex(m, m)))




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: [][]int :: #[3, n]int
	for i(0, 2)
		for j(0, n - 1)
			do a[i][j] :: cui@inputInt()
		end for
	end for
	
	const mod: int :: 46
	var dp: [][]int :: #[4, mod]int
	do dp[0][0] :: 1
	
	for i(0, 2)
		for j(0, n - 1)
			var v: int :: a[i][j]
			for k(0, mod - 1)
				do dp[i + 1][(k + v)%mod] :+ dp[i][k]
			end for
		end for
	end for
	
	var ans: int :: dp[3][0]
	do cui@print("\{ans}\n")
end func
AtCoder problem 047

C++ solution:
#include <string>

#include <vector>

#include <iostream>

using namespace std;

const int mod = 699999953; // large prime number

int main() {

	int N; string S, T;

	cin >> N >> S >> T;

	vector<int> seq1(N), seq3(N);

	for (int i = 0; i < N; ++i) {

		seq1[i] = (S[i] == 'R' ? 0 : (S[i] == 'G' ? 1 : 2));

		seq3[i] = (T[i] == 'R' ? 0 : (T[i] == 'G' ? 1 : 2));

	}

	int answer = 0;

	for (int i = 0; i < 3; ++i) {

		vector<int> seq2(N);

		for (int j = 0; j < N; ++j) {

			seq2[j] = (i - seq3[j] + 3) % 3;

		}

		int power3 = 1, hash1 = 0, hash2 = 0;

		for (int j = 0; j < N; ++j) {

			hash1 = (hash1 * 3 + seq1[j]) % mod;

			hash2 = (hash2 + power3 * seq2[N - j - 1]) % mod;

			if (hash1 == hash2) {

				++answer;

			}

			power3 = power3 * 3 % mod;

		}

		power3 = 1, hash1 = 0, hash2 = 0;

		for (int j = 0; j < N - 1; ++j) {

			hash1 = (hash1 + power3 * seq1[N - j - 1]) % mod;

			hash2 = (hash2 * 3 + seq2[j]) % mod;

			if (hash1 == hash2) {

				++answer;

			}

			power3 = power3 * 3 % mod;

		}

	}

	cout << answer << endl;

	return 0;

}

Crystal solution: class RollingHash
  MOD = (1u64 << 61) - 1

  private def mul(a : UInt64, b : UInt64)
    t = a.to_u128 * b
    t = (t >> 61) + (t & MOD)
    (t < MOD ? t : t - MOD).to_u64
  end

  getter size : Int32

  def initialize(s : String, base : UInt64 = 10007u64)
    initialize(s.size, s.each_char, base, &.ord)
  end

  def initialize(a : Array(Int), base : UInt64 = 10007u64)
    initialize(a.size, a, base, &.itself)
  end

  def initialize(@size, a : Enumerable, base : UInt64 = 10007u64, &)
    @pow = Array(UInt64).new(size + 1, 1)
    @hash = Array(UInt64).new(size + 1, 0)
    a.each_with_index do |x, i|
      @pow[i + 1] = mul(@pow[i], base)
      @hash[i + 1] = mul(@hash[i], base) + yield(x)
      @hash[i + 1] -= MOD if @hash[i + 1] >= MOD
    end
  end

  def [](start : Int, count : Int)
    res = @hash[start + count] + MOD - mul(@hash[start], @pow[count])
    res < MOD ? res : res - MOD
  end

  def [](range : Range)
    self[*Indexable.range_to_index_and_count(range, size) || raise IndexError.new]
  end
end

n = read_line.to_i
s, t = {0, 1}.map {
  read_line.chars.map { |c| "RGB".index(c).not_nil! }
}
s_rh = RollingHash.new(s)
puts (0...3).sum { |k|
  t_rh = RollingHash.new(n, t) { |i| i == k ? i : 3 ^ k ^ i }
  (-n + 1...n).count do |i|
    if i < 0
      s_rh[-i, n + i] == t_rh[0, n + i]
    else
      s_rh[0, n - i] == t_rh[i, n - i]
    end
  end
}
AtCoder problem 048

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  priority_queue<pair<int, int>> q;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    q.push({b, a});
  }
  ll ans = 0;
  while (k > 0) {
    pair<int, int> now = q.top();
    q.pop();
    ans += now.first;
    q.push({now.second - now.first, -1});
    k--;
  }
  cout << ans << endl;
}

Crystal solution: n, k = read_line.split.map(&.to_i)
puts (1..n).flat_map {
  a, b = read_line.split.map(&.to_i64)
  [a - b, b]
}.sort[-k..].sum
AtCoder problem 048

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  priority_queue<pair<int, int>> q;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    q.push({b, a});
  }
  ll ans = 0;
  while (k > 0) {
    pair<int, int> now = q.top();
    q.pop();
    ans += now.first;
    q.push({now.second - now.first, -1});
    k--;
  }
  cout << ans << endl;
}


Python solution: #!/usr/bin/env python3


def main():
    n, k = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]

    points = []
    for [a, b] in ab:
        points.append(b)
        points.append(a - b)

    points.sort()
    points.reverse()

    ans = sum(points[:k])
    print(ans)


if __name__ == "__main__":
    main()
AtCoder problem 048

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  priority_queue<pair<int, int>> q;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    q.push({b, a});
  }
  ll ans = 0;
  while (k > 0) {
    pair<int, int> now = q.top();
    q.pop();
    ans += now.first;
    q.push({now.second - now.first, -1});
    k--;
  }
  cout << ans << endl;
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var c: []int :: #[2 * n]int
	for i(0, n - 1)
		var a: int :: cui@inputInt()
		var b: int :: cui@inputInt()
		do c[2 * i] :: b
		do c[2 * i + 1] :: a - b
	end for
	do c.sort()
	do c.reverse()
	
	var ans: int :: 0
	for i(0, k - 1)
		do ans :+ c[i]
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 048

C++ solution:

Crystal solution: n, k = read_line.split.map(&.to_i)
puts (1..n).flat_map {
  a, b = read_line.split.map(&.to_i64)
  [a - b, b]
}.sort[-k..].sum


Python solution: #!/usr/bin/env python3


def main():
    n, k = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]

    points = []
    for [a, b] in ab:
        points.append(b)
        points.append(a - b)

    points.sort()
    points.reverse()

    ans = sum(points[:k])
    print(ans)


if __name__ == "__main__":
    main()
AtCoder problem 048

C++ solution:

Crystal solution: n, k = read_line.split.map(&.to_i)
puts (1..n).flat_map {
  a, b = read_line.split.map(&.to_i64)
  [a - b, b]
}.sort[-k..].sum





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var c: []int :: #[2 * n]int
	for i(0, n - 1)
		var a: int :: cui@inputInt()
		var b: int :: cui@inputInt()
		do c[2 * i] :: b
		do c[2 * i + 1] :: a - b
	end for
	do c.sort()
	do c.reverse()
	
	var ans: int :: 0
	for i(0, k - 1)
		do ans :+ c[i]
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 048

C++ solution:


Python solution: #!/usr/bin/env python3


def main():
    n, k = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]

    points = []
    for [a, b] in ab:
        points.append(b)
        points.append(a - b)

    points.sort()
    points.reverse()

    ans = sum(points[:k])
    print(ans)


if __name__ == "__main__":
    main()




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var c: []int :: #[2 * n]int
	for i(0, n - 1)
		var a: int :: cui@inputInt()
		var b: int :: cui@inputInt()
		do c[2 * i] :: b
		do c[2 * i + 1] :: a - b
	end for
	do c.sort()
	do c.reverse()
	
	var ans: int :: 0
	for i(0, k - 1)
		do ans :+ c[i]
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 049

C++ solution:
#include <iostream>

#include <vector>

#include <tuple>

#include <algorithm>

using namespace std;



class UnionFind {

public:

	vector<int> par;



	void init(int sz) {

		par.resize(sz, -1);

	}

	int root(int pos) {

		if (par[pos] == -1) return pos;

		par[pos] = root(par[pos]);

		return par[pos];

	}

	void unite(int u, int v) {

		u = root(u); v = root(v);

		if (u == v) return;

		par[u] = v;

	}

	bool same(int u, int v) {

		if (root(u) == root(v)) return true;

		return false;

	}

};



long long N, M;

long long C[1 << 19], L[1 << 19], R[1 << 19];



int main() {

	// Step #1. 入力

	cin >> N >> M;

	for (int i = 1; i <= M; i++) {

		cin >> C[i] >> L[i] >> R[i];

	}



	// Step #2. ソート

	vector<tuple<long long, int, int>> tup;

	for (int i = 1; i <= M; i++) tup.push_back(make_tuple(C[i], L[i] - 1, R[i]));

	sort(tup.begin(), tup.end());



	// Step #3. 最小全域木を求める

	long long cnt1 = 0, cnt2 = 0;

	UnionFind UF;

	UF.init(N + 2);

	for (int i = 0; i < tup.size(); i++) {

		int u = get<1>(tup[i]);

		int v = get<2>(tup[i]);

		if (UF.same(u, v) == false) {

			UF.unite(u, v);

			cnt1 += get<0>(tup[i]);

			cnt2 += 1;

		}

	}



	// Step #4. 出力

	if (cnt2 != N) cout << "-1" << endl;

	else cout << cnt1 << endl;

	return 0;

}

Crystal solution: class UnionFind
  @d : Array(Int32)

  def initialize(n : Int32)
    @d = Array.new(n, -1)
  end

  def root(x : Int32)
    @d[x] < 0 ? x : (@d[x] = root(@d[x]))
  end

  def unite(x : Int32, y : Int32)
    x = root(x)
    y = root(y)
    return false if x == y
    x, y = y, x if @d[x] > @d[y]
    @d[x] += @d[y]
    @d[y] = x
    true
  end

  def same?(x : Int32, y : Int32)
    root(x) == root(y)
  end

  def size(x : Int32)
    -@d[root(x)]
  end
end

struct Edge2(T)
  include Comparable(Edge2(T))

  property from : Int32
  property to : Int32
  property cost : T

  def initialize(@from : Int32, @to : Int32, @cost : T)
  end

  def <=>(other : Edge2(T))
    {cost, from, to} <=> {other.cost, other.from, other.to}
  end

  def to_s(io) : Nil
    io << '(' << from << ", " << to << ", " << cost << ')'
  end
end

def kruskal(n : Int32, edges : Array(Edge2(T))) : T? forall T
  uf = UnionFind.new(n)
  result = edges.sort.sum do |edge|
    if uf.unite(edge.from, edge.to)
      edge.cost
    else
      T.zero
    end
  end
  uf.size(0) == n ? result : nil
end

n, m = read_line.split.map(&.to_i)
edges = (1..m).map {
  c, l, r = read_line.split.map(&.to_i)
  Edge2.new(l - 1, r, c.to_i64)
}
puts kruskal(n + 1, edges) || -1
AtCoder problem 049

C++ solution:
#include <iostream>

#include <vector>

#include <tuple>

#include <algorithm>

using namespace std;



class UnionFind {

public:

	vector<int> par;



	void init(int sz) {

		par.resize(sz, -1);

	}

	int root(int pos) {

		if (par[pos] == -1) return pos;

		par[pos] = root(par[pos]);

		return par[pos];

	}

	void unite(int u, int v) {

		u = root(u); v = root(v);

		if (u == v) return;

		par[u] = v;

	}

	bool same(int u, int v) {

		if (root(u) == root(v)) return true;

		return false;

	}

};



long long N, M;

long long C[1 << 19], L[1 << 19], R[1 << 19];



int main() {

	// Step #1. 入力

	cin >> N >> M;

	for (int i = 1; i <= M; i++) {

		cin >> C[i] >> L[i] >> R[i];

	}



	// Step #2. ソート

	vector<tuple<long long, int, int>> tup;

	for (int i = 1; i <= M; i++) tup.push_back(make_tuple(C[i], L[i] - 1, R[i]));

	sort(tup.begin(), tup.end());



	// Step #3. 最小全域木を求める

	long long cnt1 = 0, cnt2 = 0;

	UnionFind UF;

	UF.init(N + 2);

	for (int i = 0; i < tup.size(); i++) {

		int u = get<1>(tup[i]);

		int v = get<2>(tup[i]);

		if (UF.same(u, v) == false) {

			UF.unite(u, v);

			cnt1 += get<0>(tup[i]);

			cnt2 += 1;

		}

	}



	// Step #4. 出力

	if (cnt2 != N) cout << "-1" << endl;

	else cout << cnt1 << endl;

	return 0;

}


Python solution: from numpy import*
from scipy.sparse import*
(n,m),*a=[int_(t.split())for t in open(0)]
a=int_(a).T
a[1]-=1
a=a[:,argsort(a[0])]
a=a[:,unique(a[1:],return_index=1,axis=1)[1]]
print([-1,int(sum(b:=csgraph.minimum_spanning_tree(coo_matrix((a[0],a[1:]),(n+1,n+1))).data))][len(b)==n])
AtCoder problem 049

C++ solution:

Crystal solution: class UnionFind
  @d : Array(Int32)

  def initialize(n : Int32)
    @d = Array.new(n, -1)
  end

  def root(x : Int32)
    @d[x] < 0 ? x : (@d[x] = root(@d[x]))
  end

  def unite(x : Int32, y : Int32)
    x = root(x)
    y = root(y)
    return false if x == y
    x, y = y, x if @d[x] > @d[y]
    @d[x] += @d[y]
    @d[y] = x
    true
  end

  def same?(x : Int32, y : Int32)
    root(x) == root(y)
  end

  def size(x : Int32)
    -@d[root(x)]
  end
end

struct Edge2(T)
  include Comparable(Edge2(T))

  property from : Int32
  property to : Int32
  property cost : T

  def initialize(@from : Int32, @to : Int32, @cost : T)
  end

  def <=>(other : Edge2(T))
    {cost, from, to} <=> {other.cost, other.from, other.to}
  end

  def to_s(io) : Nil
    io << '(' << from << ", " << to << ", " << cost << ')'
  end
end

def kruskal(n : Int32, edges : Array(Edge2(T))) : T? forall T
  uf = UnionFind.new(n)
  result = edges.sort.sum do |edge|
    if uf.unite(edge.from, edge.to)
      edge.cost
    else
      T.zero
    end
  end
  uf.size(0) == n ? result : nil
end

n, m = read_line.split.map(&.to_i)
edges = (1..m).map {
  c, l, r = read_line.split.map(&.to_i)
  Edge2.new(l - 1, r, c.to_i64)
}
puts kruskal(n + 1, edges) || -1


Python solution: from numpy import*
from scipy.sparse import*
(n,m),*a=[int_(t.split())for t in open(0)]
a=int_(a).T
a[1]-=1
a=a[:,argsort(a[0])]
a=a[:,unique(a[1:],return_index=1,axis=1)[1]]
print([-1,int(sum(b:=csgraph.minimum_spanning_tree(coo_matrix((a[0],a[1:]),(n+1,n+1))).data))][len(b)==n])