【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
再来回顾一下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的更多相关文章
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...
- 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 ...
- 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 ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...
随机推荐
- Educational Codeforces Round 11 A
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- [ST表/贪心] NOI2010 超级钢琴
[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i ...
- 如何让spring源码正常的部署在idea中
我在这里把我从GitHub下载的源码成功编译之后的文件放在了我的百度网盘上大家可以直接下载,也可以按如下步骤自己编译部署到idea中, 下载的地址是:http://pan.baidu.com/s/1d ...
- sysctl -P net.bridge.bridge-nf-call-ip6tables报错解决办法
问题症状 修改 linux 内核文件 #vi /etc/sysctl.conf后执行sysctl -P 报错 error: "net.bridge.bridge-nf-call-ip6ta ...
- nginx对指定目录做代理
环境介绍 web1,作为前端端服务器,访问地址是http://192.168.1.1,要将http://192.168.1.1/bbs的请求交给web2.在web1的网站根目录下并没有bbs目录 we ...
- html中音频和视频
HTML5音频中的新元素标签 src:音频文件路径. autobuffer:设置是否在页面加载时自动缓冲音频. autoplay:设置音频是否自动播放. loop:设置音频是否要循环播放. contr ...
- DecimalFormat中格式化问题
一:前言 每天自己斗会看到新的东西,每天自己都会学到东西,但是觉得自己老是想一口吃一个胖子.每天看到一个知识点都把其收藏了,但是自己也没有时间去看,不知道自己到底想感谢什么.真是自己无语,本来说是把自 ...
- cmd常用命令行
新建文件夹或文件 打开磁盘 F: 退出cmd exit 返回上一级 cd.. 创建文件夹 md 文件夹名 在d盘创建文件夹 md d:\文件夹名 在当前目录打开 ...
- SSL_TLS
http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html 配置 IBM HTTP Server 以支持 TLS 1.2 http://www-01.ibm ...
- 使用MapReduce将HDFS数据导入到HBase(三)
使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ...