Submission #2226187
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.Arrays; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; import java.util.StringTokenizer; 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(); UnionFind u1 = new UnionFind(n); UnionFind u2 = new UnionFind(n); for (int i = 0; i < k; i++) { int p = in.nextInt()-1; int q = in.nextInt()-1; u1.link(p, q); } for (int i = 0; i < l; i++) { int r = in.nextInt()-1; int s = in.nextInt()-1; u2.link(r, s); } Map<Long, Integer> roots = new HashMap<Long, Integer>(); for (int i = 0; i < n; i++) { long code = (long)u1.data[i] << 32 | u2.data[i]; roots.merge(code, 1, Integer::sum); } for (int i = 0; i < n; i++) { if (i > 0)out.print(" "); long code = (long)u1.data[i] << 32 | u2.data[i]; out.print(roots.get(code)); } } public class UnionFind { int[] data; public UnionFind(int n) { data = new int[n]; Arrays.fill(data, -1); } boolean link(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) { data[y] += data[x]; data[x] = y; } else { data[x] += data[y]; data[y] = x; } } return x != y; } int root(int x) { return data[x] < 0 ? x : (data[x] = root(data[x])); } } } 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 | 3005 Byte |
Status | WA |
Exec Time | 597 ms |
Memory | 67232 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 400 | ||||
Status |
|
|
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 | WA | 145 ms | 23252 KB |
subtask0_1.txt | WA | 151 ms | 26452 KB |
subtask0_2.txt | WA | 154 ms | 25812 KB |
subtask1_0.txt | WA | 340 ms | 43896 KB |
subtask1_1.txt | WA | 480 ms | 54080 KB |
subtask1_10.txt | WA | 337 ms | 39760 KB |
subtask1_11.txt | WA | 465 ms | 45108 KB |
subtask1_12.txt | WA | 458 ms | 55572 KB |
subtask1_13.txt | WA | 458 ms | 52936 KB |
subtask1_14.txt | WA | 576 ms | 64624 KB |
subtask1_2.txt | WA | 450 ms | 48764 KB |
subtask1_3.txt | WA | 450 ms | 55448 KB |
subtask1_4.txt | WA | 597 ms | 67232 KB |
subtask1_5.txt | WA | 328 ms | 41216 KB |
subtask1_6.txt | WA | 442 ms | 43776 KB |
subtask1_7.txt | WA | 472 ms | 56664 KB |
subtask1_8.txt | WA | 485 ms | 58516 KB |
subtask1_9.txt | WA | 575 ms | 50688 KB |