結果

問題 No.1477 Lamps on Graph
ユーザー tentententen
提出日時 2024-04-23 17:45:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 922 ms / 2,000 ms
コード長 2,416 bytes
コンパイル時間 3,985 ms
コンパイル使用メモリ 92,108 KB
実行使用メモリ 69,800 KB
最終ジャッジ日時 2024-04-23 17:45:28
合計ジャッジ時間 25,615 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
37,932 KB
testcase_01 AC 69 ms
37,948 KB
testcase_02 AC 70 ms
38,304 KB
testcase_03 AC 69 ms
37,880 KB
testcase_04 AC 70 ms
38,104 KB
testcase_05 AC 69 ms
37,880 KB
testcase_06 AC 70 ms
38,016 KB
testcase_07 AC 76 ms
38,200 KB
testcase_08 AC 75 ms
38,304 KB
testcase_09 AC 74 ms
38,336 KB
testcase_10 AC 76 ms
38,176 KB
testcase_11 AC 71 ms
38,352 KB
testcase_12 AC 578 ms
53,604 KB
testcase_13 AC 551 ms
53,200 KB
testcase_14 AC 656 ms
54,744 KB
testcase_15 AC 384 ms
50,160 KB
testcase_16 AC 373 ms
48,704 KB
testcase_17 AC 376 ms
49,908 KB
testcase_18 AC 693 ms
58,812 KB
testcase_19 AC 560 ms
53,924 KB
testcase_20 AC 319 ms
49,048 KB
testcase_21 AC 574 ms
56,936 KB
testcase_22 AC 267 ms
47,640 KB
testcase_23 AC 460 ms
50,408 KB
testcase_24 AC 701 ms
55,872 KB
testcase_25 AC 384 ms
49,404 KB
testcase_26 AC 712 ms
56,840 KB
testcase_27 AC 462 ms
50,864 KB
testcase_28 AC 528 ms
53,240 KB
testcase_29 AC 481 ms
51,012 KB
testcase_30 AC 495 ms
51,236 KB
testcase_31 AC 414 ms
50,620 KB
testcase_32 AC 905 ms
69,800 KB
testcase_33 AC 818 ms
63,640 KB
testcase_34 AC 641 ms
57,512 KB
testcase_35 AC 922 ms
66,452 KB
testcase_36 AC 897 ms
63,696 KB
testcase_37 AC 847 ms
60,944 KB
testcase_38 AC 881 ms
60,292 KB
testcase_39 AC 853 ms
62,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] values = new int[n];
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            if (values[a] < values[b]) {
                graph.get(a).add(b);
            } else if (values[a] > values[b]) {
                graph.get(b).add(a);
            }
        }
        boolean[] isOn = new boolean[n];
        int k = sc.nextInt();
        while (k-- > 0) {
            isOn[sc.nextInt() - 1] = true;
        }
        PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> values[a] - values[b]);
        for (int i = 0; i < n; i++) {
            queue.add(i);
        }
        List<Integer> ans = new ArrayList<>();
        while (queue.size() > 0) {
            int idx = queue.poll();
            if (isOn[idx]) {
                ans.add(idx + 1);
                for (int x : graph.get(idx)) {
                    isOn[x] ^= true;
                }
            }
        }
        System.out.println(ans.size());
        System.out.println(ans.stream().map(x -> x.toString()).collect(Collectors.joining("\n")));
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0