[Java]バックアップ-ウイルス(2606)DFS


ウイルス
import java.io.*;
import java.util.*;

public class Main {
    static int[][] arr;
    static boolean[] visit;
    static int cnt = 0;
    
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        
        int cNum = Integer.parseInt(br.readLine());
        int M = Integer.parseInt(br.readLine());
        
        arr = new int[cNum+1][cNum+1];
        visit = new boolean[cNum+1];
        
        for(int i=1; i<M+1; i++){
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            arr[a][b] = 1;
            arr[b][a] = 1;
        }
        
        dfs(1);
        System.out.println(cnt);
    }
    
    public static void dfs(int start){
        visit[start] = true;
        
        for(int i=1; i<visit.length; i++){
            if(i != start && visit[i] == false && arr[start][i] == 1){
            	cnt++;
                dfs(i);
            }
        }
    }
}