Codeforces Round #360 (Div. 1)A - NP-Hard Problem
来源:互联网 发布:javascript特效实例 编辑:程序博客网 时间:2024/05/16 08:41
链接:http://codeforces.com/contest/687/problem/A
题意:给定一个n个点的图,要求将所有的点分成2部分使得每一天边连着的两个点都不在同一部分中,无解输出-1。
分析:直接dfs01染色就是了。
代码:
#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<cstdio>#include<vector>#include<string>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=100010;const int MAX=1000000100;const int mod=100000000;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=998244353;const int INF=1000000010;const double pi=acos(-1.0);typedef double db;typedef unsigned long long ull;int tot,u[N],v[2*N],pre[2*N];void add(int x,int y) { v[tot]=y;pre[tot]=u[x];u[x]=tot++;}int bo=1,q[N];void dfs(int x,int y) { y==1 ? q[x]=2:q[x]=1; for (int i=u[x];i!=-1;i=pre[i]) if (!q[v[i]]) dfs(v[i],q[x]); else if (q[v[i]]==q[x]) bo=0;}int main(){ int i,n,m,x,y; scanf("%d%d", &n, &m); tot=0;memset(u,-1,sizeof(u)); for (i=1;i<=m;i++) { scanf("%d%d", &x, &y); add(x,y);add(y,x); } memset(q,0,sizeof(q)); for (i=1;i<=n;i++) if (!q[i]) dfs(i,2); if (!bo) printf("-1\n"); else { m=0; for (i=1;i<=n;i++) if (q[i]==1) m++; printf("%d\n", m); for (i=1;i<=n;i++) if (q[i]==1) printf("%d ", i); printf("\n"); printf("%d\n", n-m); for (i=1;i<=n;i++) if (q[i]==2) printf("%d ", i); printf("\n"); } return 0;}
0 0
- Codeforces Round #360 (Div. 1)A - NP-Hard Problem
- Codeforces Round #360 (Div. 1) A. NP-Hard Problem(二分图染色)
- Codeforces Round #360 (Div. 1) A. NP-Hard Problem (二分图)
- Codeforces Round #360 (Div. 2) C. NP-Hard Problem
- Codeforces Round #360 (Div. 2) C. NP-Hard Problem
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
- Codeforces Round #360 (Div. 2) C NP-Hard Problem(二分图)
- Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)
- Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)
- Codeforces-687A NP-Hard Problem
- Codeforces 687A NP-Hard Problem
- CodeForces 687A NP-Hard Problem
- codeforces 687A - NP-Hard Problem(dfs
- CodeForces 687A NP-Hard Problem 搜索
- codeforces 687A NP-Hard Problem
- Codeforces Round #360 (Div. 2)——C. NP-Hard Problem(BFS染色判二分图)
- A. NP-Hard Problem
- CodeForces 687A - NP-Hard Problem(二分图染色)
- Cisco InterCloud
- YAML 语言教程
- 371. Sum of Two Integers
- java内部类总结
- 三维重建(二)Sift特征提取与匹配
- Codeforces Round #360 (Div. 1)A - NP-Hard Problem
- Android之SD卡简单的文件读写操作
- leetcode:Water and Jug Problem
- 2016sdau课程练习专题三 1008
- XMPP 学习笔记一 OpenFire与Spark安装
- PCH文件自定义NSLog及存在C文件时PCH文件定义注意事项
- 290. Word Pattern
- Android之SD卡以追加形式写文件并把文件保存到SD卡中
- centos搭建rsync远程同步服务器