Timus #1080 Mapping color (簡易圖論 DFS BFS)

Map Coloring
Time limit: 1.0 second
Memory limit: 64 MB
We consider a geographical map with N countries numbered from 1 to N (0 < N < 99). For every country we know the numbers of other countries which are connected with its border. From every country we can reach to any other one, eventually crossing some borders. Write a program which determines whether it is possible to color the map only in two colors — red and blue in such a way that if two countries are connected their colors are different. The color of the first country is red. Your program must output one possible coloring for the other countries, or show, that such coloring is impossible.
Input
On the first line is written the number N. On the following N lines, the i-th line contains the countries to which the i-th country is connected. Every integer on this line is bigger than i, except the last one which is 0 and marks that no more countries are listed for country i. If a line contains 0, that means that the i-th country is not connected to any other country, which number is larger than i.
Output
The output contains exactly one line. If the coloring is possible, this line must contain a list of zeros and ones, without any separators between them. The i-th digit in this sequence is the color of the i-th country. 0 corresponds to red color, and one — to blue color. If a coloring is not possible, output the integer ?1.
Sample

input output
3 010
2 0
3 0
0

本題使用DFS或BFS可快速解答,源碼如下

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class q1080 {
    public static int[][] map;
    public static int[] color;
    public static boolean flag = false;
    public static void DFS(int index,int size){
        if(flag) return;
        for(int i = 0 ;i < size;i++){
            if(map[index][i] == 1){
                if(color[i] == -1){
                    color[i] = color[index]==0?1:0;
                    DFS(i,size);
                }else if(color[i] == color[index]){
                    flag = true;
                    return;
                }
            }
        }
    }
    public static void BFS(int index,int size){
        if(flag) return;
        Queue<Integer> que = new LinkedList<>();
        que.offer(index);
        while(!que.isEmpty()){
            int cur = que.poll();
            int c = color[cur]==0?1:0;
            for(int i = 0;i < size;i++){
                if(map[cur][i] == 1){
                    if(color[i] == -1){
                        color[i] = c;
                        que.offer(i);
                    }else if(color[i] == color[cur]){
                        flag = true;
                        return;
                    }
                }
            }
        }
    }
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        map = new int[n][n];
        color = new int[n];
        for(int i = 0;i < n;i++){
            color[i]  = -1;
        }
        for(int i = 0;i<n;i++){
            while(true){
                int index = s.nextInt()-1;
                if(index == -1) break;
                map[i][index] = 1;
                map[index][i] = 1;
            }
        }
        for(int i = 0;i < n;i++){
            if(color[i]==-1){
                color[i] = 0;
                BFS(i,n);
              //DFS(i,n);
            }
        }
        if(flag){
            System.out.println(-1);
        }else{
            for(int i = 0;i<n;i++){
                System.out.print(color[i]);
            }
        }

    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 夜鶯2517閱讀 128,158評論 1 9
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,838評論 28 54
  • 信任包括信任自己和信任他人 很多時候,很多事情,失敗、遺憾、錯過,源于不自信,不信任他人 覺得自己做不成,別人做不...
    吳氵晃閱讀 6,367評論 4 8

友情鏈接更多精彩內(nèi)容