hihocoder #1121 : 二分图一•二分图判定 并不能AC 孤立点问题?

import java.util.Scanner;

public class Main {

	static boolean isWrongAnswer;
	static int[] edgeList;
	static int edgeNumber;
	static int[] pointValueList;
	static int pointValueToAdd;

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int T = scanner.nextInt();
		String[] resultStr = new String[T];
		for (int t = 0; t < T; t++) {
			isWrongAnswer = false;
			pointValueToAdd = 1;
			// 单次
			int pointNumber = scanner.nextInt();
			// 保存点位值的数组
			pointValueList = new int[pointNumber];
			// 初始所有点均为 0
			for (int i = 0; i < pointNumber; i++) {
				pointValueList[i] = 0;
			}
			// 边数
			edgeNumber = scanner.nextInt();
			// 保存边

			edgeList = new int[edgeNumber * 2];

			for (int i = 0; i < edgeNumber; i++) {
				edgeList[i * 2] = scanner.nextInt() - 1;
				edgeList[i * 2 + 1] = scanner.nextInt() - 1;
			}

			// testPoint(0);
			// 非连通图时,还有为0的元素  或者有孤立点
			for (int i = 0; i < pointNumber; i++) {
				boolean hasEdge = false;
				for(int index = 0; index<edgeNumber * 2;index++){
					if(i == edgeList[index]){
						hasEdge =  true;
					}
				}
				
				
				if (pointValueList[i] == 0 && hasEdge) {
					testPoint(i);
				}
			}

			for (int i = 0; i < pointNumber; i++) {
				if (pointValueList[i] == 0) {
					isWrongAnswer = true;
				}
			}
			if (isWrongAnswer == true) {
				resultStr[t] = "Wrong";
			} else {
				resultStr[t] = "Correct";
			}
		}

		for (int t = 0; t < T; t++) {
			System.out.println(resultStr[t]);
		}

	}

	// 检测i号点 (从0开始编号)
	// 从edgeList里选取出与i相邻的点
	// 如果该点值为0或符合规则,抹除该路径
	// 如果该点值不符合规则 停止
	// 判断该点值
	// 检测该点
	static void testPoint(int i) {
		// 该点未赋值
		if (pointValueList[i] == 0 && isWrongAnswer == false) {
			if (pointValueToAdd == 1) {
				pointValueToAdd = 2;
				pointValueList[i] = 1;
			} else if (pointValueToAdd == 2) {
				pointValueToAdd = 1;
				pointValueList[i] = 2;
			}
			// System.out.println((i + 1) + "值:" + pointValueList[i]);
			// 找相邻点
			for (int k = 0; k < edgeNumber; k++) {
				if (edgeList[k * 2] == i && isWrongAnswer == false) {
					int tempI = edgeList[(k * 2) + 1];
					// System.out.println(k + "相邻点:" + tempI);
					edgeList[k * 2] = -1;
					edgeList[(k * 2) + 1] = -1;
					testPoint(tempI);
				}
				if (edgeList[(k * 2) + 1] == i && isWrongAnswer == false) {
					int tempI = edgeList[(k * 2)];
					edgeList[k * 2] = -1;
					edgeList[(k * 2) + 1] = -1;
					testPoint(tempI);
				}
			}
		} else if (pointValueList[i] != 0 && isWrongAnswer == false) {
			if (pointValueList[i] != pointValueToAdd) {
				// System.out.println("位置:" + (i + 1) + "值" +
				// pointValueList[i]);
				isWrongAnswer = true;
				return;
			} else {
				// 找相邻点
				for (int k = 0; k < edgeNumber; k++) {
					if (edgeList[k * 2] == i && isWrongAnswer == false) {
						int tempI = edgeList[(k * 2) + 1];
						edgeList[k * 2] = -1;
						edgeList[(k * 2) + 1] = -1;
						testPoint(tempI);
					}
					if (edgeList[(k * 2) + 1] == i && isWrongAnswer == false) {
						int tempI = edgeList[(k * 2)];
						edgeList[k * 2] = -1;
						edgeList[(k * 2) + 1] = -1;
						testPoint(tempI);
					}
				}
			}
		}

	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值