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
  • 相关阅读:
    得到一个文件夹中所有文件的名称的几个方法(命令指示符, C++, python)
    C++ 使用命名规范
    【前端】直击源头的让你3秒理解并且会用Jsonp!!!
    React Native新手入门
    【方法】纯jQuery实现星巴克官网导航栏效果
    【方法】jQuery无插件实现 鼠标拖动切换图片/内容 功能
    【总结】前端框架:react还是vue?
    【总结】2017年当下最值得你关注的前端开发框架,不知道你就OUT了!
    【疑点】<p></p>标签为什么不能包含块级标签?还有哪些特殊的HTML标签?
    【总结】最常用的正则表达式大全,你要找的这里都有!
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3243736.html
Copyright © 2011-2022 走看看