-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11724.java
44 lines (41 loc) · 1 KB
/
11724.java
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
import java.util.*;
import java.io.*;
public class Main {
static int[][] connect=new int[1001][1001];
static int[] visited_bfs = new int[1001];
static int n,m;static int num=0;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); m = sc.nextInt();
for(int i =0;i<m;i++) {
int s = sc.nextInt(); int e = sc.nextInt();
connect[s][e] = 1; connect[e][s]=1;
}
while(true) {
int zero=0;int start = 0;
for(int i=n;i>=1;i--) {
if(visited_bfs[i]==0) {
zero++;start=i;
}
}
if(zero != 0) {
num++;bfs(start,num);
}else {
break;
}
}
System.out.println(num);
}
static void bfs(int i, int num) {
Queue<Integer> q = new LinkedList<>();
q.add(i);visited_bfs[i]=num;
while(!q.isEmpty()) {
int l = q.poll();
for(int k=1;k<=n;k++) {
if(connect[l][k]==1 && visited_bfs[k]==0) {
visited_bfs[k]=num;q.add(k);
}
}
}
}
}