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 ...
随机推荐
- POJ 3648 2-sat
题目大意: 有一对新人结婚,邀请n对夫妇去参加婚礼. 有一张很长的桌子,人只能坐在桌子的两边,还要满 足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇 之中可能有通奸关系(包括男男,男女,女女) ...
- io函数
io函数一般分为两大类: 系统(不带缓存)调用: 如read.write.open 标准(带缓存)调用: fread.fwrite.fopen 上面说的带缓存/不带缓存是针对用户态的,内核态本身都是带 ...
- linux内核编译
1,进入内核源码树,如果是第一次编译,建议清理以下内核功能选择文件: make mrproper 2,删除前一次编译的残留文件: make clean 3,配置内核功能 make menuconfig ...
- Xcode如何查看内存中的数据
在 debug 模式下如何在断点处,查看字符指针变量内存中的值,像vs2008的调试工具一样的内存查看器,现在只能查看第一个内存中的值可以在输出窗口采用gdb命令:x /nfu <addr&g ...
- C# 跨线程操作控件(简洁)
C# 跨线程操作控件 .net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生.解决此问题的方法有两个: 第一 ...
- Ubuntu 14.10 下安装navicat
1 下载navicat,网址http://www.navicat.com.cn/download,我下载的是navicat111_premium_cs.tar.gz 2 解压到合适的位置 3 进入解压 ...
- 王家卫i
〈旺角卡门〉 因为我很了解我自己,我不能对你承诺什么. 你不要说两次,说两次我就相信了. 我宁愿做一日英雄,都不想成世做条虫! 厨房里有煮好的饭,另外我还买了几个杯子,我知道,用不了多久就都会被打破, ...
- UIkit框架之UItableview
1.继承链:UIScrrollView:UIview:UIresponder:NSObject 2.创建实例的时候首先需要确定table的类型 3.一个tableview对象必须要有一个数据源和一个委 ...
- 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)
题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...
- Margaritas on the River Walk_背包
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...