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);
}
}
}
}
}
}
hihocoder #1121 : 二分图一•二分图判定 并不能AC 孤立点问题?
最新推荐文章于 2021-11-24 16:01:30 发布