Submission #2226098


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			int n = in.nextInt();
			int k = in.nextInt();
			int l = in.nextInt();
			Uft[] node1 = Uft.makeSet(n);
			Uft[] node2 = Uft.makeSet(n);
			for (int i = 0; i < k; i++) {
				int p = in.nextInt()-1;
				int q = in.nextInt()-1;
				Uft.unite(node1, p, q);
			}
			for (int i = 0; i < l; i++) {
				int r = in.nextInt()-1;
				int s = in.nextInt()-1;
				Uft.unite(node2, r, s);
			}
			Map<Long, Integer> roots = new HashMap<Long, Integer>();
			for (int i = 0; i < n; i++) {
				long code = (long)node1[i].p << 32 | node2[i].p;
				roots.merge(code, 1, Integer::sum);
			}
			for (int i = 0; i < n; i++) {
				if (i > 0)out.print(" ");
				long code = (long)node1[i].p << 32 | node2[i].p;
				out.print(roots.get(code));
			}

		}
	}

	static class Uft {

		int p, rank;
		Uft(int p) {
			this.p = p;
			this.rank = 0;
		}
		static Uft[] makeSet(int n) {
			Uft[] uft = new Uft[n];
			for (int i = 0; i < n; i++) {
				uft[i] = new Uft(i);
			}
			return uft;
		}
		static void unite(Uft[] uft, int x, int y) {
			link(uft, findSet(uft, x), findSet(uft, y));
		}
		static boolean isSame(Uft[] uft, int x, int y) {
			return findSet(uft, x) == findSet(uft, y);
		}
		static void link(Uft[] uft, int x, int y) {
			if (uft[x].rank > uft[y].rank) {
				uft[y].p = x;
			} else {
				uft[x].p = y;
				if (uft[x].rank == uft[y].rank) {
					uft[y].rank++;
				}
			}
		}
		static int findSet(Uft[] uft, int x) {
			if (uft[x].p != x) {
				x = findSet(uft, uft[x].p);
			}
			return uft[x].p;
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}

	}

}

Submission Info

Submission Time
Task D - Connectivity
User tutuz
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 3317 Byte
Status WA
Exec Time 772 ms
Memory 88652 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 3
WA × 15
Set Name Test Cases
Sample subtask0_0.txt, subtask0_1.txt, subtask0_2.txt
All subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask1_0.txt, subtask1_1.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_2.txt, subtask1_3.txt, subtask1_4.txt, subtask1_5.txt, subtask1_6.txt, subtask1_7.txt, subtask1_8.txt, subtask1_9.txt
Case Name Status Exec Time Memory
subtask0_0.txt AC 179 ms 23888 KB
subtask0_1.txt AC 149 ms 23892 KB
subtask0_2.txt AC 152 ms 23764 KB
subtask1_0.txt WA 352 ms 41964 KB
subtask1_1.txt WA 750 ms 88652 KB
subtask1_10.txt WA 335 ms 39684 KB
subtask1_11.txt WA 706 ms 86192 KB
subtask1_12.txt WA 596 ms 73272 KB
subtask1_13.txt WA 640 ms 74644 KB
subtask1_14.txt WA 649 ms 80904 KB
subtask1_2.txt WA 634 ms 74220 KB
subtask1_3.txt WA 772 ms 86684 KB
subtask1_4.txt WA 662 ms 82168 KB
subtask1_5.txt WA 359 ms 43192 KB
subtask1_6.txt WA 688 ms 83628 KB
subtask1_7.txt WA 646 ms 76896 KB
subtask1_8.txt WA 633 ms 84852 KB
subtask1_9.txt WA 625 ms 69256 KB