题目链接: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. js添加方法和邦定事件

    function(obj,objArr){ if(($(obj).attr("type") == "checkbox" && $j(obj).p ...

  2. php 生成bing词典能导入的xml(有道词典->bing词典)

    编程以来一直用网易有道词典查单词.翻译:最近一直在看英文方面的资料,于是越来越对有道词典(划词.广告,本来想转灵格斯的,但灵格斯没有android版)不满意,一番试用后决定转bing词典,于是想把有道 ...

  3. 【Leetcode-easy】ZigZag Conversion

    思路1:String[numRow]行字符串数组.读取原始字符串每一个字符,设置行变量 nrow和行标志位flag(向下一行为1或向上一行为-1).将该字符连接到数组中对应的行字符串,同时nrow+= ...

  4. leetcode 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  5. 8--json交互

    8.1 为什么要进行json数据交互 json数据格式在接口调用.html页面中较常用,json格式较简单,解析较方便. 比如:webservice接口,传输json数据. 8.2      spri ...

  6. 理解多线程中的ManualResetEvent(C#)

    线程是程序中的控制流程的封装.你可能已经习惯于写单线程程序,也就是,程序在它们的代码中一次只在一条路中执行.如果你多弄几个线程的话,代码运行可能会更加“同步”.在一个有着多线程的典型进程中,零个或更多 ...

  7. create-react-app使用的问题

    // 设置 npm config set registry https://registry.npm.taobao.org // 验证是否成功 npm config get registry或npm ...

  8. BZOJ_4025_二分图_线段树按时间分治+并查集

    BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...

  9. unicode和utf-8互转

    1.1 ASCII码 我们知道, 在计算机内部, 所有的信息最终都表示为一个二进制的字符串. 每一个二进制位(bit)有0和1两种状态, 因此八个二进制位就可以组合出 256种状态, 这被称为一个字节 ...

  10. 理解I/O Completion Port(完成端口)

    欢迎阅读此篇IOCP教程.我将先给出IOCP的定义然后给出它的实现方法,最后剖析一个Echo程序来为您拨开IOCP的谜云,除去你心中对IOCP的烦恼.OK,但我不能保证你明白IOCP的一切,但我会尽我 ...