結果

問題 No.1477 Lamps on Graph
ユーザー tentententen
提出日時 2023-08-04 12:51:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 889 ms / 2,000 ms
コード長 3,000 bytes
コンパイル時間 2,914 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 66,920 KB
最終ジャッジ日時 2024-04-22 10:29:22
合計ジャッジ時間 23,289 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
37,904 KB
testcase_01 AC 68 ms
37,988 KB
testcase_02 AC 68 ms
37,516 KB
testcase_03 AC 69 ms
38,044 KB
testcase_04 AC 68 ms
37,832 KB
testcase_05 AC 71 ms
37,616 KB
testcase_06 AC 69 ms
37,944 KB
testcase_07 AC 73 ms
37,784 KB
testcase_08 AC 68 ms
37,928 KB
testcase_09 AC 67 ms
37,768 KB
testcase_10 AC 67 ms
38,004 KB
testcase_11 AC 67 ms
37,664 KB
testcase_12 AC 553 ms
54,668 KB
testcase_13 AC 556 ms
54,496 KB
testcase_14 AC 622 ms
55,600 KB
testcase_15 AC 417 ms
50,248 KB
testcase_16 AC 375 ms
52,180 KB
testcase_17 AC 343 ms
49,364 KB
testcase_18 AC 653 ms
57,352 KB
testcase_19 AC 541 ms
55,600 KB
testcase_20 AC 342 ms
49,848 KB
testcase_21 AC 591 ms
57,256 KB
testcase_22 AC 252 ms
47,040 KB
testcase_23 AC 448 ms
50,912 KB
testcase_24 AC 681 ms
57,776 KB
testcase_25 AC 386 ms
50,280 KB
testcase_26 AC 674 ms
60,904 KB
testcase_27 AC 439 ms
52,216 KB
testcase_28 AC 506 ms
54,188 KB
testcase_29 AC 476 ms
52,168 KB
testcase_30 AC 455 ms
52,176 KB
testcase_31 AC 388 ms
51,368 KB
testcase_32 AC 889 ms
66,920 KB
testcase_33 AC 782 ms
63,064 KB
testcase_34 AC 635 ms
62,800 KB
testcase_35 AC 874 ms
65,452 KB
testcase_36 AC 843 ms
62,540 KB
testcase_37 AC 750 ms
61,712 KB
testcase_38 AC 799 ms
62,960 KB
testcase_39 AC 847 ms
62,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
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];
        Idx[] idxes = new Idx[n];
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            graph.add(new ArrayList<>());
            idxes[i] = new Idx(i, values[i]);
        }
        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;
        }
        Arrays.sort(idxes);
        ArrayList<Integer> ans = new ArrayList<>();
        for (Idx x : idxes) {
            if (isOn[x.idx]) {
                ans.add(x.idx + 1);
                for (int y : graph.get(x.idx)) {
                    isOn[y] ^= true;
                }
            }
        }
        System.out.println(ans.size());
        System.out.println(ans.stream().map(x -> x.toString()).collect(Collectors.joining("\n")));
    }
    
    static class Idx implements Comparable<Idx> {
        int idx;
        int value;
        
        public Idx(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Idx another) {
            return value - another.value;
        }
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0