CF687A NP-Hard Problem(二分图判断)
来源:互联网 发布:oecd数据公布 编辑:程序博客网 时间:2024/06/06 04:47
显然是二分图即可。两人一人拿一种颜色的顶点即可。否则一定不可。
#include <bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3f#define ll long long#define N 100010inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,m,h[N],num=0,col[N];vector<int>a[2];struct edge{ int to,next;}data[N<<1];bool dfs(int x){ a[col[x]].push_back(x); for(int i=h[x];i;i=data[i].next){ int y=data[i].to;if(col[y]==col[x]) return 0; if(col[y]!=-1)continue;col[y]=col[x]^1; if(!dfs(y)) return 0; }return 1;}int main(){// freopen("a.in","r",stdin); n=read();m=read(); while(m--){ int x=read(),y=read(); data[++num].to=x;data[num].next=h[y];h[y]=num; data[++num].to=y;data[num].next=h[x];h[x]=num; }memset(col,-1,sizeof(col)); for(int i=1;i<=n;++i) if(col[i]==-1){ col[i]=0;if(!dfs(i)){puts("-1");return 0;} }printf("%d\n%d",a[0].size(),a[0][0]); for(int i=1;i<a[0].size();++i) printf(" %d",a[0][i]);puts(""); printf("%d\n",a[1].size()); for(int i=0;i<a[1].size();++i) printf("%d ",a[1][i]); return 0;}
阅读全文
0 0
- CF687A NP-Hard Problem(二分图判断)
- CodeForces 687A NP-Hard Problem(判断二分图)
- CodeForces 688C NP-Hard Problem(二分图判断)
- NP-Hard Problem(二分图染色)
- cf #360 div2C - NP-Hard Problem(二分图判断+输出)
- Codeforces687A. NP-Hard Problem 染色判断二分图
- CodeForces 687A - NP-Hard Problem(二分图染色)
- CodeForces 687A NP-Hard Problem(二分图)
- codeforces_688C. NP-Hard Problem(二分图+dfs染色)
- Codeforces687A - NP-Hard Problem(二分图染色)
- NP-Hard Problem(二分图染色)
- NP-Hard Problem codeforces (二分图,dfs)
- codeforces C. NP-Hard Problem 二分图的判定
- Codeforces-687A NP-Hard Problem(二分图染色)
- Codeforces 688C 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. 1) A. NP-Hard Problem(二分图染色)
- xml文件引入约束问题
- 在Ubuntu下用APT去安装FLASH插件
- PHP 7 新特性
- 异常
- 百度地图学习总结(1)—前置准备,申请key
- CF687A NP-Hard Problem(二分图判断)
- Linux电源管理(二)系统reboot/shutdown过程
- java后端题目常见
- 元组:戴上了枷锁的列表
- 栈的应用4--中缀表达式转换为后缀表达式
- 网络编程(python)
- apt-get安装软件Unable to locate package错误解决办法
- NYOJ201
- plan(Tag,ToDo/Doing),在路上的思考