【题目链接】: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. setAttribute的浏览器兼容性

    1.element要用getElementById 或者是ByTagName来得到 2.setAttribute("class", vName)中class是指改变"cl ...

  2. amazeui学习笔记--css(布局相关1)--网格Grid

    amazeui学习笔记--css(布局相关1)--网格Grid 一.总结 基本使用 1.div+class布局:amaze里面采取的就是div+class的布局方式  <div class=&q ...

  3. Android 解决RecyclerView删除Item导致位置错乱的问题

    RecyclerView的刷新分为内容变化和结构变化,结构变化比如remove和insert等并不会导致viewholder的更新,所以有时候我们使用 notifyItemRemoved(positi ...

  4. Day2:数据运算

    一.算数运算 如: #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan print(10%2) #求模(取模) # 0 ...

  5. HTTP--Request Headers及Cookies

    简介: HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.大多数请求头并不是必需的,但Content-L ...

  6. NSNotificationCenter消息通信(KVO)

    NSNotificationCenter是程序不同类间的消息通信. 注册消息通知: [[NSNotificationCenter defaultCenter]addObserver:self sele ...

  7. Android 自己定义主菜单

    本文介绍一个超简单的自己定义主菜单.效果例如以下: 原理:事实上就是对原生的Dialog的一个简单的封装.并加上显示和隐藏的动画效果.再给控件加上回调事件. TestDialog.java publi ...

  8. C语言深度剖析-----C语言中的字符串

    S1字符数组 S2字符串,存在于栈空间 S3最常规的写字符串的方法,malloc是堆空间,存在于只读存储区,我们不能够改变指向S3的数据 S4堆空间  S4 字符串的长度 判断字符串长度,assert ...

  9. Learn from Architects of Buildings

     Learn from Architects of Buildings Keith Braithwaite Architecture is a social act and the material ...

  10. python的报错

    1;; //////////////////////////////////////////////////////////////////////////////////////////////// ...