黑白染色棋子



package xj;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class RanColor1 {
 static int D, B,T;
 static int map[][];
 static int color[];
   
 static class Node {
  int x;
 }

 static Node[] queue;
 static boolean flag;

 public static void main(String[] args) throws FileNotFoundException {
  /* Scanner sc=new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/RanColor"));
  T=0;
  while (sc.hasNext()) {
   D = sc.nextInt();
   B = sc.nextInt();
   map = new int[D + 1][D + 1];
   color = new int[D+1];// 0,1W,2B
   queue=new Node[D];
   /*if (D == 0 && B == 0) {
    break;
   }*/
   for (int i = 0; i < B; i++) {
    int a = sc.nextInt();
    int b = sc.nextInt();
    map[a][b] = 1;
    map[b][a] = 1;
   }
   flag = true;
   T++;
   bfs(1);
   if (!flag) {
    System.out.println("#"+T+" "+"-1");
   } else {
    int w=0;
    for (int i = 1; i < D+1; i++) {
     if(color[i]==1){w++;}
    }
    System.out.print("#"+T+" "+w);
    for (int i = 1; i < D+1; i++) {
     if(color[i]==1){System.out.print(" "+i);}
    }
    System.out.println();
   }// 记得正确输出
  }
 }

 private static void bfs(int step) {
  int head = 0;
  int tail = 1;
  Node n1 = new Node();
  n1.x = step;
  queue[head] = n1;
  color[step] = 1;
  while(head<tail){
   for (int i = 1; i < D + 1; i++) {
    int a = queue[head].x;
    if (map[a][i] == 1 && map[i][a] == 1) {
     if (color[a] == color[i]) {flag=false;return;}
     if(color[i]==0){
      Node n2=new Node();
      n2.x=i;
      queue[tail]=n2;
      tail++;
      if(color[a]==1){color[i]=2;}
      if(color[a]==2){color[i]=1;}
     }
    }
   }
   head++;
  }
  
 }

}

//

2 1 2 1

4 3 1 2 2 3 3 4

5 5
1 2
2 3
3 4
4 5
5 1

6 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6

4 3
1 2
2 3
4 3

4 4
1 2
1 3
1 4
2 3

11 10 1 2 1 3 3 4 4 5 5 10 10 11 9 8 9 4 8 6 6 4

//

#1 1 1
#2 2 1 3
#3 -1
#4 3 1 2 3
#5 2 1 3
#6 -1
#7 4 1 4 8 10

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值