[백준/BOJ] 2606. 바이러스 [Silver 3]

jychan99·2023년 10월 26일
0

  1. 바이러스
    문제출처 : https://www.acmicpc.net/problem/2606

BFS

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static Queue<Integer> q = new LinkedList<>();
    static int[][] network;
    static int[] c;
    static int com, conn, cnt=0,x,y;

    public static void BFS(){
        q.add(1);
        c[1]=1;
        while(!q.isEmpty()){
            int temp = q.peek();
            q.poll();
            for(int i=1;i<=com;i++){
                if(network[temp][i] ==1){
                    if(c[i]==1){
                        continue;
                    }else{
                        c[i]=1;
                        q.add(i);
                        cnt++;
                    }
                }
            }
        }
    }    

    public static void main(String args[])throws IOException{
        com = Integer.parseInt(br.readLine());
        conn = Integer.parseInt(br.readLine());

        network = new int[com+1][com+1];
        c = new int[com+1];
        for(int i=1;i<=com;i++){
            c[i] = 0;
        }
        for(int i=1;i<=conn;i++){
            st = new StringTokenizer(br.readLine()," ");
            x = Integer.parseInt(st.nextToken());
            y = Integer.parseInt(st.nextToken());
            network[x][y]=1;
            network[y][x]=1;
        }

        BFS();

        System.out.println(cnt);
    }
}

DFS

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static Queue<Integer> q = new LinkedList<>();
    static int[][] network;
    static int[] c;
    static int com, conn, cnt=0,x,y;

    public static void DFS(int s){
        if(c[s]==1){
            return;
        }
        if(c[1]==1){
            cnt++;
        }
        c[s]=1;
        for(int i=1;i<=com;i++){
            if(network[s][i]==1){
                DFS(i);
            }
        }
    }    

    public static void main(String args[])throws IOException{
        com = Integer.parseInt(br.readLine());
        conn = Integer.parseInt(br.readLine());

        network = new int[com+1][com+1];
        c = new int[com+1];
        for(int i=1;i<=com;i++){
            c[i] = 0;
        }
        for(int i=1;i<=conn;i++){
            st = new StringTokenizer(br.readLine()," ");
            x = Integer.parseInt(st.nextToken());
            y = Integer.parseInt(st.nextToken());
            network[x][y]=1;
            network[y][x]=1;
        }

        DFS(1);

        System.out.println(cnt);
    }
}

탐색알고리즘 BFS,DFS연습으로 두번풀어봤다.

profile
내가 지금 두려워 하고 있는 일이 바로 내가 지금 해야 할 일이다. 🐥

0개의 댓글