Submission #1096616


Source Code Expand

// package atcoder.agc.agc010;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        a = in.nextInts(n);
        graph = buildGraph(in, n, n-1);


        int[][] vi = new int[n][2];
        for (int i = 0; i < n ; i++) {
            vi[i][0] = i;
            vi[i][1] = a[i];
        }
        Arrays.sort(vi, (x, y) -> x[1] - y[1]);


        boolean[] canWin = new boolean[n];
        boolean[] activated = new boolean[n];
        for (int i = 0; i < n ; ) {
            int j = i;
            while (j < n && vi[j][1] == vi[i][1]) {
                j++;
            }

            for (int k = i ; k < j ; k++) {
                int idx = vi[k][0];
                for (int to : graph[idx]) {
                    if (activated[to] && !canWin[to]) {
                        canWin[idx] = true;
                    }
                }
            }


            for (int k = i ; k < j ; k++) {
                activated[vi[k][0]] = true;
            }
            i = j;
        }


        StringBuilder line = new StringBuilder();
        for (int i = 0; i < n ; i++) {
            if (canWin[i]) {
                line.append(' ').append(i+1);
            }
        }
        if (line.length() >= 1) {
            out.println(line.substring(1));
        }
        out.flush();
    }

    static int[] a;
    static int[][] graph;

    static int[][] buildGraph(InputReader in, int n, int m) {
        int[][] edges = new int[m][];
        int[][] graph = new int[n][];
        int[] deg = new int[n];
        for (int i = 0 ; i < m ; i++) {
            int a = in.nextInt()-1;
            int b = in.nextInt()-1;
            deg[a]++;
            deg[b]++;
            edges[i] = new int[]{a, b};
        }
        for (int i = 0 ; i < n ; i++) {
            graph[i] = new int[deg[i]];
        }
        for (int i = 0 ; i < m ; i++) {
            int a = edges[i][0];
            int b = edges[i][1];
            graph[a][--deg[a]] = b;
            graph[b][--deg[b]] = a;
        }
        return graph;
    }



    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int[] nextInts(int n) {
            int[] ret = new int[n];
            for (int i = 0; i < n; i++) {
                ret[i] = nextInt();
            }
            return ret;
        }

        private int[][] nextIntTable(int n, int m) {
            int[][] ret = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ret[i][j] = nextInt();
                }
            }
            return ret;
        }

        private long[] nextLongs(int n) {
            long[] ret = new long[n];
            for (int i = 0; i < n; i++) {
                ret[i] = nextLong();
            }
            return ret;
        }

        private long[][] nextLongTable(int n, int m) {
            long[][] ret = new long[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ret[i][j] = nextLong();
                }
            }
            return ret;
        }

        private double[] nextDoubles(int n) {
            double[] ret = new double[n];
            for (int i = 0; i < n; i++) {
                ret[i] = nextDouble();
            }
            return ret;
        }

        private int next() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public char nextChar() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            if ('a' <= c && c <= 'z') {
                return (char) c;
            }
            if ('A' <= c && c <= 'Z') {
                return (char) c;
            }
            throw new InputMismatchException();
        }

        public String nextToken() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            StringBuilder res = new StringBuilder();
            do {
                res.append((char) c);
                c = next();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c-'0';
                c = next();
            } while (!isSpaceChar(c));
            return res*sgn;
        }

        public long nextLong() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c-'0';
                c = next();
            } while (!isSpaceChar(c));
            return res*sgn;
        }

        public double nextDouble() {
            return Double.valueOf(nextToken());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }

    static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }
}

Submission Info

Submission Time
Task F - Tree Game
User hamadu
Language Java8 (OpenJDK 1.8.0)
Score 1600
Code Size 6785 Byte
Status AC
Exec Time 266 ms
Memory 14528 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1600 / 1600
Status
AC × 3
AC × 53
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in46.txt, in47.txt, in48.txt, in49.txt, in5.txt, in50.txt, in6.txt, in7.txt, in8.txt, in9.txt
Case Name Status Exec Time Memory
in1.txt AC 196 ms 13764 KB
in10.txt AC 208 ms 14024 KB
in11.txt AC 207 ms 14016 KB
in12.txt AC 185 ms 13772 KB
in13.txt AC 191 ms 13520 KB
in14.txt AC 199 ms 13772 KB
in15.txt AC 208 ms 13896 KB
in16.txt AC 195 ms 13772 KB
in17.txt AC 197 ms 13772 KB
in18.txt AC 203 ms 14156 KB
in19.txt AC 200 ms 13772 KB
in2.txt AC 205 ms 13644 KB
in20.txt AC 217 ms 14284 KB
in21.txt AC 203 ms 14028 KB
in22.txt AC 204 ms 13760 KB
in23.txt AC 199 ms 13892 KB
in24.txt AC 200 ms 14152 KB
in25.txt AC 203 ms 13776 KB
in26.txt AC 209 ms 13768 KB
in27.txt AC 204 ms 14528 KB
in28.txt AC 209 ms 13768 KB
in29.txt AC 206 ms 14276 KB
in3.txt AC 195 ms 13632 KB
in30.txt AC 194 ms 13764 KB
in31.txt AC 192 ms 13652 KB
in32.txt AC 190 ms 13520 KB
in33.txt AC 199 ms 13768 KB
in34.txt AC 199 ms 13764 KB
in35.txt AC 198 ms 13768 KB
in36.txt AC 178 ms 13140 KB
in37.txt AC 188 ms 13772 KB
in38.txt AC 188 ms 13260 KB
in39.txt AC 192 ms 13764 KB
in4.txt AC 212 ms 13908 KB
in40.txt AC 196 ms 13644 KB
in41.txt AC 208 ms 13760 KB
in42.txt AC 206 ms 13640 KB
in43.txt AC 203 ms 13780 KB
in44.txt AC 212 ms 14416 KB
in45.txt AC 188 ms 13636 KB
in46.txt AC 186 ms 13644 KB
in47.txt AC 206 ms 14272 KB
in48.txt AC 203 ms 13648 KB
in49.txt AC 201 ms 13776 KB
in5.txt AC 205 ms 14160 KB
in50.txt AC 214 ms 14164 KB
in6.txt AC 198 ms 13648 KB
in7.txt AC 205 ms 13700 KB
in8.txt AC 200 ms 13760 KB
in9.txt AC 266 ms 13772 KB
sample1.txt AC 193 ms 13384 KB
sample2.txt AC 187 ms 13384 KB
sample3.txt AC 191 ms 14024 KB