개발자 톡

연습문제 톡 우물 안 개구리

자바 코드

등록일
2025-02-06 22:43:44
조회수
46
작성자
zanghwkddnwl
import java.io.*;
import java.util.*;

public class Main {

    public static class Neighbor {
        Set<Integer> neighbor = new HashSet<>();
        int maxWeight = 0;
        public void add(int person, int weight){
            this.neighbor.add(person);
            this.maxWeight = Math.max(this.maxWeight, weight);
        }

        @Override
        public String toString(){
            return neighbor.toString() + " : MAX("+maxWeight+")";
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        int[] weight = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        Map<Integer, Neighbor> r = new HashMap<>();

        for(int i = 0; i < m; i++){
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());

            if(!r.containsKey(a)){
                Neighbor aNeighbor = new Neighbor();
                aNeighbor.add(b, weight[b-1]);
                r.put(a, aNeighbor);
            }else{
                r.get(a).add(b, weight[b-1]);
            }

            if(!r.containsKey(b)){
                Neighbor bNeighbor = new Neighbor();
                bNeighbor.add(a, weight[a-1]);
                r.put(b, bNeighbor);
            }else{
                r.get(b).add(a, weight[a-1]);
            }
            
            
        }
        int iamGood = 0;
        // bw.write(r+"\n");
        for(int i = 0; i < n; i++){
            int my = i+1;
            if(r.containsKey(my)){
                Neighbor nn = r.get(my);
                if(weight[my-1] > nn.maxWeight){
                    iamGood++;
                }
            }else{
                iamGood++;
            }
        }

        bw.write(iamGood+"\n");
        
        bw.flush();
        bw.close();
    }
}


#우물_안_개구리

이 카테고리의 톡 더보기