Submission #303204

# Submission time Handle Problem Language Result Execution time Memory
303204 2020-09-20T01:39:59 Z llaki Connecting Supertrees (IOI20_supertrees) Java 11
96 / 100
699 ms 64876 KB
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

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;
            }
        }
        @SuppressWarnings({"rawtypes", "unchecked"})
        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];
        @SuppressWarnings({"rawtypes", "unchecked"})
        ArrayList<Integer>[] G = new ArrayList[n];
        int[] used = new int[n];
        for (int i = 0; i < n; i++) G[i] = new ArrayList<>();
        for (int i = 0; i < c; i++) {
            if (C[i].size() <= 1) continue;
            ArrayList<Integer> nodes = C[i];
            for (int nd : nodes) {
                for (int j = 0; j < n; j++) {
                    if (p[nd][j] == 1 && j != nd) {
                        G[nd].add(j);
                    }
                }
            }
            ArrayList<Integer> cycle = new ArrayList<>();
            ArrayList<ArrayList<Integer>> comps = new ArrayList<>();
            for (int nd : nodes) {
                if (used[nd] > 0) continue;
                ArrayList<Integer> ls = new ArrayList<>();
                dfsMakeTree(nd, nd, b, G, used, ls);
                comps.add(ls);
                for (int x = 0; x < ls.size(); x++) {
                    for (int y = x + 1; y < ls.size(); y++) {
                        if (p[ls.get(x)][ls.get(y)] != 1 ||
                            p[ls.get(y)][ls.get(x)] != 1) return 0;
                    }
                }
                cycle.add(nd);
            }
            if (cycle.size() <= 1) continue;
            if (cycle.size() == 2) return 0;
            for (int x = 0; x < cycle.size(); x++) {
                for (int y = 0; y < cycle.size(); y++) {
                    if (x == y) continue;
                    if (p[cycle.get(x)][cycle.get(y)] != 2) return 0;
                }
            }
            for (int x = 0; x < comps.size(); x++) {
                ArrayList<Integer> l1 = comps.get(x);
                for (int y = x + 1; y < comps.size(); y++) {
                    ArrayList<Integer> l2 = comps.get(y);
                    for (int w : l1) {
                        for (int v : l2) {
                            if (p[w][v] != 2 || p[v][w] != 2) {
                                return 0;
                            }
                        }
                    }
                }
            }
            for (int j = 0; j < cycle.size(); j++) {
                int x = cycle.get(j), y = cycle.get((j + 1) % cycle.size());
                b[x][y] = 1;
                b[y][x] = 1;
            }
        }

        grader.build(b);
        return 1;
    }

    void dfsMakeTree(int v, int p, int[][] b, ArrayList<Integer>[] G, int[] used,
                     ArrayList<Integer> ls) {
        if (v != p) {
            b[v][p] = 1;
            b[p][v] = 1;
        }
        ls.add(v);
        used[v] = 1;
        for (int to : G[v]) {
            if (to == p || used[to] > 0) continue;
            dfsMakeTree(to, v, b, G, used, ls);
        }
    }

    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;
            }
        }
        @SuppressWarnings({"rawtypes", "unchecked"})
        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;
            }
        }
        @SuppressWarnings({"rawtypes", "unchecked"})
        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;
    }
}

