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 the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3.
Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party.
Help Dima split the horses into parties. Note that one of the parties can turn out to be empty.
Input
The first line contains two integers n, m — the number of horses in the horse land and the number of enemy pairs.
Next m lines define the enemy pairs. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), which mean that horse ai is the enemy of horse bi.
Consider the horses indexed in some way from 1 to n. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input.
Output
Print a line, consisting of n characters: the i-th character of the line must equal "0", if the horse number i needs to go to the first party, otherwise this character should equal "1".
If there isn't a way to divide the horses as required, print -1.
Examples
input
3 3
1 2
3 2
3 1
output
100
input
2 1
2 1
output
00
input
10 6
1 2
1 3
1 4
2 3
2 4
3 4
output
0110000000
假如一开始都是0那一组,然后开始遍历,到某一点与0这组里面冲突的有两个,那么就要把这个放到1这一组,找暴力第一组如果第一组也有两个冲突,那么在把跟他冲突的那个调到第一组,重复这个操作,完成假DFS即可,其实就是递归。
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
//---------------------------------Sexy operation--------------------------//
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define debug(n) printf("%d_________________________________\n",n);
#define speed ios_base::sync_with_stdio(0)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
//-------------------------------Actual option------------------------------//
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define Swap(a,b) a^=b^=a^=b
#define Max(a,b) (a>b?a:b)
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define mp(a,b) make_pair(a,b)
#define pb(n) push_back(n)
//--------------------------------constant----------------------------------//
#define INF 0x3f3f3f3f
#define esp 1e-9
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef long long ll;
//___________________________Dividing Line__________________________________//
const int maxn=300005;
int n,m,v[maxn]; //一开始全是0
vector<int>a[maxn];
void dfs(int x)
{
int n=a[x].size(),cnt=0;
for(int i=0; i<n; i++)
if(v[a[x][i]]==v[x]) cnt++;
if(cnt>=2)//在1里面有两个敌对,把他扔到2里
{
v[x]=!v[x];
for(int i=0; i<n; i++)
if(v[x]==v[a[x][i]]) dfs(a[x][i]);
}
}
int main()
{
cini(n),cini(m);
for(int i=1; i<=m; i++)
{
int x,y;
cini(x),cini(y);
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1; i<=n; i++)
dfs(i);
for(int i=1; i<=n; i++)
printf("%d",v[i]);
}
codeforce 272E Dima and Horses (假DFS)的更多相关文章
- CF 272E Dima and Horses 染色,dfs 难度:2
http://codeforces.com/problemset/problem/272/E 把仇恨关系想象为边, 因为度只能为0,1,2,3,所以有以下几种 0,1 直接放即可 2: 有(1,1), ...
- codeforce gym/100495/problem/F Snake++——DFS应用
emmmm.... 在被新生暴打后,我花了很久才补出这道DFS.由于WA1检查了半天,最后竟然是输出少了一个: ,心态小崩. 这里普通的dfs算出的连通区域并不能直接当做最后的答案.所以需要类似模 ...
- codeforce 272B Dima and Sequence
B. Dima and Sequence Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisti ...
- [CodeForce]358D Dima and Hares
有N<3000只宠物要喂,每次只能喂一只,每喂一只宠物,宠物的满足度取决于: 1 紧靠的两个邻居都没喂,a[i] 2 邻居中有一个喂过了,b[i] 3 两个邻居都喂过了,c[i] 把所有宠物喂一 ...
- Codeforces Round #167 (Div. 2)
C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...
- [Locked] Graph Valid Tree
Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is ...
- Codeforces Round #167 (Div. 1 + Div. 2)
C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...
- 关于雷达(Radar)信道
有些时候,我们在实际的无线网络中,会遇到无线信道一致flapping的情况,即便我们自定义了信道的,发现也会出现flapping.如果这种情况,可能需要确认是否你使用的信道上检测到了雷达. 这里记录一 ...
- cf374C Inna and Dima dfs判环+求最长链
题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...
随机推荐
- 操作系统-2-存储管理之LRU页面置换算法(LeetCode146)
LRU缓存机制 题目:运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制. 它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...
- 总结关于Mac上使用MySQL一些常见的问题
Num 1. MySQL5.7导出数据时提示--secure-file-priv解决办法: 问题分析 在官方的文档中,对secure_file_priv进行了说明,它用于限制数据的导出. secur ...
- es搜索排序不正确
沿用该文章里的数据https://www.cnblogs.com/MRLL/p/12691763.html 查询时发现,一模一样的name,但是相关度不一样 GET /z_test/doc/_sear ...
- editplus 怎么替换为换行
到editplus 的搜索 菜单中,选择替换,记住 这边如果是简单的一些 通用字符 替换可以直接替换,如果是一些特殊的字符 那必须选择 替换框左下中间的 “正则表达式”,即把这个“正则表达式” 前边的 ...
- java异常处理:finally中不要return
java异常处理:finally中不要return 复制代码 public class Ex1 { public static void main(String[] args) { System.ou ...
- [转] [知乎] 浅谈Roguelike
浅谈Roguelike 从柏林诠释说起 在2008年召开的国际Roguelike开发会议上,众多的Roguelike开发者与爱好者共同制定了<柏林诠释>,规定了Roguelike游戏需要具 ...
- C语言折半查找法练习题冒泡排序
C语言折半查找法练习题 折半查找法: 折半查找法是效率较高的一种查找方法.假设有已经按照从小到大的顺序排列好的五个整数num[0]~num[4],要查找的数是key,其基本思想是: 设查找数据的范围下 ...
- AJ学IOS(48)多线程网络之多线程简单了解
AJ分享,必须精品 一:进程和线程 1:什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内. 比如同时打开QQ.Xcode,系统就会分 ...
- webWMS开发过程记录(一)- 软件开发的流程
前言:计划开发一个webWMS,并将开发过程比较完整的记录下来.希望可以完成这个目标 软件开发的流程: 1. 了解该项目的相关概念. 了解所要开发的软件属于什么产品.该产品的基本定义是什么?基本功能模 ...
- 高级工程师-Java注解
高级工程师-Java注解 前言 代码,就是我们身为程序员的名片. 简洁,优雅,统一,是我们的追求. 优秀的代码,会给浏览者一种艺术的美感.如DL大神的JUC包,感兴趣的小伙伴,可以研究一下. 那么日常 ...