codeforces776D The Door Problem
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:CF776D
正解:$2-SAT$
解题报告:
似乎以前做过类似的题啊,不过因为没有正好$2$个这个限制所以并不好做。
考虑题目相当于是问是否存在使得每个点都变成$0$的的方案,一次操作我们可以看成是一次异或。
因为有一个强有力的限制条件,我们就可以直接往$2-SAT$模型上靠了。
当$r[i]$初值为$1$,那么需要$0$次或$2$次操作;否则需要恰好一次操作。
那么这个题的$2-SAT$模型就是控制每扇门的钥匙是否选择的异或值恰好等于$r[i]$,直接建图跑$2-SAT$就好了。
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long LL;
const int MAXN = 200011;
const int MAXM = 2000011;
int n,m,X[MAXN];
vector<int>w[MAXN];
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} namespace SAT_2{
int ecnt,first[MAXN],to[MAXM],next[MAXM],top,stack[MAXN],bel[MAXN],scnt,dfn[MAXN],low[MAXN];
bool pd[MAXN];
inline void link(int x,int y){
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
}
inline void tarjan(int x){
dfn[x]=low[x]=++ecnt;
pd[x]=1; stack[++top]=x;
for(int i=first[x];i;i=next[i]) {
int v=to[i];
if(!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(pd[v]) low[x]=min(low[x],low[v]);
}
if(dfn[x]==low[x]) {
scnt++;
while(stack[top]!=x) {
bel[stack[top]]=x; pd[x]=0;
top--;
}
pd[x]=0; bel[x]=scnt;
top--;
}
} inline void work(){
ecnt=0;
for(int i=2;i<=(m<<1)+1;i++) if(!dfn[i]) tarjan(i);
for(int i=1;i<=m;i++) if(bel[i<<1]==bel[i<<1|1]) { puts("NO"); return ; }
puts("YES");
}
} inline void work(){
using namespace SAT_2;
n=getint(); m=getint(); int x,y;
for(int i=1;i<=n;i++) X[i]=getint();
for(int i=1;i<=m;i++) {
x=getint();
while(x--) {
y=getint();
w[y].push_back(i);
}
} for(int i=1;i<=n;i++) {
x=w[i][0]; y=w[i][1];
x<<=1; y<<=1;
if(X[i]==0) {
link(x,y|1); link(y|1,x);
link(x|1,y); link(y,x|1);
}
else {
link(x,y); link(y,x);
link(x|1,y|1); link(y|1,x|1);
}
} SAT_2::work();
} int main()
{
#ifndef ONLINE_JUDGE
freopen("776.in","r",stdin);
freopen("776.out","w",stdout);
#endif
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
codeforces776D The Door Problem的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- ubuntu安装mysql步骤
https://dev.mysql.com/downloads/file/?id=477124 ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get inst ...
- 使用JCONSOLE远程监控JVM
启动JMS服务 JConsole是从Java 5中开始引入的一个用于对JVM性能和资源消耗进行监控的图形化工具.JConsole可以连接本地的Java程序,也可以连接远程的Java程序.由于是GUI的 ...
- SKBUFFER详解
纯属转载,不敢侵犯别人产权!! 一. SKB_BUFF的基本概念1. 一个完整的skb buff组成(1) struct sk_buff--用于维护socket buffer状态和描述信息(2) he ...
- Python并行编程(一):基本概念
1.线程和进程 进程是应用程序的一个执行实例,比如,在桌面上双击浏览器将会运行一个浏览器.线程是一个控制流程,可以在进程内与其他活跃的线程同时执行.控制流程指的是顺序执行一些机器指令.进程可以包含多个 ...
- MySQL优化(三):优化数据库对象
二.优化数据库对象 1.优化表的数据类型 应用设计的时候需要考虑字段的长度留有一定的冗余,但不推荐很多字段都留有大量的冗余,这样既浪费磁盘空间,也在应用操作时浪费物理内存. 在MySQL中,可以使用函 ...
- Python垃圾回收机制详解转自--Kevin Lu
一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题. 在Python中,如果一个对象的引用数为0,Python虚拟机就会回收这个对象的内存. #e ...
- HI35xx平台调试笔记
1.音视频数据循环采集 a. 在sample_venc.c文件中,海思官方是把采集到的数据都保存到文件中,我们需要更改到缓存里,以便后面推送到rtsp/rtmp/hls服务端. ; i < s3 ...
- POJ1459:Power Network(dinic)
题目链接:http://poj.org/problem?id=1459 题意:有n个结点,np个发电站,nc个消费者,m个电力运输线.接下去是m条边的信息(u,v)cost,cost表示边(u,v)的 ...
- FTP服务器文件上传的代码实现
方式一: @Test public void testFtpClient() throws Exception { // 1.创建一个FtpClient对象 FTPClient ftpClient = ...
- jmeter接口测试实战
请求方法:get/post 接口请求地址:http://172.22.24.26:8080/fundhouse/external/getdata?name=xxxx &fund_udid=35 ...