|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class JM_32070 { |
| 7 | + static class Color { |
| 8 | + int box; |
| 9 | + boolean up; |
| 10 | + |
| 11 | + public Color(int box, boolean up) { |
| 12 | + this.box = box; |
| 13 | + this.up = up; |
| 14 | + } |
| 15 | + } |
| 16 | + static int N; |
| 17 | + static int[] A; |
| 18 | + static int[] B; |
| 19 | + static Color[][] colors; |
| 20 | + static int[] boxParent; |
| 21 | + static int[] subBoxCount; |
| 22 | + |
| 23 | + private static int findParent(int x) { |
| 24 | + if(x == boxParent[x]) return x; |
| 25 | + return boxParent[x] = findParent(boxParent[x]); |
| 26 | + } |
| 27 | + |
| 28 | + private static void union(int u, int v) { |
| 29 | + int uP = findParent(u); |
| 30 | + int vP = findParent(v); |
| 31 | + if(uP < vP) { |
| 32 | + boxParent[vP] = uP; |
| 33 | + subBoxCount[uP] += subBoxCount[vP]; |
| 34 | + } |
| 35 | + else if(uP > vP) { |
| 36 | + boxParent[uP] = vP; |
| 37 | + subBoxCount[vP] += subBoxCount[uP]; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private static void makeCycle() { |
| 42 | + for (int i = 1; i <= N; i++) { |
| 43 | + boxParent[i] = i; |
| 44 | + subBoxCount[i] = 1; |
| 45 | + } |
| 46 | + |
| 47 | + for (int i = 1; i <= N; i++) { // 같은 색의 공을 담은 box union |
| 48 | + union(colors[i][0].box, colors[i][1].box); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static boolean isTwoPairsOnTop() { |
| 53 | + int[] twoPairs = new int[N + 1]; |
| 54 | + for (int i = 1; i <= N; i++) { |
| 55 | + if(colors[i][0].up && colors[i][1].up) { |
| 56 | + int rootIdx = findParent(colors[i][0].box); |
| 57 | + twoPairs[rootIdx]++; |
| 58 | + } |
| 59 | + } |
| 60 | + for (int i = 1; i <= N; i++) { |
| 61 | + int rootIdx = findParent(colors[i][0].box); |
| 62 | + if(twoPairs[rootIdx] >= 2) return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + private static int solve() { |
| 68 | + makeCycle(); |
| 69 | + if(isTwoPairsOnTop()) return -1; |
| 70 | + |
| 71 | + int ans = 0; |
| 72 | + boolean[] check = new boolean[N + 1]; |
| 73 | + for (int i = 1; i <= N; i++) { |
| 74 | + int rootIdx = findParent(colors[i][0].box); |
| 75 | + if(check[rootIdx]) continue; |
| 76 | + |
| 77 | + check[rootIdx] = true; |
| 78 | + |
| 79 | + if(subBoxCount[rootIdx] >= 2) { |
| 80 | + ans += subBoxCount[rootIdx] + 1; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ans; |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) throws IOException { |
| 88 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 89 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 90 | + N = Integer.parseInt(st.nextToken()); |
| 91 | + |
| 92 | + colors = new Color[N + 1][2]; |
| 93 | + for (int i = 1; i <= N; i++) { |
| 94 | + st = new StringTokenizer(br.readLine()); |
| 95 | + int a = Integer.parseInt(st.nextToken()); |
| 96 | + int b = Integer.parseInt(st.nextToken()); |
| 97 | + if(colors[a][0] == null) colors[a][0] = new Color(i, true); |
| 98 | + else colors[a][1] = new Color(i, true); |
| 99 | + if(colors[b][0] == null) colors[b][0] = new Color(i, false); |
| 100 | + else colors[b][1] = new Color(i, false); |
| 101 | + } |
| 102 | + |
| 103 | + boxParent = new int[N + 1]; |
| 104 | + subBoxCount = new int[N + 1]; |
| 105 | + System.out.println(solve()); |
| 106 | + } |
| 107 | +} |
0 commit comments