puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 011

C++ solution:
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using ll = long long;
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }

//43[ms]
int main(){
  int n;
  cin >> n;
  vector<tuple<int,int,int>> v(n);
  for(int i = 0; i < n; i++){
    int d,c,s;
    cin >> d >> c >> s;
    v[i] = {d,c,s};
  }
  sort(v.begin(), v.end());
  vector<ll> cur(5001), next(5001);
  for(int i = 0; i < n; i++){
    int d,c,s;
    tie(d,c,s) = v[i];
    for(int j = 0; j <= 5000; j++){
      next[j] = cur[j];
      if(j > d || j < c) continue;
      chmax(next[j], cur[j - c] + s);
    }
    swap(cur, next);
  }
  ll ans = 0;
  for(int i = 0; i <= 5000; i++) chmax(ans, cur[i]);
  cout << ans << "\n";
}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    DCS = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
    M = max(d for d, c, s in DCS)
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    for i in range(N):
        d, c, s = DCS[i]
        for j in range(M + 1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + c > d: continue
            dp[i + 1][j + c] = max(dp[i + 1][j + c], dp[i][j] + s)
    print(max(dp[N]))

if __name__ == '__main__':
    main()
AtCoder problem 011

C++ solution:
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using ll = long long;
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }

//43[ms]
int main(){
  int n;
  cin >> n;
  vector<tuple<int,int,int>> v(n);
  for(int i = 0; i < n; i++){
    int d,c,s;
    cin >> d >> c >> s;
    v[i] = {d,c,s};
  }
  sort(v.begin(), v.end());
  vector<ll> cur(5001), next(5001);
  for(int i = 0; i < n; i++){
    int d,c,s;
    tie(d,c,s) = v[i];
    for(int j = 0; j <= 5000; j++){
      next[j] = cur[j];
      if(j > d || j < c) continue;
      chmax(next[j], cur[j - c] + s);
    }
    swap(cur, next);
  }
  ll ans = 0;
  for(int i = 0; i <= 5000; i++) chmax(ans, cur[i]);
  cout << ans << "\n";
}






Ruby solution: Task = Struct.new(:d, :c, :s)

n = readline.to_i
tasks = Array.new(n) do
    Task.new(*readline.split.map(&:to_i))
end

tasks.sort_by! { |t| -t.d }
profit = [0] * 5001

for t in tasks
    d, c, s = t.d, t.c, t.s
    for k in 0..(d - c)
        profit[k] = [profit[k], profit[k + c] + s].max
    end
end

puts profit[0]
AtCoder problem 011

C++ solution:
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using ll = long long;
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }

