再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理。

然后跑强连通分量分解,保证a和非a不在同一个分量里面。

这题由于你建完图发现都是双向边,所以用并查集亦可。

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int>G[200010],rG[200010],vs;
bool used[200010],a[100010];
int n,m,cmp[200010],bel[100010][3];
void dfs(int U)
{
used[U]=1;
for(int i=0;i<G[U].size();++i)
if(!used[G[U][i]])
dfs(G[U][i]);
vs.push_back(U);
}
void rdfs(int U,int k)
{
used[U]=1;
cmp[U]=k;
for(int i=0;i<rG[U].size();++i)
if(!used[rG[U][i]])
rdfs(rG[U][i],k);
}
int main()
{
// freopen("d.in","r",stdin);
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
for(int i=1;i<=m;++i)
{
scanf("%d",&x);
for(int j=1;j<=x;++j)
{
scanf("%d",&y);
bel[y][++bel[y][0]]=i;
}
}
for(int i=1;i<=n;++i)
if(a[i])
{
G[bel[i][1]].push_back(bel[i][2]);
G[bel[i][2]].push_back(bel[i][1]);
G[bel[i][1]+m].push_back(bel[i][2]+m);
G[bel[i][2]+m].push_back(bel[i][1]+m); rG[bel[i][2]].push_back(bel[i][1]);
rG[bel[i][1]].push_back(bel[i][2]);
rG[bel[i][2]+m].push_back(bel[i][1]+m);
rG[bel[i][1]+m].push_back(bel[i][2]+m);
}
else
{
G[bel[i][1]].push_back(bel[i][2]+n);
G[bel[i][2]].push_back(bel[i][1]+n);
G[bel[i][1]+m].push_back(bel[i][2]);
G[bel[i][2]+m].push_back(bel[i][1]); rG[bel[i][2]+m].push_back(bel[i][1]);
rG[bel[i][1]+m].push_back(bel[i][2]);
rG[bel[i][2]].push_back(bel[i][1]+m);
rG[bel[i][1]].push_back(bel[i][2]+m);
}
for(int i=1;i<=m;++i)
if(!used[i])
dfs(i);
memset(used,0,sizeof(used));
int cnt=0;
for(int i=vs.size()-1;i>=0;--i)
if(!used[vs[i]])
rdfs(vs[i],++cnt);
for(int i=1;i<=m;++i)
if(cmp[i]==cmp[i+m])
{
puts("NO");
return 0;
}
puts("YES");
return 0;
}

【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem的更多相关文章

  1. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

    前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...

  2. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D

    Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlo ...

  4. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A

    Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each ...

  5. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT

    题目链接:http://codeforces.com/contest/776/problem/D D. The Door Problem time limit per test 2 seconds m ...

  6. 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...

  7. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C

    Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...

  8. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...

随机推荐

  1. ext radiogroup如何取值和设值

    var radios = Ext.create('Ext.form.Panel', { title: 'RadioGroup Example', width: 300, height: 125, bo ...

  2. Spring源码解析-AutowiredAnnotationBeanPostProcessor

    1.实现了BeanPostProcessor接口,可先看这个接口 ApplicationContext可以在自动检测BeanPostProcessor bean,在它创建完后可以创建任何的bean. ...

  3. 马上给Meltdown和Spectre漏洞打补丁

    元旦之后的第一个工作日可谓是惊喜不断,4号就传来了 Google Project Zero 等团队和个人报告的 Meltdown 和 Spectre 内核漏洞的消息,首先简单介绍一下这两个内核漏洞. ...

  4. 深入探索 高效的Java异常处理框架

    转载自:http://www.sunwei.org/archives/196 摘要:本文从Java异常最基本的概念.语法开始讲述了Java异常处理的基本知识,分析了Java异常体系结构,对比Sprin ...

  5. MySQL rpm 版本安装

     准备: [root@localhost moudles]# ls MySQL-client-5.6.36-1.linux_glibc2.5.x86_64.rpm MySQL-server-5.6.3 ...

  6. linux基础(2)

    Linux基础题 作业一:1) 新建用户natasha,uid为1000,gid为555,备注信息为“master”useradd natashagroupmod -g 555 natashauser ...

  7. HASHMAP原理解析,不错的文章

    http://blog.csdn.net/vking_wang/article/details/14166593

  8. 【BZOJ1101】Zap [莫比乌斯反演]

    Zap Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 对于给定的整数a,b和d,有多少正整 ...

  9. Maven介绍---POM、Dependency Management、Coordinates

    Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建.报告和文档的软件项目管理工具. POM(Project Object Model,对象模型): 仅仅只是一个xml配置文 ...

  10. mybatis generator 生成javabean自定义类型转换

    因为默认mybatis generator自动生成的,带小数的都转成了bigdecimal了,而且长度不同的整数转成了不同的类型. 但是我想要带小数的转成double,整数转成integer. 所有自 ...