/**
10
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
2 2 2 2 2 1 2 2 2 2
2 2 2 2 2 2 1 1 1 1
2 2 2 2 2 2 1 1 1 1
2 2 2 2 2 2 1 1 1 1
2 2 2 2 2 2 1 1 1 1
 * */
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10356 KB Output is correct
2 Correct 79 ms 10484 KB Output is correct
3 Correct 80 ms 10260 KB Output is correct
4 Correct 78 ms 10360 KB Output is correct
5 Correct 93 ms 10204 KB Output is correct
6 Correct 179 ms 15116 KB Output is correct
7 Correct 382 ms 56508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10356 KB Output is correct
2 Correct 79 ms 10484 KB Output is correct
3 Correct 80 ms 10260 KB Output is correct
4 Correct 78 ms 10360 KB Output is correct
5 Correct 93 ms 10204 KB Output is correct
6 Correct 179 ms 15116 KB Output is correct
7 Correct 382 ms 56508 KB Output is correct
8 Correct 78 ms 10244 KB Output is correct
9 Correct 78 ms 10360 KB Output is correct
10 Correct 79 ms 10232 KB Output is correct
11 Correct 79 ms 10248 KB Output is correct
12 Correct 184 ms 14832 KB Output is correct
13 Correct 447 ms 56792 KB Output is correct
14 Correct 75 ms 10232 KB Output is correct
15 Correct 78 ms 10216 KB Output is correct
16 Correct 98 ms 11488 KB Output is correct
17 Correct 148 ms 19204 KB Output is correct
18 Correct 79 ms 10232 KB Output is correct
19 Correct 83 ms 10472 KB Output is correct
20 Correct 318 ms 25248 KB Output is correct
21 Correct 446 ms 57504 KB Output is correct
22 Correct 424 ms 56052 KB Output is correct
23 Correct 428 ms 56800 KB Output is correct
24 Correct 427 ms 56324 KB Output is correct
25 Correct 159 ms 19188 KB Output is correct
26 Correct 162 ms 19520 KB Output is correct
27 Correct 427 ms 57144 KB Output is correct
28 Correct 422 ms 57224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 10220 KB Output is correct
2 Correct 77 ms 10352 KB Output is correct
3 Correct 77 ms 10472 KB Output is correct
4 Correct 80 ms 10468 KB Output is correct
5 Correct 79 ms 10340 KB Output is correct
6 Correct 79 ms 10228 KB Output is correct
7 Correct 78 ms 10356 KB Output is correct
8 Correct 189 ms 15172 KB Output is correct
9 Correct 450 ms 57000 KB Output is correct
10 Correct 80 ms 10336 KB Output is correct
11 Correct 80 ms 10360 KB Output is correct
12 Correct 152 ms 14324 KB Output is correct
13 Correct 457 ms 55872 KB Output is correct
14 Correct 77 ms 10344 KB Output is correct
15 Correct 80 ms 10244 KB Output is correct
16 Correct 92 ms 10856 KB Output is correct
17 Correct 143 ms 15392 KB Output is correct
18 Correct 79 ms 10360 KB Output is correct
19 Correct 79 ms 10232 KB Output is correct
20 Correct 76 ms 10360 KB Output is correct
21 Correct 300 ms 25940 KB Output is correct
22 Correct 428 ms 56060 KB Output is correct
23 Correct 423 ms 56168 KB Output is correct
24 Correct 462 ms 55932 KB Output is correct
25 Correct 173 ms 20160 KB Output is correct
26 Correct 165 ms 15204 KB Output is correct
27 Correct 479 ms 56708 KB Output is correct
28 Correct 434 ms 56192 KB Output is correct
29 Correct 187 ms 19468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10360 KB Output is correct
2 Correct 77 ms 10360 KB Output is correct
3 Correct 77 ms 10360 KB Output is correct
4 Correct 355 ms 25892 KB Output is correct
5 Correct 448 ms 57152 KB Output is correct
6 Correct 443 ms 56260 KB Output is correct
7 Correct 452 ms 55932 KB Output is correct
8 Correct 79 ms 10232 KB Output is correct
9 Correct 350 ms 28960 KB Output is correct
10 Correct 437 ms 56972 KB Output is correct
11 Correct 437 ms 56256 KB Output is correct
12 Correct 433 ms 57056 KB Output is correct
13 Correct 77 ms 10360 KB Output is correct
14 Correct 78 ms 10216 KB Output is correct
15 Correct 82 ms 10232 KB Output is correct
16 Correct 437 ms 34952 KB Output is correct
17 Correct 447 ms 57260 KB Output is correct
18 Correct 533 ms 60740 KB Output is correct
19 Correct 699 ms 64876 KB Output is correct
20 Correct 459 ms 56608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10356 KB Output is correct
2 Correct 79 ms 10484 KB Output is correct
3 Correct 80 ms 10260 KB Output is correct
4 Correct 78 ms 10360 KB Output is correct
5 Correct 93 ms 10204 KB Output is correct
6 Correct 179 ms 15116 KB Output is correct
7 Correct 382 ms 56508 KB Output is correct
8 Correct 78 ms 10244 KB Output is correct
9 Correct 78 ms 10360 KB Output is correct
10 Correct 79 ms 10232 KB Output is correct
11 Correct 79 ms 10248 KB Output is correct
12 Correct 184 ms 14832 KB Output is correct
13 Correct 447 ms 56792 KB Output is correct
14 Correct 75 ms 10232 KB Output is correct
15 Correct 78 ms 10216 KB Output is correct
16 Correct 98 ms 11488 KB Output is correct
17 Correct 148 ms 19204 KB Output is correct
18 Correct 79 ms 10232 KB Output is correct
19 Correct 83 ms 10472 KB Output is correct
20 Correct 318 ms 25248 KB Output is correct
21 Correct 446 ms 57504 KB Output is correct
22 Correct 424 ms 56052 KB Output is correct
23 Correct 428 ms 56800 KB Output is correct
24 Correct 427 ms 56324 KB Output is correct
25 Correct 159 ms 19188 KB Output is correct
26 Correct 162 ms 19520 KB Output is correct
27 Correct 427 ms 57144 KB Output is correct
28 Correct 422 ms 57224 KB Output is correct
29 Correct 76 ms 10220 KB Output is correct
30 Correct 77 ms 10352 KB Output is correct
31 Correct 77 ms 10472 KB Output is correct
32 Correct 80 ms 10468 KB Output is correct
33 Correct 79 ms 10340 KB Output is correct
34 Correct 79 ms 10228 KB Output is correct
35 Correct 78 ms 10356 KB Output is correct
36 Correct 189 ms 15172 KB Output is correct
37 Correct 450 ms 57000 KB Output is correct
38 Correct 80 ms 10336 KB Output is correct
39 Correct 80 ms 10360 KB Output is correct
40 Correct 152 ms 14324 KB Output is correct
41 Correct 457 ms 55872 KB Output is correct
42 Correct 77 ms 10344 KB Output is correct
43 Correct 80 ms 10244 KB Output is correct
44 Correct 92 ms 10856 KB Output is correct
45 Correct 143 ms 15392 KB Output is correct
46 Correct 79 ms 10360 KB Output is correct
47 Correct 79 ms 10232 KB Output is correct
48 Correct 76 ms 10360 KB Output is correct
49 Correct 300 ms 25940 KB Output is correct
50 Correct 428 ms 56060 KB Output is correct
51 Correct 423 ms 56168 KB Output is correct
52 Correct 462 ms 55932 KB Output is correct
53 Correct 173 ms 20160 KB Output is correct
54 Correct 165 ms 15204 KB Output is correct
55 Correct 479 ms 56708 KB Output is correct
56 Correct 434 ms 56192 KB Output is correct
57 Correct 187 ms 19468 KB Output is correct
58 Correct 80 ms 10236 KB Output is correct
59 Correct 76 ms 10228 KB Output is correct
60 Correct 96 ms 11000 KB Output is correct
61 Correct 139 ms 15092 KB Output is correct
62 Correct 80 ms 10344 KB Output is correct
63 Correct 91 ms 10256 KB Output is correct
64 Correct 79 ms 10196 KB Output is correct
65 Correct 393 ms 30132 KB Output is correct
66 Correct 158 ms 15720 KB Output is correct
67 Correct 141 ms 15312 KB Output is correct
68 Correct 155 ms 15480 KB Output is correct
69 Correct 177 ms 19960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10356 KB Output is correct
2 Correct 79 ms 10484 KB Output is correct
3 Correct 80 ms 10260 KB Output is correct
4 Correct 78 ms 10360 KB Output is correct
5 Correct 93 ms 10204 KB Output is correct
6 Correct 179 ms 15116 KB Output is correct
7 Correct 382 ms 56508 KB Output is correct
8 Correct 78 ms 10244 KB Output is correct
9 Correct 78 ms 10360 KB Output is correct
10 Correct 79 ms 10232 KB Output is correct
11 Correct 79 ms 10248 KB Output is correct
12 Correct 184 ms 14832 KB Output is correct
13 Correct 447 ms 56792 KB Output is correct
14 Correct 75 ms 10232 KB Output is correct
15 Correct 78 ms 10216 KB Output is correct
16 Correct 98 ms 11488 KB Output is correct
17 Correct 148 ms 19204 KB Output is correct
18 Correct 79 ms 10232 KB Output is correct
19 Correct 83 ms 10472 KB Output is correct
20 Correct 318 ms 25248 KB Output is correct
21 Correct 446 ms 57504 KB Output is correct
22 Correct 424 ms 56052 KB Output is correct
23 Correct 428 ms 56800 KB Output is correct
24 Correct 427 ms 56324 KB Output is correct
25 Correct 159 ms 19188 KB Output is correct
26 Correct 162 ms 19520 KB Output is correct
27 Correct 427 ms 57144 KB Output is correct
28 Correct 422 ms 57224 KB Output is correct
29 Correct 76 ms 10220 KB Output is correct
30 Correct 77 ms 10352 KB Output is correct
31 Correct 77 ms 10472 KB Output is correct
32 Correct 80 ms 10468 KB Output is correct
33 Correct 79 ms 10340 KB Output is correct
34 Correct 79 ms 10228 KB Output is correct
35 Correct 78 ms 10356 KB Output is correct
36 Correct 189 ms 15172 KB Output is correct
37 Correct 450 ms 57000 KB Output is correct
38 Correct 80 ms 10336 KB Output is correct
39 Correct 80 ms 10360 KB Output is correct
40 Correct 152 ms 14324 KB Output is correct
41 Correct 457 ms 55872 KB Output is correct
42 Correct 77 ms 10344 KB Output is correct
43 Correct 80 ms 10244 KB Output is correct
44 Correct 92 ms 10856 KB Output is correct
45 Correct 143 ms 15392 KB Output is correct
46 Correct 79 ms 10360 KB Output is correct
47 Correct 79 ms 10232 KB Output is correct
48 Correct 76 ms 10360 KB Output is correct
49 Correct 300 ms 25940 KB Output is correct
50 Correct 428 ms 56060 KB Output is correct
51 Correct 423 ms 56168 KB Output is correct
52 Correct 462 ms 55932 KB Output is correct
53 Correct 173 ms 20160 KB Output is correct
54 Correct 165 ms 15204 KB Output is correct
55 Correct 479 ms 56708 KB Output is correct
56 Correct 434 ms 56192 KB Output is correct
57 Correct 187 ms 19468 KB Output is correct
58 Correct 77 ms 10360 KB Output is correct
59 Correct 77 ms 10360 KB Output is correct
60 Correct 77 ms 10360 KB Output is correct
61 Correct 355 ms 25892 KB Output is correct
62 Correct 448 ms 57152 KB Output is correct
63 Correct 443 ms 56260 KB Output is correct
64 Correct 452 ms 55932 KB Output is correct
65 Correct 79 ms 10232 KB Output is correct
66 Correct 350 ms 28960 KB Output is correct
67 Correct 437 ms 56972 KB Output is correct
68 Correct 437 ms 56256 KB Output is correct
69 Correct 433 ms 57056 KB Output is correct
70 Correct 77 ms 10360 KB Output is correct
71 Correct 78 ms 10216 KB Output is correct
72 Correct 82 ms 10232 KB Output is correct
73 Correct 437 ms 34952 KB Output is correct
74 Correct 447 ms 57260 KB Output is correct
75 Correct 533 ms 60740 KB Output is correct
76 Correct 699 ms 64876 KB Output is correct
77 Correct 459 ms 56608 KB Output is correct
78 Correct 80 ms 10236 KB Output is correct
79 Correct 76 ms 10228 KB Output is correct
80 Correct 96 ms 11000 KB Output is correct
81 Correct 139 ms 15092 KB Output is correct
82 Correct 80 ms 10344 KB Output is correct
83 Correct 91 ms 10256 KB Output is correct
84 Correct 79 ms 10196 KB Output is correct
85 Correct 393 ms 30132 KB Output is correct
86 Correct 158 ms 15720 KB Output is correct
87 Correct 141 ms 15312 KB Output is correct
88 Correct 155 ms 15480 KB Output is correct
89 Correct 177 ms 19960 KB Output is correct
90 Incorrect 91 ms 10380 KB Answer gives possible 1 while actual possible 0
91 Halted 0 ms 0 KB -