【题目链接】:http://codeforces.com/contest/776/problem/D

【题意】



每个门严格由两个开关控制;

按一下开关,这个开关所控制的门都会改变状态;

问你能不能使所有的门都打开(同时);

【题解】

/*
对于每个门,
有两个开关连接着它;
用一条边连接这两个开关
则如果这个门的状态是关的,
则这条边的边权为1
表示它们俩的颜色不能一样;
(也即这两个开关只能改变一个)
也即要1开1关;
如果这个门的状态是开的
则这条边的边权为0;
表示它们俩的颜色相同;
即同时开或同时关.
然后做一个二分图染色
可以就是YES否则NO
也就是说把开关看成节点,门看成边,根据边来确定这条边的两端的节点的颜色是一样还是不同;
*/

【完整代码】

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#define rei(x) cin >> x
#define rep1(i,x,y) for (int i = x;i <= y;i++)
using namespace std; const int N = 2e5 + 100; int n, m, a[N],color[N];
vector <int> g1[N],g[N],w[N]; void dfs(int x, int c)
{
color[x] = c;
int len = g[x].size();
rep1(i, 0, len - 1)
{
int y = g[x][i];
int ju = w[x][i];
if (color[y] != 0)
{
if (ju == 1 && color[y] == color[x])
{
puts("NO");
exit(0);
}
if (ju == 0 && color[y] != color[x])
{
puts("NO");
exit(0);
}
}
else
if (ju == 0)
dfs(y, c);
else
dfs(y, 3 - c);
}
} int main()
{
//freopen("D:\\rush.txt", "r", stdin);
rei(n),rei(m);
rep1(i, 1, n)
rei(a[i]);
rep1(i, 1, m)
{
int num;
rei(num);
rep1(j, 1, num)
{
int x;
cin >> x;
g1[x].push_back(n + i);
g1[n + i].push_back(x);
}
}
rep1(i, 1, n)
{
int y1 = g1[i][0], y2 = g1[i][1];
int bian = 1 - a[i];
g[y1].push_back(y2), g[y2].push_back(y1);
w[y1].push_back(bian),w[y2].push_back(bian);
}
rep1(i,n+1,n+m)
if (color[i] == 0)
dfs(i, 1);
puts("YES");
return 0;
}

【codeforces 776D】The Door Problem的更多相关文章

  1. 【codeforces 442B】 Andrey and Problem

    http://codeforces.com/problemset/problem/442/B (题目链接) 题意 n个人,每个人有p[i]的概率出一道题.问如何选择其中s个人使得这些人正好只出1道题的 ...

  2. 【Codeforces 442B】Andrey and Problem

    [链接] 我是链接,点我呀:) [题意] n个朋友 第i个朋友帮你的概率是pi 现在问你恰好有一个朋友帮你的概率最大是多少 前提是你可以选择只问其中的某些朋友不用全问. [题解] 主要思路是逆向思维, ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. 一本通1548【例 2】A Simple Problem with Integers

    1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...

  5. 【codeforces 527D】Clique Problem

    [题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...

  6. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

  7. 【codeforces 793C】Mice problem

    [题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...

  8. 【codeforces 807D】Dynamic Problem Scoring

    [题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...

  9. 【codeforces 742B】Arpa’s obvious problem and Mehrdad’s terrible solution

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. Android圆环控件

    Android圆环控件 近期在做一个功能.界面效果要求例如以下: 看到这个界面,我首先想到了曾经在做phone模块的时候,我们定制的来电界面InCallTouchUi,界面效果是相似的. 来电控件使用 ...

  2. web.config访问走代理的配置

    <system.net>    <defaultProxy>      <proxy bypassonlocal="False" usesystemd ...

  3. UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂)

    UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂) 题目链接 题目大意:假设有一个合数.然后它满足随意大于1小于n的整数a, 满足a^n%n = a;这种合数叫做Ca ...

  4. nginx启用https访问

    什么是https? https 全称:Hyper Text Transfer Protocol over Secure Socket Layer,是http的安全版.即http下加入SSL协议层,因此 ...

  5. 清除浮动.md

    清除浮动的三种方法 1 加空div层(.clear) 2 overflow属性设置(.clearo) 3 :after伪元素(.clearfix) <!DOCTYPE html> < ...

  6. FTP、SSH、NFS等环境工具的安装

    注意:通过ftp互传文件或者通过ssh登录的时候,ubuntu需要使用bridged上网环境 ftp: sudo apt-get install vsftpd sudo vi /etc/vsftpd. ...

  7. java測试网络连接是否成功并设置超时时间

    /** * 获取RMI接口状态 * * @return "0":服务正常,"1": 连接报错,"2":连接超时 */ @Override p ...

  8. 修改SVN中文件的可执行属性

    博文来自下面路径,转载请注明原出处: http://bigwhite.blogbus.com/logs/74568031.html 修改SVN中文件的可执行属性 - [开源世界] Tag:开源世界 S ...

  9. MySQL数据导出导入任务脚本

    #!/usr/bin/env python#-*- encoding: utf8 -*- import timeimport osimport mysql.connector #定义一些全局变量  w ...

  10. 小米笔记本(13.3 I7) ubuntu14.04下网卡驱动安装

    ubuntu 内核升级到4.6.4(更高版本可能造成系统无法启动) kernel debian包下载地址 http://kernel.ubuntu.com/~kernel-ppa/mainline/v ...