結果

問題 No.1477 Lamps on Graph
ユーザー tenten
提出日時 2021-04-28 09:19:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 634 ms / 2,000 ms
コード長 2,415 bytes
コンパイル時間 2,065 ms
コンパイル使用メモリ 80,164 KB
実行使用メモリ 62,792 KB
最終ジャッジ日時 2024-07-07 04:15:06
合計ジャッジ時間 19,072 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

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];
        Lamp[] lamps = new Lamp[n];
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            lamps[i] = new Lamp(i, values[i]);
            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);
            }
            if (values[b] < values[a]) {
                graph.get(b).add(a);
            }
        }
        int k = sc.nextInt();
        boolean[] ons = new boolean[n];
        for (int i = 0; i < k; i++) {
            ons[sc.nextInt() - 1] = true;
        }
        Arrays.sort(lamps);
        ArrayList<Integer> ans = new ArrayList<>();
        for (Lamp x : lamps) {
            if (ons[x.idx]) {
                ans.add(x.idx + 1);
                for (int y : graph.get(x.idx)) {
                    ons[y] ^= true;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(ans.size()).append("\n");
        for (int x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Lamp implements Comparable<Lamp> {
        int idx;
        int value;
        
        public Lamp(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Lamp another) {
            return value - another.value;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0