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 ...
随机推荐
- HDU 5442 后缀自动机+kmp
题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样大,希望找到起始位置最小的,如果还相同,就默认顺时针 比赛一直因为处理最小位置出错,一结束就想 ...
- 未能加载文件或程序集“DeveloperKit10.1/DotNet/ESRI.ArcGIS.ADF.Local.或它的某一个依赖项
使用VS2010进行ArcGIS Engine 10.1进行开发过程中,出现: 错误 1 未能加载文件或程序集“file:///D:/ArcGIS/DeveloperKit10.0/DotNet/ES ...
- MongoDB的安装、配置服务(转)
一,简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个高性能,开源,无模式的文档型数据库,是当前 ...
- 实现viewpager下的圆点滑动
在Drawable目录下创建资源文件 使用shape标签画出背景圆点与当前圆点 背景圆点: <?xml version="1.0" encoding="utf-8& ...
- 用C++,调用浏览器打开一个网页
http://blog.csdn.net/heaven13483/article/details/9369029
- linux基础命令学习五(软件包管理、下载管理)
Linux 软件包管理 本文主要是记录下RedHat系列的软件包管理. 内容分为以下二个部分:二进制包的管理,源代码包的管理 一.二进制包的管理 1.1概念 主要有RPM和YUM这两种包管理. 两 ...
- HttpApplication 类,HttpApplicationState 类
HttpApplication 类 定义 ASP.NET 应用程序中的所有应用程序对象共有的方法.属性和事件.此类是用户在 Global.asax 文件中所定义的应用程序的基类. https://ms ...
- [ASP.net教程]ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等)
以下是关于ASP.NET中保存各种信息的对象的比较,理解这些对象的原理,对制作完善的程序来说是相当有必要的(摘至互联网,并非原创--xukunping)在ASP.NET中,有很多种保存信息的对象.例如 ...
- 深入分析:Fragment与Activity交互的几种方式(二,使用Bundle)
首先我们需要在Activity中动态添加Fragment时,用Bundle封装我们需要传递的数据. public void button(View view) { ArgFragment arg = ...
- R函数是对A方法的封装
$user = new UserController; === $user=A("User"); $user = new UserController; $user-& ...