zoukankan      html  css  js  c++  java
  • ural 1080 Map Coloring DFS染色

    1080. Map Coloring

    Time Limit: 1.0 second
    Memory Limit: 16 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
    

    题目:
    只用0,1对图进行染色,并且相邻的不能是相同颜色

    分析:
    直接DFS染色,对于相邻的染成相反的颜色即可

    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int X = 105;
     8 
     9 int po[X],tol,n;
    10 int use[X];
    11 
    12 struct node{
    13     int y,next;
    14 }edge[X*X];
    15 
    16 void add(int x,int y){
    17     edge[++tol].y = y;
    18     edge[tol].next = po[x];
    19     po[x] = tol;
    20 }
    21 
    22 bool dfs(int x){
    23     int y;
    24     bool ok = true;
    25     for(int i=po[x];i;i=edge[i].next){
    26         y = edge[i].y;
    27         if(use[x]==use[y])
    28             return false;
    29         if(use[y]!=-1)
    30             continue;
    31         use[y] = !use[x];
    32         ok = ok&&dfs(y);
    33         if(!ok)
    34             return false;
    35     }
    36     return true;
    37 }
    38 
    39 int main(){
    40     //freopen("sum.in","r",stdin);
    41     int x;
    42     while(cin>>n){
    43         memset(po,0,sizeof(po));
    44         tol = 0;
    45         for(int i=1;i<=n;i++){
    46             while(scanf("%d",&x),x){
    47                 add(i,x);
    48                 add(x,i);
    49             }
    50         }
    51         bool ok = true;
    52         memset(use,-1,sizeof(use));
    53         for(int i=1;i<=n;i++)
    54             if(use[i]==-1){
    55                 use[i] = 0;
    56                 if(!dfs(i)){
    57                     ok = false;
    58                     break;
    59                 }
    60             }
    61         if(ok){
    62             for(int i=1;i<=n;i++)
    63                 printf("%d",use[i]);
    64             puts("");
    65         }
    66         else
    67             puts("-1");
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    Redis(五)——主从做读写分离原理与优化
    Redis(四)——持久化方案(RDB和AOF使用)
    Redis(三)——高级用法(GEO地理位置信息)
    Redis(二)——五大数据类型的基本操作(字符串类型,哈希类型,列表类型,集合类型,有序集合类型)
    Redis(一)——redis初识,redis安装和启动
    15 Ajax技术
    ORM多表分组、F与Q查询
    ORM多表操作
    ORM单表操作
    CentOS 7 下 rdesktop 的安装
  • 原文地址:https://www.cnblogs.com/yejinru/p/2679472.html
Copyright © 2011-2022 走看看