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. nginx简介和配置gd

    转自:https://www.cnblogs.com/zhouxinfei/p/7862285.html nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也 ...

  2. Linux学习 -->解决Ubuntu系统上 No command 'crond' found

    前两天,准备在Ubuntu服务器上,定时执行Gitlab备份的命令,如下所示 编辑 vi /etc/crontab 文件,添加如下定时脚本 # edited by ouyang 2017-8-11 添 ...

  3. T-SQL练习题

    转自:http://www.cnblogs.com/jenrrychen/p/5348546.html 1 - 3 题: 数据表结构: OrderID ProductID OrderDate  Sal ...

  4. js-template-art【二】语法

    参看地址 一.模板语法 1.变量使用与输出 <% if (user) { %> <h2><%= user.name %></h2> <% } %& ...

  5. 一个Spark job的生命历程

    一个job的生命历程 dagScheduler.runJob //(1) --> submitJob ( eventProcessLoop.post(JobSubmitted,***) //(2 ...

  6. Spark调优秘诀

    1.诊断内存的消耗 在Spark应用程序中,内存都消耗在哪了? 1.每个Java对象都有一个包含该对象元数据的对象头,其大小是16个Byte.由于在写代码时候,可能会出现这种情况:对象头比对象本身占有 ...

  7. [py][lc]python的纸牌知识点

    模块collections-collections.namedtuple表示tuple 如表示一个坐标, t = (1,2), 搞不清楚. 如果这样就对了Point(x=1, y=2) from co ...

  8. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online Solution

    A    Live Love 水. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; const i ...

  9. [one day one question] 有没有免费接收短信验证用于注册的软件或者平台?

    问题描述: 想要批量注册撸羊毛,有手机短信验证码验证,这怎么破? 解决方案: 免费的肯定没有的,不过"一条短信收费一毛钱"倒是有一个,本人是亲自试用过,该平台收不到短信验证码不收费 ...

  10. PHP设计模式_单例模式

    了解 单例设计模式用于限制特定对象只能被实例化创建一次,有且只有一个此类型的资源.例如,通过数据库句柄到数据库的连接是独占的.您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销 ...