結果

問題 No.1477 Lamps on Graph
ユーザー tenten
提出日時 2024-07-29 11:58:13
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,388 bytes
コンパイル時間 3,697 ms
コンパイル使用メモリ 92,160 KB
実行使用メモリ 81,568 KB
最終ジャッジ日時 2024-07-29 11:58:40
合計ジャッジ時間 26,280 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23 WA * 15
権限があれば一括ダウンロードができます

ソースコード

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 = 1; i <= m; i++) {
            int left = sc.nextInt() - 1;
            int right = sc.nextInt() - 1;
            if (values[left] < values[right]) {
                graph.get(left).add(right);
            } else if (values[left] > values[right]) {
                graph.get(right).add(left);
            }
        }
        int k = sc.nextInt();
        TreeSet<Integer> queue = new TreeSet<>((a, b) -> values[a] - values[b]);
        while (k-- > 0) {
            queue.add(sc.nextInt() - 1);
        }
        List<Integer> ans = new ArrayList<>();
        while (queue.size() > 0) {
            int x = queue.pollFirst();
            ans.add(x + 1);
            for (int y : graph.get(x)) {
                if (queue.contains(y)) {
                    queue.remove(y);
                } else {
                    queue.add(y);
                }
            }
        }
        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