Codeforces687A - NP-Hard Problem(二分图染色)
来源:互联网 发布:基础软件服务经营范围 编辑:程序博客网 时间:2024/06/06 14:24
题目链接
http://codeforces.com/contest/687/problem/A
思路
只需要将这个图二分染色,然后输出解即可
代码
#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)const int maxn = 100000 + 5;vector<int> G[maxn];int n, m, color[maxn];vector<int> ans1, ans2;bool bicolor(int u) { for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (color[v] == color[u]) return false; if (!color[v]) { color[v] = 3 - color[u]; if (!bicolor(v)) return false; } } return true;}int main() { scan2(n, m); for (int i = 0; i < m; i++) { int x, y; scan2(x, y); G[x].push_back(y); G[y].push_back(x); } bool flag = true; for (int i = 1; i <= n; i++) { if (!color[i]) { color[i] = 1; if (!bicolor(i)) flag = false;; } } if (!flag) printf("-1\n"); else { for (int i = 1; i <= n; i++) { if (color[i] == 1) ans1.push_back(i); if (color[i] == 2) ans2.push_back(i); } printf("%d\n", ans1.size()); for (int i = 0; i < ans1.size(); i++) printf("%d ", ans1[i]); printf("\n%d\n", ans2.size()); for (int i = 0; i < ans2.size(); i++) printf("%d ", ans2[i]); printf("\n"); } return 0;}
0 0
- Codeforces687A - NP-Hard Problem(二分图染色)
- Codeforces687A. NP-Hard Problem 染色判断二分图
- NP-Hard Problem(二分图染色)
- NP-Hard Problem(二分图染色)
- CodeForces 687A - NP-Hard Problem(二分图染色)
- codeforces_688C. NP-Hard Problem(二分图+dfs染色)
- Codeforces-687A NP-Hard Problem(二分图染色)
- Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)
- Codeforces Round #360 (Div. 1) A. NP-Hard Problem(二分图染色)
- Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)
- CodeForces 687A - NP-Hard Problem(二分染色)
- Codeforces Round #360 (Div. 2)——C. NP-Hard Problem(BFS染色判二分图)
- CodeForces 687A NP-Hard Problem(二分图)
- CodeForces 687A NP-Hard Problem(判断二分图)
- CodeForces 688C NP-Hard Problem(二分图判断)
- CF687A NP-Hard Problem(二分图判断)
- NP-Hard Problem codeforces (二分图,dfs)
- cf #360 div2C - NP-Hard Problem(二分图判断+输出)
- 贪心算法-HDU1045-Fire Net
- 反应c语言程序结构特点的程序
- 9.12
- CentOS目录结构
- 相同的功能,不同的代码
- Codeforces687A - NP-Hard Problem(二分图染色)
- TCP协议中的三次握手和四次挥手(图解)
- html提交跳出框架页面
- <poj-2632>Crashing Robots
- html5 net XMLHttpRequest
- 并查集系列(一)——Social network connectivity
- Linux网络通信 -- TCP/IP协议
- Codeforces 401B Sereja and Contests【水题】
- LeetCode 第46,47题(Permutations)Java