-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkConnectivity.java
More file actions
64 lines (57 loc) · 1.48 KB
/
Copy pathNetworkConnectivity.java
File metadata and controls
64 lines (57 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
import java.lang.*;
import java.io.*;
class Network{
public int dfs(int s, boolean nei[][], boolean vis[], int n){
vis[s]=true;
int ret=0;
for(int i=1; i<=n;i++){
if(nei[s][i]){
if(!vis[i]){
ret = 1;
ret+=dfs(i,nei,vis,n);
}
}
}
return ret;
}
public int bfs(int s, boolean nei[][], boolean vis[], int n){
Queue<Integer> queue = new LinkedList<Integer>();
vis[s]=true;
queue.add(s);
int ret = 0;
while(!queue.isEmpty()){
int t = queue.poll();
for(int i=1; i<=n;i++){
if(nei[t][i]){
if(!vis[i]){
queue.add(i);
vis[i]=true;
ret++;
}
} }
}
return ret;
}
public static void main (String args[])throws java.lang.Exception{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int s = scanner.nextInt();
boolean nei[][] = new boolean[n+1][n+1];
boolean vis[]=new boolean[n];
/* for (int i=0; i<n; i++){
for(int j=0; j<n; j++){
nei[i][j]=false;
}
}*/
for (int i=0; i<m; i++){
int from = scanner.nextInt();
int to = scanner.nextInt();
nei[from][to]=true;
nei[to][from]=true;
//System.out.println("Output is"+from+to);
}
System.out.println("Output is"+new Network().bfs(s,nei,vis,n));
}
}