結果

問題 No.1477 Lamps on Graph
ユーザー tenten
提出日時 2022-08-05 11:48:02
言語 Java
(openjdk 23)
結果
AC  
実行時間 790 ms / 2,000 ms
コード長 2,393 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 79,776 KB
実行使用メモリ 62,480 KB
最終ジャッジ日時 2024-09-15 07:34:48
合計ジャッジ時間 21,904 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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();
        Path[] lights = new Path[n];
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            lights[i] = new Path(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 (lights[a].value > lights[b].value) {
                graph.get(b).add(a);
            } else if (lights[a].value < lights[b].value) {
                graph.get(a).add(b);
            }
        }
        Arrays.sort(lights);
        boolean[] isOn = new boolean[n];
        int k = sc.nextInt();
        while (k-- > 0) {
            isOn[sc.nextInt() - 1] = true;
        }
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for (Path x : lights) {
            if (!isOn[x.idx]) {
                continue;
            }
            sb.append(x.idx + 1).append("\n");
            count++;
            for (int y : graph.get(x.idx)) {
                isOn[y] ^= true;
            }
        }
        System.out.println(count);
        System.out.print(sb);
    }

    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path 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 double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0