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 ...
随机推荐
- 棋盘格 测量 相机近似精度 (像素精度&物理精度)
像素精度计算 像素精度——一像素对应多少毫米——距离不同像素精度也不同 将棋盘格与相机CCD平面大致平行摆放,通过[每个点处的近似像素精度=相邻两个角点之间的实际距离(棋盘格尺寸已知)/ 棋盘格上检出 ...
- 18.解决合并androidmanfest错误
这个是minsdk和tartgetsdk的版本不一致的问题
- python学习笔记(二十六)经典类和新式类的区别
首先了解一下什么是经典类,什么是新式类 class Person: #经典类 pass class Person2(object): #新式类 pass #类名首字母都大写 #在python3里面经典 ...
- python学习笔记(三)函数
一.定义函数 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参 ...
- Using RUNDLL32.exe to call a function within a dll
Using RUNDLL32.exe to call a function within a dll Rundll32 is a utility included with Window ...
- Html.DropDownListFor的用法总结
在ASP.NET MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值. 注:重点是要将DropDownList的数据源转换成IEnumerable<SelectL ...
- 1-CommonJs
诞生背景JS没有模块系统.标准库较少.缺乏包管理工具:前端端没有模块化编程还可以,因为前端逻辑没那么复杂,可以工作下去,在服务器端逻辑性那么强必须要有模块为了让JS可以在任何地方运行,以达到Java. ...
- Linux Swap交换分区介绍
Swap分区在系统的物理内存不够用的时候,把物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap分区中, ...
- Spring Tomcat启动过程
入口,配置再web.xml中, 初始化applicationContext 创建webApplicationContext,这里可加载web.xml文件中定义了contextClass 获取conte ...
- Clonal hematopoiesis of indeterminate potential(意义不明的克隆性造血)-CHIP
意义不明的克隆性造血是指由一个造血干细胞或者其他早期的起始血细胞为了更好的适应环境而发展成一个带有一些基因变异的亚型. 这个亚型带有基因变异一般是非驱动性的,而且该亚型占血细胞的比率跟年龄有很大的相关 ...