답안 #306363

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
306363 2020-09-25T09:52:15 Z llaki 기지국 (IOI20_stations) Java 11
0 / 100
1601 ms 39084 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;
        if (isLarge) {
            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.
# 결과 실행 시간 메모리 Grader output
1 Correct 1215 ms 33232 KB Output is correct
2 Correct 1144 ms 37104 KB Output is correct
3 Correct 1575 ms 37932 KB Output is correct
4 Correct 1171 ms 35912 KB Output is correct
5 Correct 1326 ms 34096 KB Output is correct
6 Correct 1037 ms 39020 KB Output is correct
7 Correct 1139 ms 34028 KB Output is correct
8 Correct 177 ms 21036 KB Output is correct
9 Correct 183 ms 21452 KB Output is correct
10 Incorrect 163 ms 21024 KB Wrong query response.
# 결과 실행 시간 메모리 Grader output
1 Correct 1126 ms 39084 KB Output is correct
2 Incorrect 1145 ms 34528 KB Wrong query response.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1238 ms 36712 KB Wrong query response.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1601 ms 38672 KB Output is correct
2 Correct 1355 ms 36568 KB Output is correct
3 Correct 1218 ms 34748 KB Output is correct
4 Correct 170 ms 20920 KB Output is correct
5 Correct 183 ms 21388 KB Output is correct
6 Correct 160 ms 20900 KB Output is correct
7 Incorrect 1284 ms 33048 KB Wrong query response.
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1108 ms 36664 KB Wrong query response.
2 Halted 0 ms 0 KB -