题目链接:http://codeforces.com/problemset/problem/687/A

题意:给出一个n个点m条边的图,分别将每条边连接的两个点放到两个集合中,输出两个集合中的点,若不可能则输出-1;

思路:通过画图我们不难发现,图中没有出现长度为奇数的环则是可行的,反之则是不行的.

对于这点我们可以直接用dfs搜索+染色,对于当前标记为1的点,我们将其所有儿子标记为2, 对于当前标记为2的点,将其所有儿子标记为1,若出现某个节点的标记与其儿子相同,则有长度为奇数的环。

代码:

 #include <bits/stdc++.h>
using namespace std; const int MAXN=1e5+;
vector<int> v[MAXN];
vector<int> st1, st2;
bool flag=false;
int vis[MAXN]; void dfs(int point){
if(flag){
return;
}
for(int i=; i<v[point].size(); i++){
int cnt=v[point][i];
if(vis[point]==vis[cnt]){
flag=true;
return;
}else if(!vis[cnt]){
if(vis[point]==){
vis[cnt]=;
st2.push_back(cnt);
dfs(cnt);
}else if(vis[point]==){
vis[cnt]=;
st1.push_back(cnt);
dfs(cnt);
}
}
}
} int main(void){
ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n, m, x, y;
cin >> n >> m;
while(m--){
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=; i<=n; i++){
if(v[i].size()==){
continue;
}else if(!vis[i]){
vis[i]=;
st1.push_back(i);
dfs(i);
}
}
if(flag){
cout << - << endl;
}else{
cout << st1.size() << endl;
for(int i=; i<st1.size(); i++){
cout << st1[i] << " ";
}
cout << endl;
cout << st2.size() << endl;
for(int i=; i<st2.size(); i++){
cout << st2[i] << " ";
}
cout << endl;
}
return ;
}

Codeforces Round #360 (Div. 1)A (二分图&dfs染色)的更多相关文章

  1. Codeforces Round #360 (Div. 2)——C. NP-Hard Problem(BFS染色判二分图)

    C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题

    C. NP-Hard Problem 题目连接: http://www.codeforces.com/contest/688/problem/C Description Recently, Pari ...

  3. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

  4. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

  5. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  6. Codeforces Round #383 (Div. 1) C(二分图)

    一道很巧妙的二分图的题目 简单分析性质可知,一个合法序列一定是由12,21这样的子串构成的,所以相邻的每隔2个两两配对 然后BF和GF互相配对,思考一下,如果存在奇环,那么必定有一个BG有两个GF,或 ...

  7. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  8. Codeforces Round #435 (Div. 2) B (二分图) C(构造)

    B. Mahmoud and Ehab and the bipartiteness time limit per test 2 seconds memory limit per test 256 me ...

  9. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

随机推荐

  1. ThreadLocalMap里Entry声明为WeakReference

    Java里,每个线程都有自己的ThreadLocalMap,里边存着自己私有的对象.Map的Entry里,key为ThreadLocal对象,value即为私有对象T.在spring MVC中,常用T ...

  2. UVA - 11464 Even Parity 【暴力枚举】

    题意 给出一个 01 二维方阵 可以将里面的 0 改成1 但是 不能够 将 1 改成 0 然后这个方阵 会对应另外一个 方阵 另外一个方阵当中的元素 为 上 下 左 右 四个元素(如果存在)的和 要求 ...

  3. 简单学习github代码托管

    之前尝试使用阿里云code做代码托管 egret+git+阿里云code搭建团队开发 ,现在来学习一下使用 Github做代码托管服务. 总体上看使用的步骤差不多,都需要使用GIT客户端来进行相关的操 ...

  4. git创建项目的两种方式

    场景1: 将本地内容推送给远程库 1.创建版本库 git init 将此目录转换为git可管理的仓库 git config --global user.name "xx" 或 gi ...

  5. 对于glut和freeglut的一点比较和在VS2013上的配置问题

    先大概说一下glut.h和freeglut.h 首先要知道openGL是只提供绘图,不管窗口的,所以你需要给它一个绘图的区域(openGL能跨平台也与此有些关系) glut.h和freeglut.h都 ...

  6. python把源代码打包成.exe文件

    1.在windows命令行把当前文件夹用cd命令切换到源代码所在文件夹. 2.输入命令:pyinstaller -w -F main.py

  7. hdu5776sum

    题目连接  抽屉原理:如果现在有3个苹果,放进2个抽屉,那么至少有一个抽屉里面会有两个苹果 抽屉原理的运用 现在假设有一个正整数序列a1,a2,a3,a4.....an,试证明我们一定能够找到一段连续 ...

  8. leetcode 304. Range Sum Query 2D - Immutable(递推)

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

    py文件直接在cmd窗口用python命令执行时正常:代码逐句在ipython中也正常:但是, 在wingIDE中运行报错“UnicodeEncodeError: 'ascii' codec can' ...

  10. MySQL_各城市在线产品天订单数据20161130

    #sealreport010 `tb010_02d`各城市在线产品天订单数据 #sealreport010 `tb010_02d`各城市在线产品天订单数据 SELECT d.ID,a.城市,a.在线日 ...