File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed
Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static StringTokenizer st;
9+ static int K ;
10+ static int V , E ;
11+ static List<Integer > [] adjLists;
12+
13+ public static void main (String [] args ) throws IOException {
14+ K = Integer . parseInt(br. readLine());
15+
16+ for (int i = 0 ; i < K ; i++ ) {
17+ st = new StringTokenizer (br. readLine());
18+ V = Integer . parseInt(st. nextToken());
19+ E = Integer . parseInt(st. nextToken());
20+ adjLists = new List [V + 1 ];
21+ for (int j = 1 ; j <= V ; j++ ) {
22+ adjLists[j] = new ArrayList<> ();
23+ }
24+ for (int j = 0 ; j < E ; j++ ) {
25+ st = new StringTokenizer (br. readLine());
26+ int u = Integer . parseInt(st. nextToken());
27+ int v = Integer . parseInt(st. nextToken());
28+ adjLists[u]. add(v);
29+ adjLists[v]. add(u);
30+ }
31+ if (! BFS ()) {
32+ bw. write(" NO\n " );
33+ } else {
34+ bw. write(" YES\n " );
35+ }
36+ }
37+ bw. close();
38+ }
39+
40+ public static boolean BFS () {
41+ ArrayDeque<Integer > q = new ArrayDeque<> ();
42+ int [] visited = new int [V + 1 ];
43+
44+ for (int i = 1 ; i <= V ; i++ ) {
45+ if (visited[i] == 0 ) {
46+ q. offer(i);
47+ visited[i] = 1 ;
48+
49+ while (! q. isEmpty()) {
50+ int cur = q. poll();
51+
52+ for (int next : adjLists[cur]) {
53+ if (visited[next] == 0 ) {
54+ visited[next] = - visited[cur];
55+ q. offer(next);
56+ } else if (visited[next] == visited[cur]) {
57+
58+ return false ;
59+ }
60+ }
61+ }
62+ }
63+ }
64+ return true ;
65+ }
66+ }
67+
68+ ```
You can’t perform that action at this time.
0 commit comments