Graph Coloring I(染色)
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
-->
输入
3 2
1 2
1 3
输出
0
0 1 1
输入
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(染色)的更多相关文章
- UVA 193 Graph Coloring 图染色 DFS 数据
题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色. 很水的一题,用dfs回溯即可.先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归 ...
- Codeforces 664D Graph Coloring 二分图染色
题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...
- UVA Graph Coloring
主题如以下: Graph Coloring You are to write a program that tries to find an optimal coloring for agiven ...
- poj 1419 Graph Coloring
http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...
- GPS-Graph Processing System Graph Coloring算法分析 (三)
HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158 Graph coloring is the problem of assignin ...
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4893 Accepted: 2271 ...
- POJ1419 Graph Coloring(最大独立集)(最大团)
Graph Coloring Time Limit: 1000MS Memor ...
- uva193 - Graph Coloring
Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
随机推荐
- [转]判断是否 Win7 且需要管理员权限
public static void Load() { if (NeedAdmin()) { new Form().ShowDialog(); Environment.Exit(); } } publ ...
- [UE4]GetWorld()->GetDeltaSeconds()方法
void AAvatar::Yaw(float amount) { if (Controller && amount) { // AddControllerYawInput()函数用于 ...
- Linux网络编程经典书籍推荐
UNIX环境高级编程<高级unix环境编程><unix网络编程><深入理解计算机系统>比较好 =====================Linux网络编程经典书籍推 ...
- Web 单元测试
问题描述: The import org.junit.Test conflicts with a type defined in the same file 导入的org.junit.Test和一个相 ...
- 对象DIY
1.在java开发中,好代码都是组织的比较好,模拟现实很好,而不是步骤指令 2.对象组织+继承(归类
- nodejs——向另一个服务器发送文件
业务需要,需在客户本地建立一个服务,将本地的数据库文件发送到主服务器. 下面是本地服务代码: var exec = require('child_process').exec; var request ...
- shell中使用函数
函数定义.调用 $ cat te.sh #!/bin/bash # define a function test() { echo "This is a function." } ...
- Android手机图片路径
H:\dcim\100MEDIA H:\Tencent\MobileQQ\photo H:\Tencent\MobileQQ\photo H:\Tencent\MobileQQ\thumb H:\Te ...
- uva-10004-俩色图验证
题意: 在1976年,四色猜想被一个计算机助手提出,这个理论表示对任意一个地图的上色都只需要四种颜色,地图内每一个区块和相邻的区块颜色都不相同.你现在被要求解决一个相似但相对简单的问题.给你任意一个连 ...
- 应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测
应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测 Real-Time Anomaly Detection for Streaming Analytics Subutai Ahmad SAHM ...