CF 272E Dima and Horses 染色,dfs 难度:2
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的更多相关文章
- 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 ...
- cf D. Dima and Trap Graph
http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...
- hdu 3478 Catch(染色 dfs 或 bfs )
Problem Description A thief is running away! We can consider the city to N–. The tricky thief starts ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- UVA 193 Graph Coloring 图染色 DFS 数据
题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色. 很水的一题,用dfs回溯即可.先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归 ...
- 缩点+染色+DFS codeforce467D
题目链接:https://vjudge.net/contest/219056#problem/A 推荐博客:https://blog.csdn.net/ck_boss/article/details/ ...
- CF 85E Guard Towers——二分图染色
题目:http://codeforces.com/contest/85/problem/E 当然是二分.然后连一个图,染色判断是不是二分图即可.方案数就是2^(连通块个数). 别真的连边!不然时间空间 ...
- [POI2010] GIL-Guilds - 二分图染色,DFS
给一张无向图,要求你用黑白灰给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白点,至少有一个黑点与他相邻,对于任意一个灰点,至少同时有一个黑点和白点和灰点与他相邻,问能否成功 S ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
随机推荐
- Git 基本操作(二)
1. 分支操作 1.1 Fast-forward 当被合并分支(C4)位于合并分支(C2)的历史线上,此时的合并称为"fast-forward"; // hotfix 被合并到 m ...
- (2.9)Mysql之SQL基础——索引的查看与删除
(2.9)Mysql之SQL基础——索引的查看与删除 关键词:mysql索引查看,mysql索引删除 1.索引查询(以下包括主键,唯一,普通,复合,全文,但不包括外键) (1)按库查询 select ...
- 通过html<map>标签给图片加链接
前面我们有谈到了通过图片定位给一张图片添加多个链接,现在用另外一种方法来实现,用html<map>标签给图片加链接 <img src="/images/hlj.jpg&qu ...
- OleDb未指定错误
桌面开发,居然也出这种问题: 1. C#读取Excel“未指定错误” http://www.connectionstrings.com/ http://www.dnetzj.com/Content/2 ...
- EasyUI Easyloader 加载器
用法 加载 EasyUI 模块 easyloader.base = '../'; // 设置 easyui 的基本目录 easyloader.load('messager', function(){ ...
- Android ActionBar自定义
关于自定义的ActionBar的实现过程,这里做下笔记以供之后查看. 1.默认状态 使用Android Studio新建一个名为“ActionBar”的应用,默认actionbar如图(1)所示. 图 ...
- springcloud20---Config加入eureka
Config server也可以加用户名和密码.Config client通过用户名和密码访问. Config server也可以做成高可用集群. Config与eureka配置使用.把Config ...
- 微信 audio 获取 duration 为 NaN 的解决方法
先加load() myaudio.load(); myaudio.oncanplay = function () { alert(myaudio.duration); } load() 方法用于在更改 ...
- Python笔记 #18# Pandas: Grouping
10 Minutes to pandas 引 By “group by” we are referring to a process involving one or more of the foll ...
- 微信小程序:工具配置 project.config.json
微信小程序:工具配置 project.config.json 一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.js ...