Graph Coloring I

https://www.nowcoder.com/acm/contest/203/J

题目描述

修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染色,满足相邻点不同色。
澜澜不服气,在黑板上画了一个三个点的完全图。修修跟澜澜说,这个图我能找到一个简单奇环。
澜澜又在黑板上画了一个n个点m条边的无向连通图。很可惜这不是一道数数题,修修做不出来了。
澜澜非常得意,作为一位毒瘤出题人,有了好题当然要跟大家分享,于是他把这道题出给你做了。

输入描述:

第一行两个整数n,m (1≤ n,m≤ 3*10

5

),接下来m行每行两个整数a

i

,b

i

表示一条边 (1≤ a

i

,b

i

≤ n)。
保证图连通,并且不存在重边和自环。

输出描述:

如果你能把图二染色,第一行输出0,第二行输出n个整数

表示每个点的颜色 (0≤ x

i

≤ 1)。如果有多种合法方案,你可以输出任意一种。
如果你能找到一个简单奇环,第一行输出环长k,第二行输出k个整数

表示环上结点编号 (1≤ y

i

≤ n),你需要保证y

i

和y

i+1

之间有边,y

1

和y

n

之间有边。如果有多种合法方案,你可以输出任意一种。
如果两种情况都是可行的,你只需要输出任意一种。
如果两种情况都是不可行的,请输出一行一个整数-1。

输入例子:
3 2
1 2
1 3
输出例子:
0
0 1 1

-->

示例1

输入

3 2
1 2
1 3

输出

0
0 1 1
示例2

输入

3 3
1 2
1 3
2 3

输出

3
1 2 3

用dfs边搜索边染色,看看有没有相邻的节点有相同的颜色,并判断是否有奇环(如果相邻的节点有相同的颜色就说明存在奇环),都不满足的话就输出-1

 #include<iostream>
#include<cmath>
#include<vector>
#include<cstring>
#include<algorithm>
#define maxn 300005
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std; vector<int>ve[maxn];
int book[maxn];
int color[maxn];
int n,m;
int flag,Start; int dfs(int pos,int fa,int clr,int num){
if(book[pos]){
if(color[pos]==(clr^)) flag=;
if((num-book[pos])&){
cout<<num-book[pos]<<endl;
Start=pos;
return ;
}
return ;
}
book[pos]=num;
color[pos]=clr;
for(int i=;i<ve[pos].size();i++){
if(ve[pos][i]!=fa){
if(!dfs(ve[pos][i],pos,clr^,num+)){
if(pos!=Start){
cout<<pos<<" ";
return ;
}
else{
cout<<pos<<endl;
exit();
}
}
}
}
return ;
} int main(){
std::ios::sync_with_stdio(false);
cin>>n>>m;
int a,b;
flag=;
for(int i=;i<=m;i++){
cin>>a>>b;
ve[a].push_back(b);
ve[b].push_back(a);
}
mem(book,);
mem(color,-);
if(!dfs(,,,)) return ;
if(flag){
cout<<-<<endl;
}
else if(!flag){
cout<<<<endl;
for(int i=;i<=n;i++){
if(i!=){
cout<<" ";
}
cout<<color[i];
}
cout<<endl;
}
}

Graph Coloring I(染色)的更多相关文章

  1. UVA 193 Graph Coloring 图染色 DFS 数据

    题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色. 很水的一题,用dfs回溯即可.先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归 ...

  2. Codeforces 664D Graph Coloring 二分图染色

    题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...

  3. UVA Graph Coloring

    主题如以下: Graph Coloring  You are to write a program that tries to find an optimal coloring for agiven ...

  4. poj 1419 Graph Coloring

    http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...

  5. GPS-Graph Processing System Graph Coloring算法分析 (三)

        HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158     Graph coloring is the problem of assignin ...

  6. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

  7. POJ1419 Graph Coloring(最大独立集)(最大团)

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  8. uva193 - Graph Coloring

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

  9. 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5775   Accepted: 2678   ...

随机推荐

  1. 开源框架:Apache的DBUtils框架

    开源框架:Apache的DBUtils框架 Commons DbUtils 1.4 API 开源框架:DBUtils使用详解 Download Apache Commons DbUtils  官方文档

  2. python selenium-2 定位元素

    元素 方法 示例 id find_element_by_id('su') driver.get("http://www.baidu.com")driver.find_element ...

  3. shell 9test命令

    shell中的test用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 * -eq 等于为true * -ne 不等,为true * -gt 大于,为true * -ge ...

  4. ROS-RouterOS 的license注册级别

    原文: https://wiki.mikrotik.com/wiki/Manual:CHR#CHR_Licensing https://wiki.mikrotik.com/wiki/Manual:Li ...

  5. 利用.pbk来实现ADSL开机自动拨号

    当你新建拨号连接或者VPN连接之后在你的电脑里会创建一个.pbk的文件 这个.pbk的文件可以说是一个集合,将你电脑的所有连接都保存在一起. 同时你还可以将此连接复制起来传给其他人. 系统默认的.pb ...

  6. git 场景 :从一个分支cherry-pick多个commit

    场景: 在branch1开发,进行多个提交,这是切换到branch2,想把之前branch1分支提交的commit都[复制]过来,怎么办? 首先切换到branch1分支,然后查看提交历史记录,也可以用 ...

  7. 不重启修改'log_slave_updates'变量

    Variable 'log_slave_updates' is a read only variable 不重启修改mysql变量 执行复制的时候遇到的问题 mysql> show variab ...

  8. tensorflow定义神经网络损失函数MSE

    import numpy as np import tensorflow as tf y_pred = np.array([[1], [2], [3]],dtype=np.float32) y_rea ...

  9. hive的安装,一般不容易察觉的hdfs的配置问题导致hive安装的失败

    在安装hive的过程中,一般需要的准备环境就是hadoop集群的正常启动,要装有mysql,zookeeper. 具体怎么安装配置我在这里不多说,安装教程网上有挺多的. 我这里说下我遇到的问题,首先从 ...

  10. gz文件最后四位检测

    [root@node-0 ~]# ll -rw-r--r--  1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...