結果

問題 No.1477 Lamps on Graph
ユーザー neko_the_shadowneko_the_shadow
提出日時 2021-04-18 14:26:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,202 ms / 2,000 ms
コード長 7,048 bytes
コンパイル時間 3,684 ms
コンパイル使用メモリ 96,152 KB
実行使用メモリ 82,336 KB
最終ジャッジ日時 2024-07-04 04:33:14
合計ジャッジ時間 28,570 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
38,412 KB
testcase_01 AC 82 ms
38,204 KB
testcase_02 AC 80 ms
38,736 KB
testcase_03 AC 82 ms
38,612 KB
testcase_04 AC 80 ms
38,204 KB
testcase_05 AC 81 ms
38,312 KB
testcase_06 AC 82 ms
38,496 KB
testcase_07 AC 81 ms
38,516 KB
testcase_08 AC 81 ms
38,708 KB
testcase_09 AC 82 ms
38,540 KB
testcase_10 AC 81 ms
38,160 KB
testcase_11 AC 81 ms
38,420 KB
testcase_12 AC 816 ms
62,276 KB
testcase_13 AC 757 ms
61,088 KB
testcase_14 AC 856 ms
61,896 KB
testcase_15 AC 539 ms
52,328 KB
testcase_16 AC 467 ms
53,912 KB
testcase_17 AC 429 ms
53,308 KB
testcase_18 AC 799 ms
62,768 KB
testcase_19 AC 755 ms
59,312 KB
testcase_20 AC 434 ms
50,276 KB
testcase_21 AC 692 ms
61,772 KB
testcase_22 AC 330 ms
48,360 KB
testcase_23 AC 601 ms
53,868 KB
testcase_24 AC 955 ms
66,588 KB
testcase_25 AC 483 ms
51,048 KB
testcase_26 AC 888 ms
68,652 KB
testcase_27 AC 607 ms
53,616 KB
testcase_28 AC 695 ms
58,212 KB
testcase_29 AC 579 ms
56,280 KB
testcase_30 AC 524 ms
56,924 KB
testcase_31 AC 471 ms
54,344 KB
testcase_32 AC 997 ms
82,336 KB
testcase_33 AC 940 ms
73,448 KB
testcase_34 AC 900 ms
68,144 KB
testcase_35 AC 1,184 ms
78,420 KB
testcase_36 AC 1,193 ms
76,428 KB
testcase_37 AC 1,065 ms
70,692 KB
testcase_38 AC 1,202 ms
80,164 KB
testcase_39 AC 1,150 ms
76,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package _1477;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public void exec() {
        int n = stdin.nextInt();
        int m = stdin.nextInt();
        int[] a = stdin.nextIntArray(n);
        
        List<List<Integer>> nexts = IntStream.range(0, n).mapToObj(unused -> new ArrayList<Integer>()).collect(Collectors.toList());
        
        for (int i = 0; i < m; i++) {
            int u = stdin.nextInt()-1;
            int v = stdin.nextInt()-1;
            if (a[u]<a[v]) nexts.get(u).add(v);
            if (a[v]<a[u]) nexts.get(v).add(u);
        }
        
        boolean[] light = new boolean[n];
        int k = stdin.nextInt();
        for (int i = 0; i < k; i++ ) {
            int b = stdin.nextInt()-1;
            light[b] = true;
        }
        
        Tsort tsort = new Tsort(n);
        for (int i = 0; i < n; i++) {
            for (int j : nexts.get(i)) {
                tsort.add(i, j);
            }
        }
        
        List<Integer> p = tsort.run();
        List<Integer> q = new ArrayList<>();
        for (int cur : p) {
            if (!light[cur]) continue;
            q.add(cur);
            light[cur] = !light[cur];
            for (int nxt : nexts.get(cur)) light[nxt] = !light[nxt];
        }
        
        boolean all = true;
        for (int i = 0; i < n; i++) {
            if (light[i]) {
                all = false;
                break;
            }
        }
        
        if (all) {
            stdout.println(q.size());
            for (int v : q) {
                stdout.println(v+1);
            }
        } else {
            stdout.println(-1);
        }
    }
    
    
    public class Tsort {
        
        private int[] count;
        private List<List<Integer>> nexts;
        
        public Tsort(int n) {
            this.count = new int[n];
            this.nexts = IntStream.range(0, n).mapToObj(unused -> new ArrayList<Integer>()).collect(Collectors.toList());
        }
        
        public void add(int from, int to) {
            this.count[to]++;
            this.nexts.get(from).add(to);
        }
        
        public List<Integer> run() {
            Deque<Integer> stack = IntStream.range(0, this.count.length).filter(i -> count[i]==0).boxed().collect(Collectors.toCollection(ArrayDeque::new));
            List<Integer> result = new ArrayList<>();
            
            while (!stack.isEmpty()) {
                int cur = stack.removeFirst();
                result.add(cur);
                for (int nxt : nexts.get(cur)) {
                   this.count[nxt]--;
                   if (this.count[nxt]==0) {
                       stack.add(nxt);
                   }
                }
            }
            return result;
        }
    }

    private static final Stdin stdin = new Stdin(System.in);
    private static final Stdout stdout = new Stdout(System.out);

    public static void main(String[] args) {
        try {
            new Main().exec();
        } finally {
            stdout.flush();
        }
    }

    public static class Stdin {
        private Deque<String> queue;
        private BufferedReader in;
        private Pattern space;

        public Stdin(InputStream in) {
            this.queue = new ArrayDeque<>();
            this.in = new BufferedReader(new InputStreamReader(in));
            this.space = Pattern.compile(" ");
        }

        public String nextString() {
            if (queue.isEmpty()) {
                try {
                    String line = in.readLine();
                    if (line == null) {
                        throw new EOFException();
                    }
                    space.splitAsStream(line).forEach(this.queue::addLast);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return queue.removeFirst();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public long nextLong() {
            return Long.parseLong(nextString());
        }
        
        public BigInteger nextBigInteger() {
            return new BigInteger(nextString());
        }

        public String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) a[i] = nextString();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public double[] nextDoubleArray(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) a[i] = nextDouble();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
        
        public BigInteger[] nexBigIntegerArray(int n) {
            BigInteger[] a = new BigInteger[n];
            for (int i = 0; i < n; i++) a[i] = nextBigInteger();
            return a;
        }
        
        public List<Integer> nextIntegerList(int n) {
            return nextList(n, this::nextInt);
        }
        
        public List<Long> nextLongList(int n) {
            return nextList(n, this::nextLong);
        }
        
        public List<Double> nextDoubleList(int n) {
            return nextList(n, this::nextDouble);
        }
        
        public List<String> nextStringList(int n) {
            return nextList(n, this::nextString);
        }
        
        public List<BigInteger> nextBigIntegerList(int n) {
            return nextList(n, this::nextBigInteger);
        }
        
        private <T> List<T> nextList(int n, Supplier<T> supplier) {
            List<T> a = new ArrayList<>();
            for (int i = 0; i < n; i++) a.add(supplier.get());
            return a;
        }
    }

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout(PrintStream stdout) {
            this.stdout =  new PrintWriter(stdout, false);
        }

        public void println(Object ... objs) {
            for (int i = 0, len = objs.length; i < len; i++) {
                stdout.print(objs[i]);
                if (i != len-1) stdout.print(' ');
            }
            stdout.println();
        }

        public void flush() {
            stdout.flush();
        }
    }
}
0