zoukankan      html  css  js  c++  java
  • 1080. Map Coloring

    1080. 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 Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
    ************************************************************************************************
    利用队列的特性广搜,如果当前有前缀且未搜到,计入队列,如果有相同的,则不满足条件。
    ************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cctype>
     6 #include<cstdio>
     7 #include<stack>
     8 #include<queue>
     9 #include<vector>
    10 #include<algorithm>
    11 using namespace std;
    12 int pay[105];
    13 queue<int>Q;
    14 int map[105][105];
    15 int i,j,k,n,s;
    16 void init()
    17 {
    18     cin>>n;
    19     memset(map,0,sizeof(map));
    20     memset(pay,-1,sizeof(pay));
    21     for(i=1;i<=n;i++)
    22      {
    23          while(cin>>s&&s)
    24           {
    25               map[i][s]=1;
    26               map[s][i]=1;
    27           }
    28      }
    29      while(!Q.empty())Q.pop();
    30 
    31 }
    32 int bfs(int x)//广搜
    33 {
    34     pay[x]=0;
    35     Q.push(x);
    36     while(!Q.empty())
    37      {
    38          int v=Q.front();
    39          Q.pop();
    40          for(i=1;i<=n;i++)
    41           if(map[v][i])
    42            {
    43                if(pay[i]==-1)
    44                 {
    45                     pay[i]=1-pay[v];
    46                     Q.push(i);
    47                 }
    48                 else
    49                   if(pay[i]==pay[v])
    50                     return 0;
    51            }
    52      }
    53      return 1;
    54 }
    55 int main()
    56 {
    57     init();
    58     if(!bfs(1))
    59      cout<<"-1"<<endl;
    60     else
    61       {
    62           for(j=1;j<=n;j++)
    63            cout<<pay[j];
    64           cout<<endl;
    65       }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    如何删除或更改已经释放的TR
    [问题解决]调用BAPI_ACC_DOCUMENT_POST时报错“被合并的公司 XXXX 和 XXXX 是不同的”
    如何判断暂存采购订单(EKKO-MEMORY)
    Django之HttpRequest和HttpReponse
    Django之模板继承
    Django之模板语法
    python库之selectors
    python库之threading
    JDBC学习笔记(9)——DBUtils的使用
    XML学习笔记(1)--XML概述
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3243736.html
Copyright © 2011-2022 走看看