-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcluersGraph.java
More file actions
103 lines (74 loc) · 1.99 KB
/
EcluersGraph.java
File metadata and controls
103 lines (74 loc) · 1.99 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package Ecluers;
import java.util.ArrayList;
import java.util.Scanner;
public class EcluersGraph {
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
public EcluersGraph(int n){
for(int i = 0; i <= n; i++){
adj.add(new ArrayList<>());
}
}
public void addEdge(int src , int dest){
adj.get(src).add(dest);
adj.get(dest).add(src);
}
public void dfs(int node , ArrayList<ArrayList<Integer>> adj , boolean vis[]){
vis[node] = true;
for(int it : adj.get(node)){
if(!vis[it]){
dfs(it, adj, vis);
}
}
}
public boolean isConnected(int n){
boolean vis[] = new boolean[n + 1];
int start = -1;
for(int i = 0; i < n; i++){
if(!adj.get(i).isEmpty()){
start = i;
break;
}
}
if(start == -1){
return true;
}
dfs(start, adj, vis);
for(int i = 0; i <= n; i++){
if(!vis[i] && !adj.get(i).isEmpty()){
return false;
}
}
return true;
}
public String isEculer(int n){
if(!isConnected(n)){
return "Not";
}
int odd = 0;
for(int i = 1; i <= n; i++){
if(adj.get(i).size() % 2 != 0){
odd++;
}
}
if(odd == 0){
return "Yes";
}else if(odd == 2){
return "Semi - Yes";
}else{
return "Not";
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
EcluersGraph eg = new EcluersGraph(n);
for(int i = 0; i < m; i++){
int src = sc.nextInt();
int dest = sc.nextInt();
eg.addEdge(src , dest);
}
System.out.println(eg.isEculer(n));
sc.close();
}
}