codeforces 687A
来源:互联网 发布:java单元测试testng 编辑:程序博客网 时间:2024/04/29 07:27
题意: n个点m个边 ,要求将点分为两组,每组含每条边的一个点。 单独的点可以分到任何一组。
思路: 奇偶染色。
#include<bits/stdc++.h>using namespace std;const int maxn = 100010;int col[maxn];vector<int>v[maxn];bool dfs( int u ,int color ){ col[u] = color; for(int i = 0; i<v[u].size(); i++) { int t = v[u][i]; if(col[t]==color) { return false; } else if(col[t]==0) { if(!dfs(t,-color)) return false; } } return true;}int main(){ memset(col,0,sizeof(col)); int n ,m; scanf("%d%d",&n,&m); for(int i = 1; i<=m; i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } int flag = true; for(int i = 1; i<=n; i++) { if(col[i]==0) { if(!dfs(i,1)) { flag = false; break; } } } if(!flag) cout<<"-1"<<endl; else { int cot = 0; for(int i = 1; i<=n; i++) { if(col[i]==1) cot++; } printf("%d\n",cot); for(int i = 1;i<=n;i++) { if(col[i]==1) printf("%d ",i); } printf("\n"); printf("%d\n",n-cot); for(int i = 1;i<=n;i++) { if(col[i]==-1) { printf("%d ",i); } } cout<<endl; } return 0;}
0 0
- codeforces 687A
- CodeForces 687A
- CodeForces 687A
- CodeForces-a
- Codeforces 903A A
- Codeforces-687A NP-Hard Problem
- CodeForces 687A--判断二分图
- 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 - 687A (连通图染色)
- CodeForces 18A A - Triangle
- CodeForces 133A A. HQ9+
- codeforces 651A A. Joysticks
- 【Codeforces 597A】A. Divisibility
- 【 Codeforces 615A】A. Bulbs
- jqgrid设置取消选择
- Android中 PopupWindow 与 ListView 结合使用
- 如何将Eclipse中的项目迁移到Android Studio 中
- 手机游戏资源 特效 显存分析工具
- mongodb c++driver连接数据错误
- codeforces 687A
- 《了凡四训(中华人生智慧经典)》读后感
- DBMS_METADATA查表结构
- 惠普暗影精灵笔记本如何从U盘启动
- 计算机网络——基于socket的网络应用
- 手机趋势
- iOS webView预加载
- [eclipse]Syntax error on token ";",{ expected after this token
- 剑指offer-3-面试15:链表中倒数第k个结点