Advertisement
vov44k

t164

Feb 1st, 2023
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class t164 {
  4.     static int[][] G ;
  5.     static int n;
  6.     static boolean [] used ;
  7.     static void dfs (int v) {
  8.         used[v]=true;
  9.         for (int i=0; i<G.length; i++) {
  10.             if(G[v][i]==1 && !used[i]) {
  11.                 dfs(i);
  12.             }}}
  13.     public static void main(String[] args) {
  14.        
  15.         Scanner in = new Scanner  (System.in);
  16.         n = in.nextInt();
  17.         used= new boolean[n];
  18.         G=new int[n][n];
  19.         int s = in.nextInt()-1;
  20.         for (int i=0; i<n; i++) {
  21.             for (int j=0; j<n; j++) {
  22.                 G[i][j]=in.nextInt();
  23.             }}
  24.         dfs(s);
  25.         int count = 0;
  26.         for (int i=0; i<used.length; i++) {
  27.             if (used[i]) {
  28.                 count++;
  29.             }
  30.         }
  31.         System.out.println(count);
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement