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表示; 或者给出解题 ...
随机推荐
- centos中如何安装php-bcmath扩展?
talk is cheap,show me the code: [root@LAMP1 lib]# php -v PHP (cli) (built: Oct ::) Copyright (c) - T ...
- ffmpeg C++程序编译时报__cxa_end_catch错误
解决方法在编译sh中加上 -lsupc++ 即可. 2.STL模块函数找不到,链接失败stdc++/include/bits/stl_list.h:466: error: undefined refe ...
- RK3288 device descriptor read/64, error -32
CPU:RK3288 系统:Android 5.1 主板有两个USB接口,一个接USB摄像头,一个接身份证模块. 插入摄像头可以正常打开,再插入身份证模块时,摄像头就会卡主,而且身份证模块无法识别,内 ...
- (转)html中使用表单和input file上传图片
本文转载自:http://hi.baidu.com/love_1210/item/120e452b42b2a854c38d59eb 客户端代码: <form name="form1&q ...
- nginx 点播mp4方法
1.配置文件 配置文件中rmtp部分: application vod { play html; } 配置文件中http部分: location ~ .mp4$ { root html; mp4; l ...
- [html][javascript]动态增删页面元素
<script type="text/javascript"> function append(event){ var myhref = document.create ...
- [html]window.open 使用示例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 1 预备知识--Hadoop简介
1 预备知识--Hadoop简介 Hadoop是Apache的一个开源的分布式计算平台,以HDFS分布式文件系统和MapReduce分布式计算框架为核心,为用户提供了一套底层透明的分布式基础设施Had ...
- varnish/squid/nginx cache 有什么不同?
SQUID 是功能最全面的,但是架构太老,性能不咋的Varnish 是内存缓存,速度一流,但是内存缓存也限制了其容量,缓存页面和图片一般是挺好的Nginx 本来是反向代理/web服务器,用了插件可以做 ...
- RPM包下载网址
https://pkgs.org/ (最爱) http://rpm.pbone.net/ http://rpmfind.net/linux/RPM/index.html