南阳理工 acm 42 一笔画问题

一笔画问题

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述

zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来。

规定,所有的边都只能画一次,不能重复画。

 

输入
第一行只有一个正整数N(N<=10)表示测试数据的组数。
每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线。(点的编号从1到P)
随后的Q行,每行有两个正整数A,B(0<A,B<P),表示编号为A和B的两点之间有连线。
输出
如果存在符合条件的连线,则输出"Yes",
如果不存在符合条件的连线,输出"No"。
样例输入
2
4 3
1 2
1 3
1 4
4 5
1 2
2 3
1 3
1 4
3 4
样例输出
No
Yes

 方法一:并查集判断集合个数

package LanQiaoBei;
import java.util.Scanner;

public class EulerCircuit1 {

	private static int vertexCount;//顶点个数
	private static int edgeCount;//边个数
	private static int degree[];//计算顶点度数
	private static boolean tag[];//标记顶点是否出现过
	private static int rank[];
	private static int parent[];
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int caseCount = sc.nextInt();
		int v1, v2;
		while(caseCount-- > 0) {
			vertexCount = sc.nextInt();
			edgeCount = sc.nextInt();
			degree = new int[vertexCount+1];
			rank = new int[vertexCount+1];
			parent = new int[vertexCount+1];
			tag = new boolean[vertexCount+1];
			
			make_set();//初始化并查集
			
			for(int i=0; i<edgeCount; i++) {
				v1 = sc.nextInt();
				v2 = sc.nextInt();
				degree[v1] ++;
				degree[v2] ++;
				tag[v1] = true;
				tag[v2] = true;
				union(v1, v2);
			}
			
			if(oddCount()==false || setCount()==false) {//不是连通图
				System.out.println("No");
			} else {
				System.out.println("Yes");
			}
		}
	}
	
	private static boolean setCount() {
		for(int i=1; i<=vertexCount; i++) {
			if(tag[i] != true) return false;
			if(parent[i] != parent[1]) return false;
		}
		return true;
	}

	private static void union(int x1, int x2) {
		int r1 = find(x1);
		int r2 = find(x2);
		if(r1 != r2) {
			if(rank[r1] > rank[r2]) {
				parent[r2] = r1;
			}else {
				parent[r1] = r2;
				if(rank[r1] == rank[r2]) {
					rank[r2]++;
				}
			}
		}
	}

	private static int find(int x) {
		if(x != parent[x]) {
			parent[x] = find(parent[x]);
		}
		return parent[x];
	}

	private static void make_set() {
		for(int i=0; i<=vertexCount; i++) {
			parent[i] = i;
			rank[i] = 0;
		}
	}

	/*
	 * 判断奇数点个数是否是0||2个
	 */
	private static boolean oddCount() {
		int count = 0;
		for(int i=1; i<=vertexCount; i++) {
			if((degree[i]&1) == 1) count++;
		}
		if(count==0 || count == 2) return true;
		return false;
	}

}

方法二:深搜

 

package LanQiaoBei;

import java.util.Scanner;
import java.util.Stack;

public class EulerCircuit {

	private static int map[][];
	private static int vertexCount;
	private static int edgeCount;
	private static int vertexs[]; 
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int caseCount = sc.nextInt();
		int v1, v2;
		while(caseCount-- > 0) {
			vertexCount = sc.nextInt();
			edgeCount = sc.nextInt();
			map = new int[vertexCount+1][vertexCount+1];
			vertexs = new int[vertexCount+1];
			for(int i=0; i<edgeCount; i++) {
				v1 = sc.nextInt();
				v2 = sc.nextInt();
				if(map[v1][v2] == 0) {//去掉重边
					map[v1][v2] = 1;
					map[v2][v1] = 1;
					vertexs[v1] ++;
					vertexs[v2] ++;
				}
			}
			if(oddCount()==false || dfs()==false) {//不是连通图
				System.out.println("No");
			} else {
				System.out.println("Yes");
			}
		}
	}
	
	/*
	 * 判断奇数点个数是否是0||2个
	 */
	private static boolean oddCount() {
		int count = 0;
		for(int i=1; i<=vertexCount; i++) {
			if(vertexs[i]%2 == 1) count++;
		}
		if(count==0 || count == 2) return true;
		return false;
	}

	/*
	 * 深搜判断是否是连通图
	 */
	private static boolean dfs() {
		boolean[] tag = new boolean[vertexCount+1];
		Stack<Integer> stack = new Stack<Integer>();
		stack.add(1);
		tag[1] = true;
		int temp;
		while(!stack.isEmpty()) {
			temp = stack.pop();
			for(int i=1; i<=vertexCount; i++) {
				if(map[temp][i]!=0 && tag[i]==false) {
					stack.add(i);
					tag[i] = true;
				}
			}
		}
		//判断所有定点是否都被访问到
		for(int i=1; i<=vertexCount; i++){
			if(tag[i] == false) return false;
		}
		return true;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值