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 ...
随机推荐
- Instagram的技术探索(转)
add by zhj: 略有修改 原文:http://www.cnblogs.com/xiekeli/archive/2012/05/28/2520770.html 前一篇翻译了Instagram b ...
- 存储5——逻辑卷管理LVM
1. LVM概念 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现.LVM将一个或多个硬盘的分区在逻辑上 ...
- Docker Compose 入门使用指南
Compose is a tool for defining and running multi-container Docker applications. With Compose, you us ...
- #ifdef和#if defined的差别
注意两者都有个define的作用,区别在于使用方式上.前者的通常用法是:#ifdef XXX .... #else .... #endif 只能在两者中选择是否有定义.对于后者,常用法是: #if ...
- Integration Services 变量
如果没有变量,你会发现在ssis里面啥都干不成,和人没有灵魂一样 对系统变量唯一可配置的选项是指定变量在更改值时是否引发事件. 待续
- 机器学习实战python3 K近邻(KNN)算法实现
台大机器技法跟基石都看完了,但是没有编程一直,现在打算结合周志华的<机器学习>,撸一遍机器学习实战, 原书是python2 的,但是本人感觉python3更好用一些,所以打算用python ...
- Learning to Rank之RankNet算法简介
排序一直是信息检索的核心问题之一, Learning to Rank(简称LTR)用机器学习的思想来解决排序问题(关于Learning to Rank的简介请见我的博文Learning to Rank ...
- WdatePicker显示乱码
1.修改zh-cn.js内容: var $lang={ errAlertMsg: "不合法的日期格式或者日期超出限定范围,需要撤销吗?", aWeekStr: ["周&q ...
- 20189215《Linux内核原理与分析》第一周作业
实验1 Linux系统简介 本节主要学习了 Linux 的历史,Linux 与 Windows 的区别等入门知识.通过学习,我明确了目的,是要用 Linux 来做程序开发.搭建服务器等:并且非常接受不 ...
- ubuntu 16.04+Anaconda+theano+keras安装【转】
本文转载自:https://blog.csdn.net/u013786021/article/details/78370138 安装软件部分浪费了好长时间才装好.之前一直各种问题,后来卸卸了radin ...