CodeForces 687A NP-Hard Problem (二分图)
题意:给定 n 条边,然后让你把它分成两组,每组都有所有边的一个端点。
析:一开始我是先判定环,以为就不能成立,其实不是这样的,有环也行。用dfs进行搜索,并标记每一个端点,如果标记过并且和以前不一样,那么就是不能成立,
否则就能成立,并且标记上。最后分类输出就好。
代码如下:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector> using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
vector<int> G[maxn];
vector<int> a, b;
int vis[maxn];
bool ok = true; void dfs(int val, int i){
if(!vis[i]) vis[i] = val;//没有标记的标记上
else if(vis[i] != val){ ok =false; return ; }//如果标记不一样,那就不能标记
else return ;
for(int j = 0; j < G[i].size(); ++j){
dfs(val == 1 ? 2 : 1, G[i][j]);//搜索
}
} void print(vector<int> &x, vector<int> &y){//输出
printf("%d\n", x.size());
for(int i = 0; i < x.size(); ++i) if(!i) printf("%d", x[i]);
else printf(" %d", x[i]);
printf("\n%d\n", y.size());
for(int i = 0; i < y.size(); ++i) if(!i) printf("%d", y[i]);
else printf(" %d", y[i]);
printf("\n");
} int main(){
int n, m;
scanf("%d %d", &n, &m);
memset(vis, 0, sizeof(vis));
int u, v;
for(int i = 0; i < m; ++i){
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
} for(int i = 1; i <= n; ++i)
if(!vis[i] && !G[i].empty()) dfs(1, i);
for(int i = 1; i <= n; ++i) if(vis[i] == 1) a.push_back(i);
else if(vis[i] == 2) b.push_back(i);
if(ok) print(a, b); else printf("-1\n");
return 0;
}
CodeForces 687A NP-Hard Problem (二分图)的更多相关文章
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- Codeforces 442B Andrey and Problem(贪婪)
题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...
- CodeForces 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- Codeforces 776D The Door Problem
题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
- codeforces 803G Periodic RMQ Problem
codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...
- [Codeforces 986E] Prince's Problem
[题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...
- 【codeforces 527D】Clique Problem
[题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...
- 【codeforces 793C】Mice problem
[题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
- 【codeforces 807D】Dynamic Problem Scoring
[题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...
随机推荐
- const_cast
函数原型: const_cast < type-id > ( expression ) 去掉const属性:const_cast<int*> (&num),常用,因为不 ...
- postgresql双机热备、高可用方案(采用pacemaker+corosync实现)
http://blog.csdn.net/qguanri/article/details/51151974 需求描述 我们有两台centos7的数据库主机A.B.要对A.B实现双机热备,A作为数据库m ...
- Application共享数据
1.Application与Session的区别 Application对象:实现程序级别的数据共享. Session对象:实现会话级别的数据共享. 当需要整个程序级别的共享信息时,可以使用Appli ...
- 修改panabit web管理介面端口
panabit使用mini_httpd为web发布平台,版本为1.19.使用https协议发布,端口443,运行命令为/usr/panabit/bin/ipe_httpd. panabit启动时使用/ ...
- 设置pip镜像源
修改镜像源 vim ~/.pip/pip.conf windows 当前用户目录\pip\pip.ini 内容 [global] index-url=https://pypi.tuna.tsinghu ...
- FastDFS简介和安装
FastDFS是一个轻量级的开源分布式文件系统 FastDFS主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡 FastDFS实现了软件方式的RAID,可以使用廉价的IDE硬盘进 ...
- 学习笔记之LeetCode
LeetCode Online Judge https://leetcode.com/ Leetcode:在线编程网站-各大IT公司的笔试面试题 http://blog.csdn.net/huixin ...
- TCL数组
数组是一组使用索引对应元素的排列方式.常规数组的语法如下所示. set ArrayName(Index) value 用于创建简单数组的例子,如下所示. #!/usr/bin/tclsh set la ...
- win2008以上的系统,在vmware esxi5.5里怎么使用自定义规范管理器?sysprep
经过测试,原来08以上的系统自带了sysprep.exe,所以vcenter对08以上的系统直接使用自定义规范管理器即可,跟linux一样了.注意不要跟03一样写入了sn即可. vCenter可使用s ...
- CFGym 101194L 题解
一.题目链接 http://codeforces.com/gym/101194/problem/L 二.题意 有4个队伍,要打6场比赛(刚好每两个队伍都能相互比一次),若A和B比赛有3种结果: A赢B ...