【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的次方数. 题解 ...
随机推荐
- 近期对于windows服务的理解
1.APP.config的作用 在开发环境下时,根目录下的APP.config里面会填写一些参数之类的.当生成之后,这些参数将会被自动生成在*.exe文件目录中.如图: 其中,.exe文件为Win ...
- HDU 3446 有贪心思想的01背包
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- D. Sorting the Coins
Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins togeth ...
- Spring表达式语言之SpEL
•Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL ...
- Go 实现 soundex 算法
[转]http://www.syyong.com/Go/Go-implements-the-soundex-algorithm.html SOUNDEX 返回由四个字符组成的代码 (SOUNDEX) ...
- [BZOJ1025] [SCOI2009]游戏 解题报告
Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ...
- swift xcode设置 ,代码折叠,全局折叠 快捷键
在preference text editing 里面打开 function 折叠的项, 折叠方法快捷键: option+command +left/right 全局折叠快捷键: shift+opti ...
- CDLinux 自动休眠功能的关闭方法
CDLinux 自动休眠功能的关闭方法: 控制台下使用xset命令来完成. xset q 可以查看当前屏幕保护和电源管理的状态信息 具体设置时,常用的有以下参数: xset s //这个参数设置屏 ...
- Python用户登陆
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__:Dahlhin import sys userinfo = r'userinfo.txt ...