【HDU】1814 Peaceful Commission
http://acm.hdu.edu.cn/showproblem.php?pid=1814
题意:n个2人组,编号分别为2n和2n+1,每个组选一个人出来,且给出m条关系(x,y)使得选了x就不能选y,问是否能从每个组选出1人。且输出字典序最小的答案。(n<=8000, m<=20000)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const int N=8005, M=20005;
struct E { int next, to; }e[M<<1];
int cnt, ihead[N<<1], top, s[N<<1], n, m;
bool vis[N<<1];
void add(int u, int v) { e[++cnt]=(E){ihead[u], v}; ihead[u]=cnt; }
bool dfs(int x) {
if(vis[x^1]) return 0;
if(vis[x]) return 1;
vis[x]=1;
s[++top]=x;
for(int i=ihead[x]; i; i=e[i].next) if(!dfs(e[i].to)) return 0;
return 1;
}
bool work() {
int nn=n<<1;
for(int i=0; i<nn; i+=2) if(!vis[i] && !vis[i+1]) {
top=0;
if(!dfs(i)) {
while(top) vis[s[top--]]=0;
if(!dfs(i+1)) return 0;
}
}
for(int i=0; i<nn; ++i) if(vis[i]) printf("%d\n", i+1);
return 1;
}
int main() {
while(~scanf("%d%d", &n, &m)) {
for(int i=0; i<m; ++i) { int x, y; scanf("%d%d", &x, &y); --x; --y; add(x, y^1); add(y, x^1); }
if(!work()) puts("NIE");
cnt=top=0;
memset(vis, 0, sizeof(bool)*(n<<1));
memset(ihead, 0, sizeof(int)*(n<<1));
}
return 0;
}
本题很显然的2-sat问题= =
对于关系(x,y)实际上就是满足!(x & y)
即当x=1时y必须为0,即连边x->y'
当y=1时x必须为1,即连边y->x'
由于这个dfs的算法本身就是字典序最小了= =直接搞就行了= =
(妈呀难道这个算法是O(nm)的嘛QAQ看来得写tarjan了以后= =(虽然字典序最小只能用这个O(nm)算法= =?)
【HDU】1814 Peaceful Commission的更多相关文章
- 【HDOJ】1814 Peaceful Commission
2-SAT基础题目. /* 1814 */ #include <iostream> #include <vector> #include <algorithm> # ...
- HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)
HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...
- 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】
传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【HDU】2191 多重背包问题
原题目:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 [算法]多重背包(有限背包) 动态规划 [题解]http://blog.csdn.net/acdreamers/article/detail ...
- HDOJ 1814 Peaceful Commission
经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Mem ...
- 【HDU】6110 路径交(2017百度之星) 线段树+RMQ-LCA+树链的交
[题目]2017"百度之星"程序设计大赛 - 初赛(A) [题意]给定n个点的带边权树,m条编号1~m的路径,Q次询问编号区间[L,R]所有链的交集的长度.n<=500000 ...
- 【HDU】6148 Valley Numer 数位DP
[算法]数位DP [题意]定义V-number为从左到看单位数字未出现先递增后递减现象的数字,求0~N中满足条件的数字个数.T<=200,lenth(n)<=100 [题解]百度之星201 ...
- 【HDU】5269 ZYB loves Xor I
[算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...
随机推荐
- 在ubuntu上搭建开发环境2---Win7、Ubuntu双系统正确删除Ubuntu
这种删除ubuntu的方式就是针对本博客中搭建双系统的方式:http://www.cnblogs.com/xumenger/p/4459963.html 如果ubuntu不是以wubi方式安装的,那么 ...
- Pyqt phonon的使用
本文是用Pyqt实现了下网上一个Qt版大牛关于phonon的介绍 Qt phonon地址:http://wenku.baidu.com/link?url=nH_dZ8lZbXHy8N5__8jAWLX ...
- [LeetCode] Happy Number
Happy Number Total Accepted: 35195 Total Submissions: 106936 Difficulty: Easy Write an algorithm to ...
- [LeetCode] Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- Installing MySQL Server on CentOS
MySQL is an open-source relational database. For those unfamiliar with these terms, a database is wh ...
- Linux环境下使用perl编写CGI(httpd)
例子1: /var/www/cgi-bin/hello.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print ...
- [Eclipse] Eclipse字体问题解决
背景: Eclipse的字体总感觉有点问题,其中中文字体太小,不方便查看,今天网上搜索了一下,解决了问题,记录下来: 解决办法: Window --> Preferences --> Ge ...
- CG&CAD resource
Computational Geometry The Geometry Center (UIUC) Computational Geometry Pages (UIUC) Geometry in Ac ...
- [荐]使用Js操作注册表
使用Js操作注册表 要操作注册表需要通过ActiveX控件调用WScript.shell对象,通过该对象的一些方法来操作. WshShell对象:可以在本地运行程序.操纵注册表内容.创建快捷方式或访问 ...
- Java学习笔记(一)——HelloWorld
一.安装JDK 1.下载链接: http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.直接安装,不能有中文路径 3. ...