Submission #1920683


Source Code Expand

import java.util.Optional;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Main main = new Main();
		main.solveA();
	}

	private void solveA() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int Q = sc.nextInt();
		UnionFind uf = new ArrayUnionFind(N);
		for (int i = 0; i < Q; i++) {
			int P = sc.nextInt();
			int A = sc.nextInt();
			int B = sc.nextInt();
			if (P == 0) {
				uf.union(A, B);
			} else {
				if (uf.judge(A, B)) {
					System.out.println("Yes");
				} else {
					System.out.println("No");
				}
			}
		}
	}

	interface UnionFind {
		void union(int A, int B);
		boolean judge(int A, int B);
	}

	/**
	 * 配列によるUnionFindの実装
	 */
	class ArrayUnionFind implements UnionFind {
		int[] parent;
		int[] rank;
		public ArrayUnionFind(int size) {
			parent = new int[size];
			for (int i = 0; i < size; i++) {
				parent[i] = i;
			}
			rank = new int[size];
		}

		@Override
		public void union(int A, int B) {
			int rootA = root(A);
			int rootB = root(B);
			if (rootA != rootB) {
				if (rank[rootA] < rank[rootB]) {
					parent[rootA] = rootB;
				} else {
					parent[rootB] = rootA;
					if (rank[rootA] == rank[rootB]) {
						rank[rootA]++;
					}
				}
			}
		}

		@Override
		public boolean judge(int A, int B) {
			return root(A) == root(B);
		}

		protected int root(int id) {
			if (parent[id] == id) {
				return id;
			}
			parent[id] = root(parent[id]);
			return parent[id];
		}
	}

	private void solveB() {
		BinaryIndexedTree bit = new BinaryIndexedTree(16);
		for (int i = 1; i < 15; i++) {
			bit.add(i, i);
			System.out.println(bit.getSum(i - 1));
			System.out.println(bit.getSum(i - 0));
			System.out.println(bit.getSum(i + 1));
		}
	}

	private void solveC() {
		Scanner sc = new Scanner(System.in);
		Graph graph = new ArrayGraph(6);
		graph.link(0, 1, 3);
		graph.link(0, 2, 4);
		graph.link(1, 3, 2);
		graph.link(1, 4, 5);
		graph.link(2, 3, 1);
		graph.link(2, 4, 8);
		graph.link(3, 5, 5);
		graph.link(4, 5, 20);
		graph.link(0, 5, 1000000000000L);
		FlowResolver fr = new DfsFlowResolver(graph);
		System.out.println(fr.maxFlow(0, 5));
	}

	private void solveD() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		System.out.println(N);
	}

	private void solveE() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		System.out.println(N);
	}

	private void solveF() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		System.out.println(N);
	}

	/**
	 * 1-indexedのBIT配列
	 */
	class BinaryIndexedTree {
		private long[] array;

		public BinaryIndexedTree(int size) {
			this.array = new long[size + 1];
		}

		/**
		 * 指定した要素に値を加算する
		 * 計算量はO(logN)
		 * @param index 加算する要素の添字
		 * @param value 加算する量
		 */
		public void add(int index, long value) {
			for (int i = index; i < array.length; i += (i & -i)) {
				array[i] += value;
			}
		}

		/**
		 * 1〜指定した要素までの和を取得する
		 * 計算量はO(logN)
		 * @param index 和の終端
		 * @return 1〜indexまでの和
		 */
		public long getSum(int index) {
			long sum = 0L;
			for (int i = index; i > 0; i -= (i & -i)) {
				sum += array[i];
			}
			return sum;
		}
	}

	interface Graph {
		void link(int from, int to, long cost);
		Optional<Long> getCost(int from, int to);
		int getVertexNum();
	}

	interface FlowResolver {
		long maxFlow(int from, int to);
	}

	/**
	 * グラフの行列による実装
	 * 接点数の大きいグラフで使うとMLEで死にそう
	 */
	class ArrayGraph implements Graph {
		private Long[][] costArray;
		private int vertexNum;

		public ArrayGraph(int n) {
			costArray = new Long[n][];
			for (int i = 0; i < n; i++) {
				costArray[i] = new Long[n];
			}
			vertexNum = n;
		}

		@Override
		public void link(int from, int to, long cost) {
			costArray[from][to] = new Long(cost);
		}

		@Override
		public Optional<Long> getCost(int from, int to) {
			return Optional.ofNullable(costArray[from][to]);
		}

		@Override
		public int getVertexNum() {
			return vertexNum;
		}
	}

	/**
	 * DFS(深さ優先探索)による実装
	 * 計算量はO(E*MaxFlow)のはず (E:辺の数, MaxFlow:最大フロー)
	 */
	class DfsFlowResolver implements FlowResolver {
		private Graph graph;
		public DfsFlowResolver(Graph graph) {
			this.graph = graph;
		}

		/**
		 * 最大フロー(最小カット)を求める
		 * @param from 始点(source)のID
		 * @param to 終点(target)のID
		 * @return 最大フロー(最小カット)
		 */
		public long maxFlow(int from, int to) {
			long sum = 0L;
			long currentFlow;
			do {
				currentFlow = flow(from, to, Long.MAX_VALUE / 3, new boolean[graph.getVertexNum()]);
				sum += currentFlow;
			} while (currentFlow > 0);
			return sum;
		}

		/**
		 * フローの実行 グラフの更新も行う
		 * @param from 現在いる節点のID
		 * @param to 終点(target)のID
		 * @param current_flow ここまでの流量
		 * @param passed 既に通った節点か否かを格納した配列
		 * @return 終点(target)に流した流量/戻りのグラフの流量
		 */
		private long flow(int from, int to, long current_flow, boolean[] passed) {
			passed[from] = true;
			if (from == to) {
				return current_flow;
			}
			for (int id = 0; id < graph.getVertexNum(); id++) {
				if (passed[id]) {
					continue;
				}
				Optional<Long> cost = graph.getCost(from, id);
				if (cost.orElse(0L) > 0) {
					long nextFlow = current_flow < cost.get() ? current_flow : cost.get();
					long returnFlow = flow(id, to, nextFlow, passed);
					if (returnFlow > 0) {
						graph.link(from, id, cost.get() - returnFlow);
						graph.link(id, from, graph.getCost(id, from).orElse(0L) + returnFlow);
						return returnFlow;
					}
				}
			}
			return 0L;
		}
	}
}

