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表示; 或者给出解题 ...
随机推荐
- 解决crontab不加载环境变量问题
公司需要做异构库数据同步,由于之前实际使用过,且字段类型也兼容,满足业务场景,使用了阿里开源数据同步工具:datax,服务器上crontab定时脚本执行. 由于crontab只加载/ect/envir ...
- FastAdmin Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考)
Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考) 昨天群里有小伙伴询问 Bootstrap-Table 有没有计算所有页的总数. [吐槽]★隔壁老王-杭州 @F4NN ...
- Firefox渗透插件—Web渗透必备工具
1:Firebug Firefox的 五星级强力推荐插件之一,不许要多解释 2:User Agent Switcher 改变客户端的User Agent的一款插件 3:Hackbar 攻城师们的必备工 ...
- 基于ffmpeg静态库的应用开发
最近几天在试着做基本ffmpeg静态库的开发,只有main中包含了avdevice_register_all 或avfilter_register_all,编译就通不过,undefined refre ...
- IEEE 754 浮点数在计算机中的表示方法
IEEE二进制浮点数算术标准(IEEE 754)是20世纪80年代以来最广泛使用的浮点数运算标准,为许多CPU与浮点运算器所采用.这个标准定义了表示浮点数的格式(包括负零-0)与反常值(denorma ...
- VirtualBox的端口映射其实很好理解
还是和以前百度的另一个知识点一样,我真不明白网上那些人要做什么,明明很简单的事,干嘛非要讲的那么复杂,就是为了让人觉得你很高手?很厉害? 名称:随便起的,基于好记的原则,你的什么应用在使用这一条端口转 ...
- [教程]Ubuntu下完整配置自动壁纸切换
来源:http://www.linuxidc.com/Linux/2016-09/135350.html 更原始的来源就不去找了,如果有找到的同学可以回复我我到时改过来www 来源的教程是有效的,但是 ...
- Oracle DSI系列 01 DSI初识BBED
DSI是Data Server Internals的缩写,是Oracle公司内部用来培训Oracle售后工程师使用的教材. 1 bbed工具使用BBED工具介绍BBED stands for Bloc ...
- Hive 查看日志
日志记录了程序运行的过程,是一种查找问题的利器. Hive中的日志分为两种1. 系统日志,记录了hive的运行情况,错误状况.2. Job 日志,记录了Hive 中job的执行的历史过程. 系统日志存 ...
- zoj-3410-Layton's Escape
/* ZOJ Problem Set - 3410Layton's Escape ----------------------------------------------------------- ...