zoukankan      html  css  js  c++  java
  • URAL 1080 Map Coloring(染色)

    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

    inputoutput
    3
    2 0
    3 0
    0
    
    010
    
    Problem Author: Emil Kelevedzhiev
    【分析】一个很简单的图染色问题。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 170;
    const int M = 24005;
    int  n,m,k,l,tot=0;
    int parent[N],pre[N],vis[N],color[N];
    vector<int>vec[N];
    bool ok=true;
    void dfs(int u){
        vis[u]=1;
        for(int i=0;i<vec[u].size();i++){
            int v=vec[u][i];
            if(!vis[v]){
                color[v]=(color[u]+1)%2;
                dfs(v);
            }
            else if(vis[v]&&color[v]==color[u]){
                ok=false;return;
            }
        }
    }
    int main() {
        int u,v;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            while(1){
                scanf("%d",&v);
                if(!v)break;
                vec[i].pb(v);vec[v].pb(i);
            }
        }
        for(int i=1;i<=n;i++){
            if(!vis[i]){
                color[i]=0;
                dfs(i);
            }
        }
        if(ok)for(int i=1;i<=n;i++)printf("%d",color[i]);
        else printf("-1");
        printf("
    ");
    
        return 0;
    }
  • 相关阅读:
    线段树区间异或--差分时间复杂度优化
    数独
    sql语句-根据动态参数去拼sql
    Python-读取文件的大小
    Docker-教你如何通过 Docker 快速搭建各种测试环境
    Docker-本地镜像发布到阿里云
    Docker- Mysql数据库主从同步配置方法
    mysql-如何删除主从同步
    Docker数据卷的介绍和使用
    Docker镜像-拉取并且运行
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6000052.html
Copyright © 2011-2022 走看看