笔画

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;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值