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]);
            }
        }

    }
}

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末浪南,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件领曼,死亡現場離奇詭異,居然都是意外死亡领铐,警方通過查閱死者的電腦和手機悯森,發(fā)現死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绪撵,“玉大人,你說我怎么就攤上這事祝蝠∫粽” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵绎狭,是天一觀的道長细溅。 經常有香客問我,道長儡嘶,這世上最難降的妖魔是什么喇聊? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮蹦狂,結果婚禮上誓篱,老公的妹妹穿的比我還像新娘。我一直安慰自己凯楔,他們只是感情好窜骄,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著摆屯,像睡著了一般邻遏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上虐骑,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天准验,我揣著相機與錄音,去河邊找鬼廷没。 笑死糊饱,一個胖子當著我的面吹牛,可吹牛的內容都是我干的腕柜。 我是一名探鬼主播济似,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼矫废,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了砰蠢?” 一聲冷哼從身側響起蓖扑,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎台舱,沒想到半個月后律杠,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡竞惋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年柜去,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拆宛。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡嗓奢,死狀恐怖,靈堂內的尸體忽然破棺而出浑厚,到底是詐尸還是另有隱情股耽,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布钳幅,位于F島的核電站物蝙,受9級特大地震影響,放射性物質發(fā)生泄漏敢艰。R本人自食惡果不足惜诬乞,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望钠导。 院中可真熱鬧震嫉,春花似錦、人聲如沸辈双。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽湃望。三九已至换衬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間证芭,已是汗流浹背瞳浦。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留废士,地道東北人叫潦。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像官硝,于是被迫代替她去往敵國和親矗蕊。 傳聞我的和親對象是個殘疾皇子短蜕,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內容

  • 夜鶯2517閱讀 127,719評論 1 9
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭傻咖,有人歡樂有人憂愁朋魔,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,536評論 28 53
  • 信任包括信任自己和信任他人 很多時候卿操,很多事情警检,失敗、遺憾害淤、錯過扇雕,源于不自信,不信任他人 覺得自己做不成窥摄,別人做不...
    吳氵晃閱讀 6,187評論 4 8