Submission #303168

#TimeUsernameProblemLanguageResultExecution timeMemory
303168llakiConnecting Supertrees (IOI20_supertrees)Java
40 / 100
601 ms58756 KiB
import java.util.ArrayList; import java.util.Arrays; public class supertrees { int construct(int[][] p) { int n = p.length; if (areAllOnes(p)) { return constructForAllOnes(n); } if (areAllAtMostOnes(p)) { return constructForAllAtMostOnes(p, n); } if (areAllZeroOrTwo(p)) { return constructAllZeroOrTwo(p, n); } if (areAllAtMostTwo(p)) { return constructAtMostTwo(p, n); } int[][] answer = new int[n][n]; grader.build(answer); return 1; } int constructAtMostTwo(int[][] p, int n) { int[] comp = new int[n]; Arrays.fill(comp, -1); int c = 0; for (int i = 0; i < n; i++) { if (comp[i] != -1) continue; comp[i] = c; for (int j = i + 1; j < n; j++) { if (p[i][j] > 0) { if (comp[j] != -1) return 0; comp[j] = c; } } c++; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (p[i][j] > 0 && comp[i] != comp[j]) return 0; if (p[i][j] == 0 && comp[i] == comp[j]) return 0; } } ArrayList<Integer>[] C = new ArrayList[c]; for (int i = 0; i < c; i++) { C[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { C[comp[i]].add(i); } int[][] b = new int[n][n]; for (int i = 0; i < c; i++) { if (C[i].size() <= 1) continue; ArrayList<Integer> nodes = C[i]; int first = nodes.get(0); ArrayList<Integer> d1 = new ArrayList<>(); ArrayList<Integer> d2 = new ArrayList<>(); for (int j = 1; j < nodes.size(); j++) { int nd = nodes.get(j); if (p[first][nd] == 1) { d1.add(nd); } else { d2.add(nd); } } for (int dist1 : d1) { b[dist1][first] = 1; b[first][dist1] = 1; } if (d2.isEmpty()) { continue; } int b1 = d2.get(0); ArrayList<Integer> B1 = new ArrayList<>(); ArrayList<Integer> B2 = new ArrayList<>(); for (int j = 1; j < d2.size(); j++) { int nd = d2.get(j); if (p[nd][b1] == 1) { B1.add(nd); } else { B2.add(nd); } } for (int b2 : B1) { b[b2][b1] = 1; b[b1][b2] = 1; } B2.add(b1); for (int j = 0; j < B2.size() - 1; j++) { int nd1 = B2.get(j), nd2 = B2.get(j + 1); b[nd1][nd2] = 1; b[nd2][nd1] = 1; } b[first][B2.get(0)] = 1; b[B2.get(0)][first] = 1; b[first][B2.get(B2.size() - 1)] = 1; b[B2.get(B2.size() - 1)][first] = 1; } grader.build(b); return 1; } int constructAllZeroOrTwo(int[][] p, int n) { int[] comp = new int[n]; Arrays.fill(comp, -1); int c = 0; for (int i = 0; i < n; i++) { if (comp[i] != -1) continue; comp[i] = c; for (int j = i + 1; j < n; j++) { if (p[i][j] > 0) { if (comp[j] != -1) return 0; comp[j] = c; } } c++; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (p[i][j] > 0 && comp[i] != comp[j]) return 0; if (p[i][j] == 0 && comp[i] == comp[j]) return 0; } } ArrayList<Integer>[] C = new ArrayList[c]; for (int i = 0; i < c; i++) { C[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { C[comp[i]].add(i); } int[][] b = new int[n][n]; for (int i = 0; i < c; i++) { if (C[i].size() <= 1) continue; if (C[i].size() == 2) return 0; for (int j = 1; j < C[i].size() - 1; j++) { b[C[i].get(j)][C[i].get(j + 1)] = 1; b[C[i].get(j + 1)][C[i].get(j)] = 1; } b[C[i].get(0)][C[i].get(1)] = 1; b[C[i].get(1)][C[i].get(0)] = 1; b[C[i].get(0)][C[i].get(C[i].size() - 1)] = 1; b[C[i].get(C[i].size() - 1)][C[i].get(0)] = 1; } grader.build(b); return 1; } boolean areAllAtMostTwo(int[][] p) { for (int i = 0; i < p.length; i++) { for (int j = 0; j < p[i].length; j++) { if (p[i][j] > 2) return false; } } return true; } int constructForAllAtMostOnes(int[][] p, int n) { int[] comp = new int[n]; int[][] b = new int[n][n]; Arrays.fill(comp, -1); int c = 0; for (int i = 0; i < n; i++) { if (comp[i] != -1) continue; comp[i] = c; for (int j = i + 1; j < n; j++) { if (p[i][j] > 0) { if (comp[j] != -1) return 0; comp[j] = c; } } c++; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (p[i][j] > 0 && comp[i] != comp[j]) return 0; if (p[i][j] == 0 && comp[i] == comp[j]) return 0; } } ArrayList<Integer>[] C = new ArrayList[c]; for (int i = 0; i < c; i++) { C[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { C[comp[i]].add(i); } for (int i = 0; i < c; i++) { for (int j = 0; j < C[i].size() - 1; j++) { b[C[i].get(j)][C[i].get(j + 1)] = 1; b[C[i].get(j + 1)][C[i].get(j)] = 1; } } grader.build(b); return 1; } boolean areAllZeroOrTwo(int[][] p) { for (int i = 0; i < p.length; i++) { for (int j = 0; j < p[i].length; j++) { if (j != i && p[i][j] % 2 == 1) return false; } } return true; } boolean areAllAtMostOnes(int[][] p) { for (int i = 0; i < p.length; i++) { for (int j = 0; j < p[i].length; j++) { if (p[i][j] > 1) return false; } } return true; } boolean areAllOnes(int[][] p) { for (int i = 0; i < p.length; i++) { for (int j = 0; j < p[i].length; j++) { if (p[i][j] != 1) return false; } } return true; } int constructForAllOnes(int n) { int[][] res = new int[n][n]; for (int i = 0; i < n - 1; i++) { res[i][i + 1] = 1; res[i + 1][i] = 1; } grader.build(res); return 1; } }

Compilation message (stderr)

Note: supertrees.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...