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 ...
随机推荐
- matrix---简单dp,边界边界-_-
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5569 简单dp,恶心的边界处理,无语: if((i+j)%2==1) dp[i][j]=a[i-1][ ...
- 常用WebSite Address
百度 魅族深度学习应用大赛 byr论坛 帖子 https://bbs.byr.cn/#!article/ML_DM/24852?p=1 百度 魅族深度学习应用大赛 官网 http://meizu.ba ...
- 在android上跑 keras 或 tensorflow 模型
https://groups.google.com/forum/#!topic/keras-users/Yob7mIDmTFs http://talc1.loria.fr/users/cerisara ...
- tkprof参数详解
tkprof参数详解 table=schema.table 指定tkprof处理sql trace文件时临时表的模式名和表名 insert=scriptfile 创建一个文件名为scriptfile的 ...
- git命令(待补充)
git log查看历史 git log -p -2 -p选项表示显示每次提交的内容差异,-2表示最近两次的更新
- org.apache.ibatis.binding.BindingException: Mapper method 'attempted to return null from a method with a primitive return type (long).
一.问题描述 今天发现测试环境报出来一个数据库相关的错误 org.apache.ibatis.binding.BindingException: Mapper method 'attempted to ...
- iClap的名字是怎么来的,clap是有什么特殊的意义么?
iClap的名字来源于:Clap中文是鼓掌的意思,鼓掌代表合拍,一个团队的价值观以及工作方式合拍,是最重要的,当项目启动时,大家对产品认可,鼓掌开始实施:当项目成功上线,团队也会以鼓掌的形式庆祝:当我 ...
- How can For each...
Answer: I understand the IEnumerator/IEnumerable methods and properties and also how they are inte ...
- netbeans许可证模板设置
工具->模板->设置 user=your name 工具->模板->许可证->默认许可证 <#if licenseFirst??>${licenseFirst ...
- vue package.json 解析
vue package.json 中可以看到这个包的来源位置 在repository对象中的url中可以看到 所以当你要封装自己包的时候就可以改变这个地址去加载这个包(把你封装的包发到某个gi ...