import java.util.*;
public class Main {
static Main ybh = new Main();
private class StackX {
private final int SIZE=1000;
private int[] st;
private int top;
public StackX(){
st = new int[SIZE];
top = -1;
}
public void push(int j){
st[++top] = j;
}
public int pop(){
return st[top--];
}
public int peek(){
return st[top];
}
public boolean isEmpty(){
return top==-1;
}
}
private class Vertex {
public int value;
public boolean wasVisited;
public Vertex(int a){
this.value = a;
this.wasVisited = false;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
Vertex vertexList[] = new Vertex[num];
int[][]adjArr = new int[num][num];
int m = 1;
int nVerts = 0;
while(num>0){
vertexList[nVerts++] = ybh.new Vertex(m);
m++;
int a = scan.nextInt();
int b = scan.nextInt();
adjArr[a-1][b-1] = 1;
adjArr[b-1][a-1] = 1;
num--;
}
//判断是否是连通图
boolean flag = dfs(vertexList,adjArr,nVerts);
if(flag){
//判断每行1的个数,放进数组,循环数组,若素组元素均是偶数返回true,如果素组中有两个元素是奇数页返回true,否则返回false;
int itemCount = 0;
List<Integer> ll = new ArrayList<Integer>();
for (int i = 0; i < adjArr.length; i++) {
for (int j = 0; j < adjArr.length; j++) {
if(adjArr[i][j] == 1)
itemCount++;
}
ll.add(itemCount);
itemCount = 0;
}
int []arr = new int[adjArr.length];
Iterator<Integer> it = ll.iterator();
int index = 0;
while (it.hasNext()) {
arr[index++] = it.next();
}
int odd = 0;
int even = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]%2==0)
even++;
else
odd++;
}
if(even==arr.length || odd==2)
System.out.println(true);
else
System.out.println(false);
}
else
System.out.println(false);
}
private static boolean dfs(Vertex[] vertexList, int[][] adjArr,int n) {
StackX theStack = ybh.new StackX();
vertexList[0].wasVisited = true;
theStack.push(0);
int count = 1;
while (!theStack.isEmpty()) {
int v = getAdjUnvisiedVertex(vertexList,theStack.peek(),adjArr,n);
if(v==-1)
theStack.pop();
else{
vertexList[v].wasVisited = true;
theStack.push(v);
count++;
}
}
for (int i = 0; i < n; i++) {
vertexList[i].wasVisited = false;
}
if(count == n)
return true;
else
return false;
}
private static int getAdjUnvisiedVertex(Vertex[] vertexList,int v,int[][] adjArr,int nVerts) {
for (int j = 0; j < nVerts; j++) {
if(adjArr[v][j]==1 && vertexList[j].wasVisited == false)
return j;
}
return -1;
}
}
笔画
最新推荐文章于 2021-09-22 09:12:46 发布