codeforces776D
这题的意思就是原本有一个长度为n的01串,再给出m的长度为n的01串,要求你判定是否可以通过原串与m个串中的某些串xor使得原串到达一个状态。n,m小于1e5。
这题最初我发现不可做,因为这貌似是个NPC问题/密码加密方法之一,只能暴力。
后来我发现我忽略了一个条件:n个01位中,m个串中只有两个串的位置为1,这就很资磁了,若选此串,必选彼串,这种限制条件就是2-SAT的经典应用,直接上2-SAT即可。
考完发现了神犇都是用二分图染色的变种做的,仔细想想,2-SAT只是将染色方案拆开看了,实质不变。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define LL long long
#define FILE "dealing"
#define up(i,j,n) for(int i=j;i<=n;i++)
int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
const int maxn=1600000,inf=1000000000;
bool cmin(int& a,int b){return a>b?a=b,true:false;}
bool cmax(int& a,int b){return a<b?a=b,true:false;}
int n,cnt[maxn];
struct node{int y,next;}e[maxn];int len=0,linkk[maxn];
void insert(int x,int y){e[++len].y=y;e[len].next=linkk[x];linkk[x]=len;}
int b[maxn][2],a[maxn],d[maxn];
bool c[maxn];bool flag=0;int q[maxn],top=0;
void dfs(int x){
if(((x%2)&&c[x+1])||(!(x%2)&&c[x-1])){flag=1;return;}
c[x]=1;q[++top]=x;
for(int i=linkk[x];i;i=e[i].next){
if(!c[e[i].y])dfs(e[i].y);
}
}
int m;
int solve(){
for(int i=1;i<=m<<1;i+=2){
if(!c[i]&&!c[i+1]){
top=0;
dfs(i);
if(flag){
while(top)c[q[top--]]=0;
flag=0;
dfs(i+1);
if(flag)return 1;
}
}
}
return 0;
}
int main(){
//freopen(FILE".in","r",stdin);
// freopen(FILE".out","w",stdout);
n=read();m=read();
up(i,1,n)a[i]=read();
up(i,1,m){
cnt[i]=read();
up(j,1,cnt[i]){
int y=read();
if(!b[y][0])b[y][0]=i;
else b[y][1]=i;
}
}
up(i,1,n){
if(a[i])insert(b[i][0]<<1,b[i][1]<<1),insert(b[i][1]<<1,b[i][0]<<1),insert((b[i][0]<<1)-1,(b[i][1]<<1)-1),insert((b[i][1]<<1)-1,(b[i][0]<<1)-1);
else insert((b[i][0]<<1)-1,(b[i][1]<<1)),insert((b[i][1]<<1)-1,(b[i][0]<<1)),insert((b[i][0]<<1),(b[i][1]<<1)-1),insert(b[i][1]<<1,(b[i][0]<<1)-1);
}
if(solve())printf("NO\n");
else printf("YES\n");
return 0;
}
codeforces776D的更多相关文章
- codeforces776D The Door Problem
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- 输入框为数字类型时防止maxlength属性不起作用
<input type="number" oninput="if(value.length>5)value=value.slice(0,5)" /& ...
- IntelliJ IDEA导入包的顺序调整和按包类型分类(保持和Eclipse一致)
调整的内容如下: 空行 import java.* 空行 import javax.* 空行 import com.* 空行 import all other imports 空行 import st ...
- Java获取当前时间戳/时间戳转换
时间戳精度有两个概念:1是精确到秒,2是精确到毫秒. 要操作时间戳和时间戳转换为时间一般对应的对象就是Date,而Date各种转换离不开SimpleDateFormat: 如果是要获取时间指定的年月日 ...
- P2P技术简介(包括BT软件的分析)(转)
这是一篇别人发表的论文,里面很全面的解释了P2P技术的实现,以及BT网络中应用P2P技术所设计的原理,并列举BT软件的一些专业名词的定义.由于论文发表的比较早,2005年时还没有DHT技术. (链接: ...
- 使用UltraISO为U盘或内存卡制作系统安装工具
此软件可以为U盘制作Windows安装版的启动工具,也可以为Linux制作启动工具,尤其是Ubuntu这些.提示:推荐购买一些读取速度快一些的U盘,运行起来可以节省很多时间. 如果是内存卡要实现此功能 ...
- MySQL 服务无法启动-问题处理
症状:前一天在MySQL中删除了几个不用的数据库后登陆MySQL出现以下错误: mysql -u root -p passwd ERROR 2002 (HY000): Can't connec ...
- 获取path的几种方式:NSFileManager NSHomeDirectory NSBundle
//---------------------------------------------------------------------------------NSFileManager *fi ...
- 【Jpa hibernate】一对多@OneToMany,多对一@ManyToOne的使用
项目中使用实体之间存在一对多@OneToMany,多对一@ManyToOne的映射关系,怎么设置呢? GitHub地址:https://github.com/AngelSXD/myagenorderd ...
- 调用tf.softmax_cross_entropy_with_logits函数出错解决
原来这个函数,不能按以前的方式进行调用了,只能使用命名参数的方式来调用.原来是这样的: tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y ...
- linux查看 cpu及内存和硬盘使用情况的命令top
使用时输入 top,退出时输入q http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的 ...