Submission #306367

# Submission time Handle Problem Language Result Execution time Memory
306367 2020-09-25T10:05:48 Z llaki Stations (IOI20_stations) Java 11
13 / 100
1560 ms 38524 KB
import java.util.ArrayList;

public class stations {
    void dfsLabel(int v, int p, ArrayList<Integer>[] G, int[] labels) {
        int min = label;
        for (int to : G[v]) {
            if (p != to) {
                dfsLabel(to, v, G, labels);
            }
        }
        int max = label;
        labels[v] = 1000 * min + max;
        label++;
    }

    int label = 0;

    int[] label(int n, int k, int[] u, int[] v) {
        ArrayList<Integer>[] G = new ArrayList[n];
        for (int i = 0; i < G.length; i++) {
            G[i] = new ArrayList<>();
        }
        for (int i = 0; i < u.length; i++) {
            G[u[i]].add(v[i]);
            G[v[i]].add(u[i]);
        }
        int[] labels = new int[n];
        if (k > 1000) {
            label = 0;
            dfsLabel(0, 0, G, labels);
            return labels;
        }
        boolean allTask2 = true;
        for (int i = 0; i < n - 1; i++) {
            if (u[i] == i + 1 && v[i] == i / 2) {
                continue;
            }
            if (v[i] == i + 1 && u[i] == i / 2) {
                continue;
            }
            allTask2 = false;
            break;
        }
        int maxSize = 0;
        for (int i = 0; i < n; i++) {
            maxSize = Math.max(maxSize, G[i].size());
        }
        if (allTask2 && maxSize > 2) {
            for (int i = 0; i < n; i++) {
                labels[i] = i;
            }
            return labels;
        }
        int leaf = -1;
        for (int i = 0; i < n; i++) {
            if (G[i].size() == 1) {
                leaf = i;
                break;
            }
        }
        int cur = 0;
        int node = leaf;
        int prev = -1;
        while (true) {
            labels[node] = cur;
            cur++;
            boolean found = false;
            for (int to : G[node]) {
                if (to == prev) continue;
                prev = node;
                node = to;
                found = true;
                break;
            }
            if (!found) break;
        }
        return labels;
    }

    boolean isAncestor(int a, int b) {
        if (a == b) return true;
        while (a > 0) {
            a = (a - 1) / 2;
            if (a == b) return true;
        }
        return false;
    }

    int find_next_station(int s, int t, int[] c) {
        boolean isLarge = false;
        if (s >= 1000 || t >= 1000) isLarge = true;
        for (int nd : c) if (nd >= 1000) isLarge = true;
        boolean isTask2 = true;
        for (int nd : c) {
            if (nd != s / 2 && nd != 2 * s + 1 && nd != 2 * s + 2) isTask2 = false;
        }
        if (isLarge && !isTask2) {
            int dest = t % 1000;
            for (int nd : c) {
                if (nd % 1000 > s % 1000) continue;
                int min = nd / 1000, max = nd % 1000;
                if (min <= dest && dest <= max) {
                    return nd;
                }
            }
            for (int nd : c) {
                if (nd % 1000 > s % 1000) return nd;
            }
        }
        boolean isBamboo = true;
        if (c.length > 2) {
            isBamboo = false;
        }
        else {
            for (int nd : c) {
                if (Math.abs(nd - s) > 1) isBamboo = false;
            }
        }
        if (!isBamboo) {
            if (!isAncestor(t, s)) {
                return (s - 1) / 2;
            }
            for (int nd : c) {
                if (nd < s) continue;
                if (isAncestor(t, nd)) {
                    return nd;
                }
            }
        } else {
            for (int nd : c) {
                if (s <= nd && nd <= t) return nd;
                if (s >= nd && nd >= t) return nd;
            }
        }
        return -1;
    }
}

Compilation message

Note: stations.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 1136 ms 35488 KB Output is correct
2 Correct 1085 ms 36260 KB Output is correct
3 Correct 1480 ms 36416 KB Output is correct
4 Correct 1157 ms 34288 KB Output is correct
5 Correct 1097 ms 35428 KB Output is correct
6 Correct 1137 ms 37676 KB Output is correct
7 Correct 1089 ms 33484 KB Output is correct
8 Correct 183 ms 20852 KB Output is correct
9 Correct 188 ms 21368 KB Output is correct
10 Correct 162 ms 20892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1290 ms 38400 KB Output is correct
2 Correct 1149 ms 33732 KB Output is correct
3 Correct 1560 ms 37104 KB Output is correct
4 Correct 1286 ms 35404 KB Output is correct
5 Correct 1212 ms 33836 KB Output is correct
6 Correct 1110 ms 35352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1206 ms 38168 KB Wrong query response.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1434 ms 38524 KB Output is correct
2 Correct 1129 ms 34996 KB Output is correct
3 Incorrect 1209 ms 35304 KB Wrong query response.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1119 ms 36112 KB Wrong query response.
2 Halted 0 ms 0 KB -