본문 바로가기
Problem Solving/Baekjoon

[백준] 26069 붙임성 좋은 총총이 - Data Structure / Java

by graycode 2025. 4. 6.

 문제 링크

https://www.acmicpc.net/problem/26069

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {

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

        Set<String> set = new HashSet<>();
        set.add("ChongChong");
        int n = Integer.parseInt(br.readLine());
        while (n-- > 0) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            String a = st.nextToken(), b = st.nextToken();
            if (!set.contains(a) && !set.contains(b)) continue;
            set.add(a);
            set.add(b);
        }

        bw.write(String.valueOf(set.size()));
        bw.flush();
    }

}

댓글