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 ...
随机推荐
- DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_memory=False.
DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_ ...
- Spark SQL中UDF和UDAF
转载自:https://blog.csdn.net/u012297062/article/details/52227909 UDF: User Defined Function,用户自定义的函数,函数 ...
- 详解Spark sql用户自定义函数:UDF与UDAF
UDAF = USER DEFINED AGGREGATION FUNCTION Spark sql提供了丰富的内置函数供猿友们使用,辣为何还要用户自定义函数呢?实际的业务场景可能很复杂,内置函数ho ...
- Storm程序永久代内存溢出
在集群中部署Storm应用程序的时候报错,并通过jdk自带的jconsole监控发现,永久代内存瞬间爆炸了 org.springframework.beans.factory.BeanCreation ...
- 爬虫——请求库之requests
阅读目录 一 介绍 二 基于GET请求 三 基于POST请求 四 响应Response 五 高级用法 一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,reque ...
- mariadb多源复制 muiltil source replication
环境:centos-6.5 Mariadb:10.1.13-MariaDB 多源复制:企业数据库中写的需求较大,以至于I/O负载比较大,那么就必须把写的操作分摊到多台主服务器上进行,然后在将 ...
- springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean
在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本 3.1.7 后更新为 ...
- Linux系统——sed命令
sed命令精讲 cat工作原理 cat命令只想文件,把文件打开后,将文件中所有内容一次性读到内存中,从内存里一次性输出到屏幕上,此时可能存在内存装不下的情况,因此cat命令只能查看小文件内容,不能读取 ...
- JDK环境变量配置目录jre,jvm
类路径 :CLASSPATH= .;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar JDK的路径:JAVA_HOME = C:/Program F ...
- XDU 1140 寻找万神(字符串匹配)
学会strstr的使用 strstr(str1,str2)函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. #include ...