【题目链接】: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. Session丢失原因与解决方案

    win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程,   造成保存在该进程中的session丢失. 因为Session,Appl ...

  2. JS学习笔记 - 封装getPosition函数、一串跟着鼠标的div

    function getPosition(ev) { var scrollTop = document.documentElement.scrollTop || document.body.scrol ...

  3. GCJ 2008 Round 1A Minimum Scalar Product

    https://code.google.com/codejam/contest/32016/dashboard 题目大意: GCJ(google code jam)上的水题.下周二有比赛,来熟悉熟悉. ...

  4. ZOJ 1242 Carbon Dating

    UVA昨天上不去,今天一大早起来还是上不去 0.0 于是去ZOJ 这题大意就是半衰期... 取对数用到了换底公式...我都忘了这玩意了T T 上代码... #include<iostream&g ...

  5. 【习题 3-12 UVA - 11809】Floating-Point Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] \(A*10^B = temp[M]*2^{2^E-1}\) 两边取一下对数 得到 \(lg_A+B = lg_{temp[M]} ...

  6. IOS获取手机设备所有应用

    //返回一个数组 1 NSMutableArray *applist = [[NSMutableArray alloc]init]; NSString *pathOfApplications = @& ...

  7. js进阶 12-18 jquery如何实现自定义右键菜单(把问题分细)

    js进阶 12-18  jquery如何实现自定义右键菜单(把问题分细) 一.总结 一句话总结:用鼠标右键事件contextmenu,阻止系统默认事件,让做好的右键菜单显示出来,并且显示在我们出现的位 ...

  8. 程序员的底色(IDE color scheme、CLI 命令行界面)

    1. IDE ⇒ Dracula(吸血鬼) IDE:PyCharm,VS2013: sublime:color scheme,Monokai: 2. CLI 命令行界面 $ setterm -inve ...

  9. 源码笔记---MBProgressHUD

    前言 作为初学者,想要快速提高自己的水平,阅读一些优秀的第三方源代码是一个非常好的途径.通过看别人的代码,可以学习不一样的编程思路,了解一些没有接触过的类和方法. MBProgressHUD是一个非常 ...

  10. [Node] Run Any Version of a Node Tool with npx

    As node projects evolve, new features are added all the time. This results in different errors or re ...