Map Coloring - URAL 1080 涂色问题

题目链接 URAL 1080

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

inputoutput
3
2 0
3 0
0
010
Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament

题目意思:给定一个图,要给这个图图上两种不同的颜色使得相邻的两个节点颜色不同,输出一种涂色方法。

解题思路:注意图有可能不是全部连接在一起,所以要搜寻所有节点,对于每一部分连通图,bfs从起始点开始给所有节点涂色。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

#define maxn 100+10
vector<int> vec[maxn];
int color[maxn];
int n;

bool bfs(int k){
    queue<int> que;
    que.push(k);
    color[k] = 0;
    int t;
    while(!que.empty()){
        t = que.front();
        que.pop();
      //  cout <<"###" <<t <<" " << vec[t].size() <<endl;
        for(int i=0;i<vec[t].size();i++){
            int temp = vec[t][i];
            if(color[temp] == color[t]) return false;
            else if(color[temp] == -1){
                if(color[t]==1) color[temp] = 0;
                else color[temp] = 1;
                que.push(temp);
            }
        }
    }
    return true;
}

bool solve(){
    for(int i=1;i<=n ;i++){
        if(color[i]==-1){
            if(!bfs(i)) return false;
        }
    }
    return true;
}
int main(){
    int a;
    cin >> n;
    memset(color,-1,sizeof(color));
    for(int t=1;t<=n;t++){
        while(scanf("%d",&a) && a){
            vec[t].push_back(a);
            vec[a].push_back(t);
        }
    }
    if(solve()){
        for(int i=1;i<=n;i++){
            cout << color[i];
        }
        cout << endl;
    }else{
        printf("-1\n");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值