再来回顾一下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. Any gotchas at all with converting from MyISAM to InnoDB?

    Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...

  2. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  3. 使用Idea远程部署调试tomcat

    转自:http://blog.csdn.net/jane1229/article/details/52402119 远程服务器的配置: 1.在远程服务器安装jdk和tomcat 2.配置环境变量 PA ...

  4. 使用apache构建OpenStack内部yum源

    安装apache yum install httpd -y 上传openstack-mitaka-rpms.tar包,链接:http://pan.baidu.com/s/1kVA1wKv 密码:q26 ...

  5. [转]华 使用npm安装一些包失败了的看过来(npm国内镜像介绍)

     发布于 5 年前  作者 wppept  275957 次浏览  最后一次编辑是 1 年前 这个也是网上搜的,亲自试过,非常好用! 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置 ...

  6. asp单页面301跳转

    <% Response.Status="301 Moved Permanently"Response.AddHeader "Location", &quo ...

  7. swt 更新主UI线程

    // 将msg送回对应的Applet public void write(String msg) { synchronized (msg) { try { m_out.writeUTF(msg); } ...

  8. (十四)git操作

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  9. Chrome扩展及应用开发

    Chrome扩展及应用开发(电子书) http://www.ituring.com.cn/minibook/950 文档 官方 https://developer.chrome.com/extensi ...

  10. 新版谷歌浏览器设置flash插件不提示步骤

    1.先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 2.然后去chrome:/ ...