//43[ms]
int main(){
  int n;
  cin >> n;
  vector<tuple<int,int,int>> v(n);
  for(int i = 0; i < n; i++){
    int d,c,s;
    cin >> d >> c >> s;
    v[i] = {d,c,s};
  }
  sort(v.begin(), v.end());
  vector<ll> cur(5001), next(5001);
  for(int i = 0; i < n; i++){
    int d,c,s;
    tie(d,c,s) = v[i];
    for(int j = 0; j <= 5000; j++){
      next[j] = cur[j];
      if(j > d || j < c) continue;
      chmax(next[j], cur[j - c] + s);
    }
    swap(cur, next);
  }
  ll ans = 0;
  for(int i = 0; i <= 5000; i++) chmax(ans, cur[i]);
  cout << ans << "\n";
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var works: []Work :: #[n]Work
	var dMax: int :: 0
	for i(0, n - 1)
		var d: int :: cui@inputInt()
		var c: int :: cui@inputInt()
		var s: int :: cui@inputInt()
		do works[i] :: (#Work).init(d, c, s)
		do dMax :: [dMax, d].max()
	end for
	do works.sort()
	
	; dp[仕事番号][合計仕事時間] = 合計金額
	var dp: [][]int :: #[n + 1, dMax + 1]int
	; 初期化する代わりに、最初の所持金を1円として「最終的な金額 - 1」を答えとする。
	do dp[0][0] :: 1
	for i(0, n - 1)
		for j(0, dMax)
			var d: int :: works[i].d
			var c: int :: works[i].c
			var s: int :: works[i].s
			if(j + c <= d)
				do dp[i + 1][j + c] :: [dp[i + 1][j + c], dp[i][j] + s].max()
			end if
			do dp[i + 1][j] :: [dp[i + 1][j], dp[i][j]].max()
		end for
	end for
	var ans: int :: 0
	for j(0, dMax)
		do ans :: [ans, dp[n][j]].max()
	end for
	do ans :- 1
	do cui@print("\{ans}\n")
	
	class Work()
		+var d: int
		+var c: int
		+var s: int
		+func init(d: int, c: int, s: int): Work
			do me.d :: d
			do me.c :: c
			do me.s :: s
			ret me
		end func
		+*func cmp(t: kuin@Class): int
			if(me.d < (t $ Work).d)
				ret - 1
			elif(me.d > (t $ Work).d)
				ret 1
			end if
			ret 0
		end func
	end class
end func
AtCoder problem 011

C++ solution:

Crystal solution: n = read_line.to_i
dcs = (1..n).map {
  read_line.split.try { |(d, c, s)| {d.to_i, c.to_i, s.to_i64} }
}.sort
day = dcs.max_of { |(d, c, s)| d }

puts dcs.each_with_object([0i64] * day.succ) { |(d, c, s), dp|
  (c..d).reverse_each do |i|
    dp[i] = {dp[i], dp[i - c] + s}.max
  end
  (0...day).each do |i|
    dp[i + 1] = {dp[i + 1], dp[i]}.max
  end
}.max


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    DCS = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
    M = max(d for d, c, s in DCS)
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    for i in range(N):
        d, c, s = DCS[i]
        for j in range(M + 1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + c > d: continue
            dp[i + 1][j + c] = max(dp[i + 1][j + c], dp[i][j] + s)
    print(max(dp[N]))

if __name__ == '__main__':
    main()
AtCoder problem 011

C++ solution:

Crystal solution: n = read_line.to_i
dcs = (1..n).map {
  read_line.split.try { |(d, c, s)| {d.to_i, c.to_i, s.to_i64} }
}.sort
day = dcs.max_of { |(d, c, s)| d }

puts dcs.each_with_object([0i64] * day.succ) { |(d, c, s), dp|
  (c..d).reverse_each do |i|
    dp[i] = {dp[i], dp[i - c] + s}.max
  end
  (0...day).each do |i|
    dp[i + 1] = {dp[i + 1], dp[i]}.max
  end
}.max






Ruby solution: Task = Struct.new(:d, :c, :s)

n = readline.to_i
tasks = Array.new(n) do
    Task.new(*readline.split.map(&:to_i))
end

tasks.sort_by! { |t| -t.d }
profit = [0] * 5001

for t in tasks
    d, c, s = t.d, t.c, t.s
    for k in 0..(d - c)
        profit[k] = [profit[k], profit[k + c] + s].max
    end
end

puts profit[0]
AtCoder problem 011

C++ solution:

Crystal solution: n = read_line.to_i
dcs = (1..n).map {
  read_line.split.try { |(d, c, s)| {d.to_i, c.to_i, s.to_i64} }
}.sort
day = dcs.max_of { |(d, c, s)| d }

puts dcs.each_with_object([0i64] * day.succ) { |(d, c, s), dp|
  (c..d).reverse_each do |i|
    dp[i] = {dp[i], dp[i - c] + s}.max
  end
  (0...day).each do |i|
    dp[i + 1] = {dp[i + 1], dp[i]}.max
  end
}.max





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var works: []Work :: #[n]Work
	var dMax: int :: 0
	for i(0, n - 1)
		var d: int :: cui@inputInt()
		var c: int :: cui@inputInt()
		var s: int :: cui@inputInt()
		do works[i] :: (#Work).init(d, c, s)
		do dMax :: [dMax, d].max()
	end for
	do works.sort()
	
	; dp[仕事番号][合計仕事時間] = 合計金額
	var dp: [][]int :: #[n + 1, dMax + 1]int
	; 初期化する代わりに、最初の所持金を1円として「最終的な金額 - 1」を答えとする。
	do dp[0][0] :: 1
	for i(0, n - 1)
		for j(0, dMax)
			var d: int :: works[i].d
			var c: int :: works[i].c
			var s: int :: works[i].s
			if(j + c <= d)
				do dp[i + 1][j + c] :: [dp[i + 1][j + c], dp[i][j] + s].max()
			end if
			do dp[i + 1][j] :: [dp[i + 1][j], dp[i][j]].max()
		end for
	end for
	var ans: int :: 0
	for j(0, dMax)
		do ans :: [ans, dp[n][j]].max()
	end for
	do ans :- 1
	do cui@print("\{ans}\n")
	
	class Work()
		+var d: int
		+var c: int
		+var s: int
		+func init(d: int, c: int, s: int): Work
			do me.d :: d
			do me.c :: c
			do me.s :: s
			ret me
		end func
		+*func cmp(t: kuin@Class): int
			if(me.d < (t $ Work).d)
				ret - 1
			elif(me.d > (t $ Work).d)
				ret 1
			end if
			ret 0
		end func
	end class
end func
AtCoder problem 011

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    DCS = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
    M = max(d for d, c, s in DCS)
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    for i in range(N):
        d, c, s = DCS[i]
        for j in range(M + 1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + c > d: continue
            dp[i + 1][j + c] = max(dp[i + 1][j + c], dp[i][j] + s)
    print(max(dp[N]))

if __name__ == '__main__':
    main()





Ruby solution: Task = Struct.new(:d, :c, :s)

n = readline.to_i
tasks = Array.new(n) do
    Task.new(*readline.split.map(&:to_i))
end

tasks.sort_by! { |t| -t.d }
profit = [0] * 5001

for t in tasks
    d, c, s = t.d, t.c, t.s
    for k in 0..(d - c)
        profit[k] = [profit[k], profit[k + c] + s].max
    end
end

puts profit[0]
AtCoder problem 011

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    DCS = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
    M = max(d for d, c, s in DCS)
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    for i in range(N):
        d, c, s = DCS[i]
        for j in range(M + 1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + c > d: continue
            dp[i + 1][j + c] = max(dp[i + 1][j + c], dp[i][j] + s)
    print(max(dp[N]))

if __name__ == '__main__':
    main()




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var works: []Work :: #[n]Work
	var dMax: int :: 0
	for i(0, n - 1)
		var d: int :: cui@inputInt()
		var c: int :: cui@inputInt()
		var s: int :: cui@inputInt()
		do works[i] :: (#Work).init(d, c, s)
		do dMax :: [dMax, d].max()
	end for
	do works.sort()
	
	; dp[仕事番号][合計仕事時間] = 合計金額
	var dp: [][]int :: #[n + 1, dMax + 1]int
	; 初期化する代わりに、最初の所持金を1円として「最終的な金額 - 1」を答えとする。
	do dp[0][0] :: 1
	for i(0, n - 1)
		for j(0, dMax)
			var d: int :: works[i].d
			var c: int :: works[i].c
			var s: int :: works[i].s
			if(j + c <= d)
				do dp[i + 1][j + c] :: [dp[i + 1][j + c], dp[i][j] + s].max()
			end if
			do dp[i + 1][j] :: [dp[i + 1][j], dp[i][j]].max()
		end for
	end for
	var ans: int :: 0
	for j(0, dMax)
		do ans :: [ans, dp[n][j]].max()
	end for
	do ans :- 1
	do cui@print("\{ans}\n")
	
	class Work()
		+var d: int
		+var c: int
		+var s: int
		+func init(d: int, c: int, s: int): Work
			do me.d :: d
			do me.c :: c
			do me.s :: s
			ret me
		end func
		+*func cmp(t: kuin@Class): int
			if(me.d < (t $ Work).d)
				ret - 1
			elif(me.d > (t $ Work).d)
				ret 1
			end if
			ret 0
		end func
	end class
end func
AtCoder problem 011

C++ solution:





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var works: []Work :: #[n]Work
	var dMax: int :: 0
	for i(0, n - 1)
		var d: int :: cui@inputInt()
		var c: int :: cui@inputInt()
		var s: int :: cui@inputInt()
		do works[i] :: (#Work).init(d, c, s)
		do dMax :: [dMax, d].max()
	end for
	do works.sort()
	
	; dp[仕事番号][合計仕事時間] = 合計金額
	var dp: [][]int :: #[n + 1, dMax + 1]int
	; 初期化する代わりに、最初の所持金を1円として「最終的な金額 - 1」を答えとする。
	do dp[0][0] :: 1
	for i(0, n - 1)
		for j(0, dMax)
			var d: int :: works[i].d
			var c: int :: works[i].c
			var s: int :: works[i].s
			if(j + c <= d)
				do dp[i + 1][j + c] :: [dp[i + 1][j + c], dp[i][j] + s].max()
			end if
			do dp[i + 1][j] :: [dp[i + 1][j], dp[i][j]].max()
		end for
	end for
	var ans: int :: 0
	for j(0, dMax)
		do ans :: [ans, dp[n][j]].max()
	end for
	do ans :- 1
	do cui@print("\{ans}\n")
	
	class Work()
		+var d: int
		+var c: int
		+var s: int
		+func init(d: int, c: int, s: int): Work
			do me.d :: d
			do me.c :: c
			do me.s :: s
			ret me
		end func
		+*func cmp(t: kuin@Class): int
			if(me.d < (t $ Work).d)
				ret - 1
			elif(me.d > (t $ Work).d)
				ret 1
			end if
			ret 0
		end func
	end class
end func


Ruby solution: Task = Struct.new(:d, :c, :s)

n = readline.to_i
tasks = Array.new(n) do
    Task.new(*readline.split.map(&:to_i))
end

tasks.sort_by! { |t| -t.d }
profit = [0] * 5001

for t in tasks
    d, c, s = t.d, t.c, t.s
    for k in 0..(d - c)
        profit[k] = [profit[k], profit[k + c] + s].max
    end
end

puts profit[0]
AtCoder problem 012

C++ solution:
#include <bits/stdc++.h>

#include <atcoder/all>



#define rep(i,n) for(int i=0;i < (n);i++)

#define ff first

#define ss second

#define pb push_back

#define ALL(a) (a).begin(),(a).end()



typedef long long ll;



using namespace std;

using namespace atcoder;



int i,j,k,tmp;

int ans = 0;



int main()

{

	cin.tie(0);	ios::sync_with_stdio(false);

	int H, W, Q; cin >> H >> W >> Q;

	int P[H][W]; rep(i,H){rep(j,W){P[i][j] = 0;}}

	dsu d(H * W);



	rep(i, Q){

		int t; cin >> t;

		if(t == 1){

			int r, c; cin >> r >> c; r--; c--;

			int now = r * W + c;

			P[r][c] = 1;

			if(r > 0){if(P[r-1][c] == 1){d.merge(now, now - W);}}

			if(r < H-1){if(P[r+1][c] == 1){d.merge(now, now + W);}}

			if(c > 0){if(P[r][c-1] == 1){d.merge(now, now - 1);}}

			if(c < W-1){if(P[r][c+1] == 1){d.merge(now, now + 1);}}

		}

		else{

			int ra, ca, rb, cb;

			cin >> ra >> ca >> rb >> cb;

			ra--; ca--; rb--; cb--;

			int posa = ra * W + ca;

			int posb = rb * W + cb;

			if(P[ra][ca] == 1 && P[rb][cb] == 1 && d.same(posa, posb) == true){

				cout << "Yes" << endl;

			}

			else{cout << "No" << 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

h, w = read_line.split.map(&.to_i)
uf = UnionFind.new(h * w)
red = Array.new(h) { [false] * w }

read_line.to_i.times do |i|
  query = read_line.split.map(&.to_i.pred)
  case query.shift
  when 0
    i, j = query
    red[i][j] = true
    [{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}].each do |(i2, j2)|
      if (0...h).includes?(i2) && (0...w).includes?(j2) && red[i2][j2]
        uf.unite(i * w + j, i2 * w + j2)
      end
    end
  when 1
    i1, j1, i2, j2 = query
    puts red[i1][j1] && uf.same?(i1 * w + j1, i2 * w + j2) ? "Yes" : "No"
  end
end
AtCoder problem 012

C++ solution:
#include <bits/stdc++.h>

#include <atcoder/all>



#define rep(i,n) for(int i=0;i < (n);i++)

#define ff first

#define ss second

#define pb push_back

#define ALL(a) (a).begin(),(a).end()



typedef long long ll;



using namespace std;

using namespace atcoder;



int i,j,k,tmp;

int ans = 0;



int main()

{

	cin.tie(0);	ios::sync_with_stdio(false);

	int H, W, Q; cin >> H >> W >> Q;

	int P[H][W]; rep(i,H){rep(j,W){P[i][j] = 0;}}

	dsu d(H * W);



	rep(i, Q){

		int t; cin >> t;

		if(t == 1){

			int r, c; cin >> r >> c; r--; c--;

			int now = r * W + c;

			P[r][c] = 1;

			if(r > 0){if(P[r-1][c] == 1){d.merge(now, now - W);}}

			if(r < H-1){if(P[r+1][c] == 1){d.merge(now, now + W);}}

			if(c > 0){if(P[r][c-1] == 1){d.merge(now, now - 1);}}

			if(c < W-1){if(P[r][c+1] == 1){d.merge(now, now + 1);}}

		}

		else{

			int ra, ca, rb, cb;

			cin >> ra >> ca >> rb >> cb;

			ra--; ca--; rb--; cb--;

			int posa = ra * W + ca;

			int posb = rb * W + cb;

			if(P[ra][ca] == 1 && P[rb][cb] == 1 && d.same(posa, posb) == true){

				cout << "Yes" << endl;

			}

			else{cout << "No" << endl;}

		}

	}



  return 0;

}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from typing import Iterable, List
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    Q = int(input())
    M = [[0] * W for _ in range(H)]
    uf = UnionFindTree(H * W)
    for _ in range(Q):
        t, *p = map(int, input().split())
        if t == 1:
            x, y = p
            x, y = x - 1, y - 1
            M[x][y] = 1
            for i, j in ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
                if i < 0 or i >= H or j < 0 or j >= W: continue
                if M[i][j]:
                    uf.union(x * W + y, i * W + j)
        else:
            xa, ya, xb, yb = map(lambda x: x - 1, p)
            print('Yes' if M[xa][ya] and uf.same(xa * W + ya, xb * W + yb) else 'No')

class UnionFindTree:
    __slots__ = ('parent')

    def __init__(self, n: int) -> None:
        self.parent = [-1] * n

    def find(self, x: int) -> int:
        p = self.parent
        par, seq = p[x], []
        while par >= 0:
            seq.append(x)
            x, par = par, p[par]
        for c in seq: p[c] = x
        return x

    def union(self, x: int, y: int) -> bool:
        x, y, p = self.find(x), self.find(y), self.parent
        if x == y: return False
        if p[x] > p[y]: x, y = y, x
        p[x], p[y] = p[x] + p[y], x
        return True

    def same(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)

    def size(self, x: int) -> int:
        return -self.parent[self.find(x)]

    def same_all(self, indices: Iterable[int]) -> bool:
        f, v = self.find, self.find(indices[0])
        return all(f(i) == v for i in indices)

    def groups(self) -> List[int]:
        return [i for i, p in enumerate(self.parent) if p < 0]

if __name__ == '__main__':
    main()
AtCoder problem 012

C++ solution:
#include <bits/stdc++.h>

#include <atcoder/all>



#define rep(i,n) for(int i=0;i < (n);i++)

#define ff first

#define ss second

#define pb push_back

#define ALL(a) (a).begin(),(a).end()



typedef long long ll;



using namespace std;

using namespace atcoder;



int i,j,k,tmp;

int ans = 0;



int main()

{

	cin.tie(0);	ios::sync_with_stdio(false);

	int H, W, Q; cin >> H >> W >> Q;

	int P[H][W]; rep(i,H){rep(j,W){P[i][j] = 0;}}

	dsu d(H * W);



	rep(i, Q){

		int t; cin >> t;

		if(t == 1){

			int r, c; cin >> r >> c; r--; c--;

			int now = r * W + c;

			P[r][c] = 1;

			if(r > 0){if(P[r-1][c] == 1){d.merge(now, now - W);}}

			if(r < H-1){if(P[r+1][c] == 1){d.merge(now, now + W);}}

			if(c > 0){if(P[r][c-1] == 1){d.merge(now, now - 1);}}

			if(c < W-1){if(P[r][c+1] == 1){d.merge(now, now + 1);}}

		}

		else{

			int ra, ca, rb, cb;

			cin >> ra >> ca >> rb >> cb;

			ra--; ca--; rb--; cb--;

			int posa = ra * W + ca;

			int posb = rb * W + cb;

			if(P[ra][ca] == 1 && P[rb][cb] == 1 && d.same(posa, posb) == true){

				cout << "Yes" << endl;

			}

			else{cout << "No" << endl;}

		}

	}



  return 0;

}





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var m: [][]bool :: #[h, w]bool
	var uni: UnionFind :: (#UnionFind).init(h * w)
	
	var q: int :: cui@inputInt()
	for(1, q)
		var t: int :: cui@inputInt()
		if(t = 1)
			var r: int :: cui@inputInt() - 1
			var c: int :: cui@inputInt() - 1
			do m[r][c] :: true
			var u: int :: r * w + c
			var dx: []int :: [1, 0, -1, 0]
			var dy: []int :: [0, 1, 0, -1]
			for i(0, 3)
				var c2: int :: c + dx[i]
				var r2: int :: r + dy[i]
				if(c2 < 0 | w <= c2)
					skip i
				end if
				if(r2 < 0 | h <= r2)
					skip i
				end if
				if(!m[r2][c2])
					skip i
				end if
				var v: int :: r2 * w + c2
				do uni.unite(u, v)
			end for
		else
			var ra: int :: cui@inputInt() - 1
			var ca: int :: cui@inputInt() - 1
			var rb: int :: cui@inputInt() - 1
			var cb: int :: cui@inputInt() - 1
			var ans: bool :: false
			if(m[ra][ca] & m[rb][cb])
				do ans :: uni.same(ra * w + ca, rb * w + cb)
			end if
			do cui@print(ans ?("Yes\n", "No\n"))
		end if
	end for
	
	class UnionFind()
		var parents: []int
		+func init(n: int): UnionFind
			do me.parents :: [-1].repeat(n)
			ret me
		end func
		+func size(x: int): int
			ret - me.parents[me.root(x)]
		end func
		+func same(x: int, y: int): bool
			ret me.root(x) = me.root(y)
		end func
		+func unite(x: int, y: int): bool
			do x :: me.root(x)
			do y :: me.root(y)
			if(x = y)
				ret false
			end if
			if(me.size(x) < me.size(y))
				do me.parents[y] :+ me.parents[x]
				do me.parents[x] :: y
			else
				do me.parents[x] :+ me.parents[y]
				do me.parents[y] :: x
			end if
			ret true
		end func
		
		func root(x: int): int
			if(me.parents[x] < 0)
				ret x
			else
				do me.parents[x] :: me.root(me.parents[x])
				ret me.parents[x]
			end if
		end func
	end class
end func
AtCoder problem 012

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

h, w = read_line.split.map(&.to_i)
uf = UnionFind.new(h * w)
red = Array.new(h) { [false] * w }

read_line.to_i.times do |i|
  query = read_line.split.map(&.to_i.pred)
  case query.shift
  when 0
    i, j = query
    red[i][j] = true
    [{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}].each do |(i2, j2)|
      if (0...h).includes?(i2) && (0...w).includes?(j2) && red[i2][j2]
        uf.unite(i * w + j, i2 * w + j2)
      end
    end
  when 1
    i1, j1, i2, j2 = query
    puts red[i1][j1] && uf.same?(i1 * w + j1, i2 * w + j2) ? "Yes" : "No"
  end
end


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from typing import Iterable, List
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    Q = int(input())
    M = [[0] * W for _ in range(H)]
    uf = UnionFindTree(H * W)
    for _ in range(Q):
        t, *p = map(int, input().split())
        if t == 1:
            x, y = p
            x, y = x - 1, y - 1
            M[x][y] = 1
            for i, j in ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
                if i < 0 or i >= H or j < 0 or j >= W: continue
                if M[i][j]:
                    uf.union(x * W + y, i * W + j)
        else:
            xa, ya, xb, yb = map(lambda x: x - 1, p)
            print('Yes' if M[xa][ya] and uf.same(xa * W + ya, xb * W + yb) else 'No')

class UnionFindTree:
    __slots__ = ('parent')

    def __init__(self, n: int) -> None:
        self.parent = [-1] * n

    def find(self, x: int) -> int:
        p = self.parent
        par, seq = p[x], []
        while par >= 0:
            seq.append(x)
            x, par = par, p[par]
        for c in seq: p[c] = x
        return x

    def union(self, x: int, y: int) -> bool:
        x, y, p = self.find(x), self.find(y), self.parent
        if x == y: return False
        if p[x] > p[y]: x, y = y, x
        p[x], p[y] = p[x] + p[y], x
        return True

    def same(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)

    def size(self, x: int) -> int:
        return -self.parent[self.find(x)]

    def same_all(self, indices: Iterable[int]) -> bool:
        f, v = self.find, self.find(indices[0])
        return all(f(i) == v for i in indices)

    def groups(self) -> List[int]:
        return [i for i, p in enumerate(self.parent) if p < 0]

if __name__ == '__main__':
    main()