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

input output
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 = ;
const int M = ;
int n,m,k,l,tot=;
int parent[N],pre[N],vis[N],color[N];
vector<int>vec[N];
bool ok=true;
void dfs(int u){
vis[u]=;
for(int i=;i<vec[u].size();i++){
int v=vec[u][i];
if(!vis[v]){
color[v]=(color[u]+)%;
dfs(v);
}
else if(vis[v]&&color[v]==color[u]){
ok=false;return;
}
}
}
int main() {
int u,v;
scanf("%d",&n);
for(int i=;i<=n;i++){
while(){
scanf("%d",&v);
if(!v)break;
vec[i].pb(v);vec[v].pb(i);
}
}
for(int i=;i<=n;i++){
if(!vis[i]){
color[i]=;
dfs(i);
}
}
if(ok)for(int i=;i<=n;i++)printf("%d",color[i]);
else printf("-1");
printf("\n"); return ;
}

URAL 1080 Map Coloring(染色)的更多相关文章

  1. ural 1106,二分图染色,DFS

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染 ...

  2. 训练报告 (2014-2015) 2014, Samara SAU ACM ICPC Quarterfinal Qualification Contest

    Solved A Gym 100488A Yet Another Goat in the Garden   B Gym 100488B Impossible to Guess Solved C Gym ...

  3. 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】

    2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...

  4. 开源GIS软件 2

    Android上的导航软件 AndNav AndNav 是一款 Android 手机上的 GPS导航软件(非开源).软件支持GPS定位信息,目的地查询,道路建议管理,导航提示等功能,十分强大的一款软件 ...

  5. Tars | 第3篇 Tars中期汇报测试文档(Java语言实现Subset路由规则)

    目录 前言 1. 任务介绍 2. 测试模拟方案 2.0 *前置工作 2.1 添加路由规则 2.2 添加存活节点 2.3 [输出]遍历输出当前存活节点 2.4 [核心]对存活节点按subset规则过滤 ...

  6. Tars | 第6篇 基于TarsGo Subset路由规则的Java JDK实现方式(下)

    目录 前言 1. 修改.tars协议文件 1.1 Java源码位置及逻辑分析 1.2 Java语言实现方式 1.3 通过协议文件自动生成代码 1.4 变更代码的路径 2. [核心]增添Subset核心 ...

  7. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  8. ural 1106. Two Teams 二分图染色

    链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 描述:有n(n<=100)个人,每个人有一个或多个朋友(朋友关系是相互的).将其 ...

  9. URAL 1837. Isenbaev&#39;s Number (map + Dijkstra || BFS)

    1837. Isenbaev's Number Time limit: 0.5 second Memory limit: 64 MB Vladislav Isenbaev is a two-time ...

随机推荐

  1. C/C++学习之基础-001

    1.C++虚函数的工作原理 虚函数(virtual function)需要虚函数表(virtual table)才能实现.如果一个类有函数声明成虚拟的,就会生成一个虚函数表,存放这个类的虚函数地址.若 ...

  2. 虚拟机无法上网的问题:无法启动VMnet0等问题

    虚拟机无法上网,由于之前安装过虚拟机,后来将它卸载了,然后重新安装,最后出现了虚拟机无法上网.刚开始以为是系统的原因,于是就通过linux命令查看系统里面的网卡时是否启动,如:/etc/init.d/ ...

  3. 2016 - 1- 22 NSURLConnetction --- POST请求

    一:与上一篇博客中的GET方法类似  只不过需要多注意,如果要改变请求的类型,需要生成NSMutableURLRequest对象才可以设置请求的类型. NSURL *url = [NSURL URLW ...

  4. Interview----将一棵二叉树转换成其镜像

    题目:输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点. 用递归和循环两种方法完成树的镜像转换. 例如输入:        8      /    ...

  5. wMy_Python ~储存相关~

    str,int,list,tuple,dict 是类型调用之后会产生一个 实例 >>> brand=["李宁",'耐克','阿迪达斯','鱼C'] >> ...

  6. Ubuntu系统启用Apache Mod_rewrite模块

    在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available/rewrite.l ...

  7. Squid代理之普通代理

    1.配置Squid 代理服务器IP地址 将eth1的IP地址修改为200.168.10.1

  8. 【LeetCode OJ】LRU Cache

    Problem Link: http://oj.leetcode.com/problems/lru-cache/ Long long ago, I had a post for implementin ...

  9. 8、C#基础整理(数组和冒泡排序)

    数组 概念:定义一组同类型的指定个数的变量,索引从0开始 例: ];//定义一组有10个数据的数组 shuname[] = ; Console.WriteLine(shuname[]);//打印出1 ...

  10. IDataReader转换成list通用方法

    public static IList<T> ReaderToList<T>(this IDataReader dr) { //DateTime dt = DateTime.N ...