URAL 1080 Map Coloring(染色)
Map Coloring
Memory limit: 64 MB
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
Output
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 |
010 |
#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(染色)的更多相关文章
- ural 1106,二分图染色,DFS
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染 ...
- 训练报告 (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 ...
- 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 据 ...
- 开源GIS软件 2
Android上的导航软件 AndNav AndNav 是一款 Android 手机上的 GPS导航软件(非开源).软件支持GPS定位信息,目的地查询,道路建议管理,导航提示等功能,十分强大的一款软件 ...
- Tars | 第3篇 Tars中期汇报测试文档(Java语言实现Subset路由规则)
目录 前言 1. 任务介绍 2. 测试模拟方案 2.0 *前置工作 2.1 添加路由规则 2.2 添加存活节点 2.3 [输出]遍历输出当前存活节点 2.4 [核心]对存活节点按subset规则过滤 ...
- Tars | 第6篇 基于TarsGo Subset路由规则的Java JDK实现方式(下)
目录 前言 1. 修改.tars协议文件 1.1 Java源码位置及逻辑分析 1.2 Java语言实现方式 1.3 通过协议文件自动生成代码 1.4 变更代码的路径 2. [核心]增添Subset核心 ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- ural 1106. Two Teams 二分图染色
链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 描述:有n(n<=100)个人,每个人有一个或多个朋友(朋友关系是相互的).将其 ...
- URAL 1837. Isenbaev's Number (map + Dijkstra || BFS)
1837. Isenbaev's Number Time limit: 0.5 second Memory limit: 64 MB Vladislav Isenbaev is a two-time ...
随机推荐
- Android的R.java文件
1.Android资源管理简介: Android应用程序资源可以分为两大类,分别放在assets和res文件夹下.assets目录下保存的是一些原始的文件,可以以任何方式来进行组织.这些文件最终会被原 ...
- HDFS的可靠性
HDFS的可靠性 1.冗余副本策略 2.机架策略 3.心跳机制 4.安全模式 5.校验和 6.回收站 7.元数据保护 8.快照机制 1.冗余副本策 ...
- goldengate 12c对oracle DB的改进
1. 现在可使用Oracle Universal Installer,即安装时有图形化界面,同时会自动安装java runtime environment,不过个人认为,还是ZIP安装包方便,解压即用 ...
- 第一课 Hello
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; ...
- HTML---6 运算符,类型转换
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- python global vs nonlocal (2)
上一篇中简述了这两个关键字的基本用法: global用于在局部作用于修改或重定义全局变量 nonlocal用于在内部作用域修改或重定义外部变量(全局变量除外) 这只是很浅的理解方式 注意python的 ...
- 极客DIY:开源WiFi智能手表制作
如果你喜欢拥有一款属于自己的无线手表,那么请不要错过,相信阅读完这篇文章对你会很有帮助. 硬件规格 ESP8266(32Mbit闪存) MPU-9250(陀螺仪传感器)以及 AK8963(内置磁力计) ...
- python的循环语句等
names = ['Michael', 'Bob', 'Tracy'] for name in names: print name sum = 0 for x in [1, 2, 3, 4, 5, 6 ...
- CSS样式选择器
<!-- css样式选择器 HTML选择器 类选择器 ID选择器 关联选择器 组合选择器 伪元素选择器 selector{ /* selector是样式选择器 property:value; / ...
- 如何从oc中去获取一个私有的变量.....
运行时 的用法 1.定义的一个类,里面有一个私有变量mt_,并且在初始化值为"HaHa Ha ".@interface Mobj : NSObject {@privateNSStr ...