|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static TreeMap<String, Integer> country; |
| 9 | + private static Map<Integer, String> map; |
| 10 | + private static int[] uf; |
| 11 | + private static int N, M; |
| 12 | +
|
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | +
|
| 16 | + TreeSet<String> answer = new TreeSet<>(); |
| 17 | +
|
| 18 | + for (int i = 1; i <= N; i++) { |
| 19 | + answer.add(map.get(uf[i])); |
| 20 | + } |
| 21 | +
|
| 22 | + bw.write(answer.size() + "\n"); |
| 23 | +
|
| 24 | + for (String element : answer) { |
| 25 | + bw.write("Kingdom of " + element + "\n"); |
| 26 | + } |
| 27 | + bw.flush(); |
| 28 | + bw.close(); |
| 29 | + br.close(); |
| 30 | + } |
| 31 | +
|
| 32 | + private static void init() throws IOException { |
| 33 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 34 | +
|
| 35 | + N = Integer.parseInt(st.nextToken()); |
| 36 | + M = Integer.parseInt(st.nextToken()); |
| 37 | +
|
| 38 | + country = new TreeMap<>(); |
| 39 | + map = new HashMap<>(); |
| 40 | + uf = new int[N+1]; |
| 41 | +
|
| 42 | + for (int i = 1; i <= N; i++) { |
| 43 | + uf[i] = i; |
| 44 | + } |
| 45 | +
|
| 46 | + for (int i = 1; i <= N; i++) { |
| 47 | + String[] input = br.readLine().split(" "); |
| 48 | + country.put(input[2], i); |
| 49 | + map.put(i, input[2]); |
| 50 | + } |
| 51 | +
|
| 52 | + for (int i = 0; i < M; i++) { |
| 53 | + String[] input = br.readLine().split(" "); |
| 54 | + String country1 = input[2].split(",")[0]; |
| 55 | + String country2 = input[4].split(",")[0]; |
| 56 | +
|
| 57 | + int index1 = country.get(country1); |
| 58 | + int index2 = country.get(country2); |
| 59 | + int result = Integer.parseInt(input[4].split(",")[1]); |
| 60 | +
|
| 61 | + union(index1, index2, result); |
| 62 | + } |
| 63 | +
|
| 64 | + } |
| 65 | +
|
| 66 | + private static void union(int x, int y, int result) { |
| 67 | + int X = find(x); |
| 68 | + int Y = find(y); |
| 69 | +
|
| 70 | + int[] temp; |
| 71 | + if (X == Y) { |
| 72 | + if (result == 1) temp = new int[]{0, x, Y}; |
| 73 | + else temp = new int[]{0, X, y}; |
| 74 | +
|
| 75 | + for (int i = 1; i <= N; i++) { |
| 76 | + if (uf[i] == X) uf[i] = temp[result]; |
| 77 | + } |
| 78 | + } else { |
| 79 | + if (result == 1) { |
| 80 | + for (int i = 1; i <= N; i++) { |
| 81 | + if (uf[i] == Y) uf[i] = X; |
| 82 | + } |
| 83 | + } else { |
| 84 | + for (int i = 1; i <= N; i++) { |
| 85 | + if (uf[i] == X) uf[i] = Y; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + private static int find(int x) { |
| 92 | + if(uf[x] == x) return x; |
| 93 | +
|
| 94 | + return uf[x] = find(uf[x]); |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
0 commit comments