Submission Info

Submission Time
Task B - Union Find
User schwarzahl
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 6119 Byte
Status AC
Exec Time 1766 ms
Memory 150116 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 19
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 96 ms 21588 KB
subtask_01_01.txt AC 1224 ms 61512 KB
subtask_01_02.txt AC 95 ms 20052 KB
subtask_01_03.txt AC 1693 ms 76716 KB
subtask_01_04.txt AC 1731 ms 75948 KB
subtask_01_05.txt AC 404 ms 44608 KB
subtask_01_06.txt AC 418 ms 41940 KB
subtask_01_07.txt AC 1726 ms 87876 KB
subtask_01_08.txt AC 1766 ms 92704 KB
subtask_01_09.txt AC 104 ms 22100 KB
subtask_01_10.txt AC 113 ms 20684 KB
subtask_01_11.txt AC 1691 ms 66036 KB
subtask_01_12.txt AC 1724 ms 147268 KB
subtask_01_13.txt AC 1394 ms 92676 KB
subtask_01_14.txt AC 151 ms 24404 KB
subtask_01_15.txt AC 1672 ms 88568 KB
subtask_01_16.txt AC 1699 ms 97676 KB
subtask_01_17.txt AC 1290 ms 148132 KB
subtask_01_18.txt AC 1327 ms 150116 KB