http://codeforces.com/problemset/problem/776/D

题意:有n个门,m个开关,每个门有一个当前的状态(0表示关闭,1表示打开),每个开关控制k个门,但是每个门确切的受两个开关控制,如果一个开关打开,那么原来关闭的门会打开,打开的门关闭,问是否存在一个情况使得所有的门打开。

思路:类似于01染色,把开关当成点,门当前的状态当成边权建图。初始先假设一个门的状态(初始假设为0和假设为1都是一样的,举几个例子就发现了),然后因为每个门受两个开关控制,所以可以推出下一个门的状态,如果发现互斥的情况,那么就是“NO”。

这里设成门状态0表示打开,1表示关闭,方便运算。

 #include <bits/stdc++.h>
using namespace std;
#define N 200010
struct Edge {
int v, nxt, w;
} edge[N*];
int col[N], fir[N], sec[N], head[N], tot, w[N]; void Add(int u, int v, int w) {
edge[tot] = (Edge) {v, head[u], w}; head[u] = tot++;
edge[tot] = (Edge) {u, head[v], w}; head[v] = tot++;
} bool dfs(int u) {
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(col[v] == -) { // 如果这个开关没染色
col[v] = col[u] ^ w;
if(!dfs(v)) return false;
} else if(col[u] ^ col[v] ^ w) return false; // 如果染过色并发生矛盾
}
return true;
} int main() {
int n, m;
cin >> n >> m;
for(int i = ; i <= n; i++) scanf("%d", &w[i]), w[i] ^= ;
for(int i = ; i <= m; i++) {
int k; scanf("%d", &k);
while(k--) {
int a; scanf("%d", &a);
if(fir[a] == ) fir[a] = i; // 连a点的第一个开关
else sec[a] = i; // 连a点的第二个开关
}
}
memset(head, -, sizeof(head)); tot = ;
for(int i = ; i <= n; i++)
Add(fir[i], sec[i], w[i]);
memset(col, -, sizeof(col));
bool flag = ;
for(int i = ; i <= m; i++) {
if(col[i] == -) {
col[i] = ; // 假设这个开关一开始是关的
if(!dfs(i)) { flag = ; break; }
}
}
if(!flag) puts("NO");
else puts("YES");
return ;
}

Codeforces 776D:The Door Problem(DFS染色)的更多相关文章

  1. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  2. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  3. 【codeforces 776D】The Door Problem

    [题目链接]:http://codeforces.com/contest/776/problem/D [题意] 每个门严格由两个开关控制; 按一下开关,这个开关所控制的门都会改变状态; 问你能不能使所 ...

  4. Codeforces 781A:Andryusha and Colored Balloons(DFS染色)

    http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...

  5. Codeforces Gym100502A:Amanda Lounges(DFS染色)

    http://codeforces.com/gym/100502/attachments 题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点 ...

  6. Codeforces 1144F Graph Without Long Directed Paths DFS染色

    题意: 输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2. 如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0. ...

  7. cf804C(dfs染色)

    题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...

  8. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  9. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  10. hdu 4751 Divide Groups(dfs染色 或 2-sat)

    Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more c ...

随机推荐

  1. ControlTemplate

    ControlTemplate:外观定制 <Window.Resources> <ControlTemplate x:Key="CheckBoxControlTemplat ...

  2. WPF 附加属性的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. 关于C#的可变长参数

    可变参数 params===>> params int[] list,传入参数的类型必须是一种类型 static void Main(string[] args) { , , , ); C ...

  4. epplus输出成thml

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. HTML特殊编码转换

    var encoded = ""'&<>¡¢£¤" + "¥¦§¨©ª«¬­®" + "¯°±²³´µ¶·" ...

  6. GIS基础软件及操作(四)

    原文 GIS基础软件及操作(四) 练习四.空间数据处理 1.空间数据处理(融合.合并.剪切.交叉.合并)2.设置地图投影及投影变换 空间数据处理 数据:云南县界.shp; Clip.shp西双版纳森林 ...

  7. Android零基础入门第9节:Android应用实战,不懂代码也可以开发

    原文:Android零基础入门第9节:Android应用实战,不懂代码也可以开发 通过上一期的学习,我们成功开发了Android学习的第一个应用程序,不仅可以在Android模拟器上运行,同时还能在我 ...

  8. R3 HOOK OpenProcess 的问题

    unit HookAPI; //Download by http://www.codefans.net interface uses Windows, Classes; function Locate ...

  9. Codility---FrogJmp

    Task description A small frog wants to get to the other side of the road. The frog is currently loca ...

  10. 解决socket.error: [Errno 98] Address already in use问题

    如果python中socket 绑定的地址正在使用,往往会出现错误, 在linux下: 则会显示“ socket.error: [Errno 98] Address already in use” 在 ...