http://codeforces.com/problemset/problem/272/E

把仇恨关系想象为边,

因为度只能为0,1,2,3,所以有以下几种

0,1 直接放即可

2: 有(1,1),(0,2)两种情况,第一种随便放,第二种放0那里

3:有(1,2),(0,3)两种情况,第一种放1,第二种放0那里

也就是说,怎样都有解

dfs寻找即可

不过一开始觉得这样不靠谱,因为时间复杂度很高,不过没想到过了

#include <cstdio>
#include <vector>
using namespace std;
const int maxn=3e5+5;
bool other[maxn];
vector<int> e[maxn];
int n,m;
void print(){
for(int i=1;i<=n;i++){
if(other[i])printf("1");
else printf("0");
}
puts("");
}
void judge(int s){
int o=0;
for(int i=0;i<(int)e[s].size();i++){
int t=e[s][i];
if(other[t]==other[s])o++;
}
if(o>1){
other[s]=!other[s];
for(int i=0;i<(int)e[s].size();i++){
int t=e[s][i];
if(other[t]==other[s])judge(t);
}
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int f,t;
scanf("%d%d",&f,&t);
e[f].push_back(t);
e[t].push_back(f);
}
for(int i=1;i<=n;i++){
judge(i);
}
print(); return 0;
}

  

CF 272E Dima and Horses 染色,dfs 难度:2的更多相关文章

  1. codeforce 272E Dima and Horses (假DFS)

    E. Dima and Horses Dima came to the horse land. There are n horses living in the land. Each horse in ...

  2. cf D. Dima and Trap Graph

    http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...

  3. hdu 3478 Catch(染色 dfs 或 bfs )

    Problem Description A thief is running away! We can consider the city to N–. The tricky thief starts ...

  4. uva 193 Graph Coloring(图染色 dfs回溯)

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

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

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

  6. 缩点+染色+DFS codeforce467D

    题目链接:https://vjudge.net/contest/219056#problem/A 推荐博客:https://blog.csdn.net/ck_boss/article/details/ ...

  7. CF 85E Guard Towers——二分图染色

    题目:http://codeforces.com/contest/85/problem/E 当然是二分.然后连一个图,染色判断是不是二分图即可.方案数就是2^(连通块个数). 别真的连边!不然时间空间 ...

  8. [POI2010] GIL-Guilds - 二分图染色,DFS

    给一张无向图,要求你用黑白灰给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白点,至少有一个黑点与他相邻,对于任意一个灰点,至少同时有一个黑点和白点和灰点与他相邻,问能否成功 S ...

  9. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

随机推荐

  1. Python开发【笔记】: __get__和__getattr__和__getattribute__区别

    引言: 1.object.__getattr__(self, name) 当一般位置找不到attribute的时候,会调用getattr,返回一个值或AttributeError异常. 2.objec ...

  2. HTML实例 - 购物商场页面

    效果图 代码 https://coding.net/u/James_Lian/p/Shopping-page/git 示例 https://coding.net/u/James_Lian/p/Shop ...

  3. 洛谷P3939 数颜色 二分查找

    正解:二分 解题报告: 传送门! 话说其实我开始看到这题想到的是分块,,, 但是显然不用这么复杂,,,因为仔细看下这题,会发现每次只改变相邻的兔子的位置 所以开个vector(或者开个数组也成QwQ( ...

  4. hive引入jar包--HIVE.AUX.JARS.PATH和hive.aux.jars.path

    hive需要引入包时?该怎么引入? 一.hive-site.xml中的hive.aux.jars.path 此配置项对于hive server有效,但是是不会作用到hive shell.也就是说即使你 ...

  5. HTTP cookies 详解(国外一位大牛的文章)

    原文:http://blog.csdn.net/lijing198997/article/details/9378047 HTTP cookies,通常又称作"cookies",已 ...

  6. android唯一设备标识、设备号、设备ID的获取方法

    ##如何获取Android设备唯一ID? ###问题 每一个android设备都有唯一ID吗?如果有?怎么用java最简单取得呢? ###回答1(最佳) 如何取得android唯一码? 好处: 1.不 ...

  7. Openstack(六)RabbitMQ集群

    各组件通过消息发送与接收是实现组件之间的通信: 6.1安装RabbitMQ 6.1.1安装RabbitMQ # yum install rabbitmq-server –y # systemctl s ...

  8. PAT 1137 Final Grading[一般][排序]

    1137 Final Grading(25 分) For a student taking the online course "Data Structures" on China ...

  9. 在jQuery中解决事件冒泡问题

    <pre name="code" class="html">事件会按照DOM层次结构像水泡一样不断向上直至顶端 解决方法:在事件处理函数中返回fal ...

  10. 接口测试之接口api文档的重要性

    接口文档的特点 接口文档,顾名思义就是对接口说明的文档.好的接口文档包含了对接口URL,参数以及输出内容的说明,我们参照接口文档就能编写出一个个的测试用例.而且接口文档详细的话,测试用例编写简单,不会 ...