結果

問題 No.483 マッチ並べ
ユーザー 37zigen
提出日時 2017-02-11 04:45:52
言語 Java
(openjdk 23)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 1,459 bytes
コンパイル時間 4,005 ms
コンパイル使用メモリ 77,736 KB
実行使用メモリ 42,120 KB
最終ジャッジ日時 2024-12-29 11:59:44
合計ジャッジ時間 14,270 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main {
	static int N;
	static int[] r0, c0, r1, c1;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		r0 = new int[N];
		c0 = new int[N];
		r1 = new int[N];
		c1 = new int[N];
		DJSet ds = new DJSet(100 * 100);
		for (int i = 0; i < N; ++i) {
			r0[i] = sc.nextInt() - 1;
			c0[i] = sc.nextInt() - 1;
			r1[i] = sc.nextInt() - 1;
			c1[i] = sc.nextInt() - 1;
			ds.setUnion(r0[i] + c0[i] * 100, r1[i] + c1[i] * 100);
		}
		for (int i = 0; i < 100; ++i) {
			for (int j = 0; j < 100; ++j) {
				if (ds.edgeNumber[ds.root(i + j * 100)] >= -ds.upper[ds.root(i + j * 100)] + 1) {
					System.out.println("NO");
					return;
				}
			}
		}
		System.out.println("YES");
	}

	static class DJSet {
		int n;
		int[] upper;
		int[] edgeNumber;

		public DJSet(int n) {
			this.n = n;
			upper = new int[n];
			edgeNumber = new int[n];
			Arrays.fill(upper, -1);
		}

		boolean equiv(int x, int y) {
			return root(x) == root(y);
		}

		int root(int x) {
			return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
		}

		void setUnion(int x, int y) {
			x = root(x);
			y = root(y);

			if (x != y) {
				if (upper[x] < upper[y]) {
					int tmp = x;
					x = y;
					y = tmp;
				}
				upper[y] += upper[x];
				upper[x] = y;
				edgeNumber[y] += edgeNumber[x];
			}
			++edgeNumber[y];
		}

	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0