codeforce 776 D The Door Problem(DFS遍历)
来源:互联网 发布:软件项目组人员 编辑:程序博客网 时间:2024/06/06 13:18
Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.
You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.
You need to tell Sherlock, if there exists a way to unlock all doors at the same time.
First line of input contains two integers n and m (2 ≤ n ≤ 105, 2 ≤ m ≤ 105) — the number of rooms and the number of switches.
Next line contains n space-separated integers r1, r2, ..., rn (0 ≤ ri ≤ 1) which tell the status of room doors. The i-th room is locked if ri = 0, otherwise it is unlocked.
The i-th of next m lines contains an integer xi (0 ≤ xi ≤ n) followed by xi distinct integers separated by space, denoting the number of rooms controlled by the i-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to n. It is guaranteed that each door is controlled by exactly two switches.
Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes.
这一题先构个图,节点为开关,边为门,然后进行着色。当门为1时,这两端的开关颜色形同,门为0时,这两端的颜色不同。然后dfs遍历一遍,然后边跑边判断。这种方法比较巧妙。这一题用并查集和2-SAT什么的也可以。
AC代码:
#include<stdio.h>#include<iostream>#include<vector>#include<stack>#include<string>#include<stdlib.h>#include<math.h>#include<queue>#include<map>#include<string.h>#include<algorithm>using namespace std;typedef long long int ll;typedef unsigned long long int ull;int used[100010];struct edge {int to;int attribute;};int n, m, a[100010], wrong;vector<edge> v[100010];//图vector<int> s[100010];//门的编号对应的边void ConstructGraph(){//构图for (int i = 1; i <= n; i++) {int t1 = s[i][0], t2=s[i][1];edge e;e.to = t2;e.attribute = a[i];v[t1].push_back(e);e.to = t1;e.attribute = a[i];v[t2].push_back(e);}}void dfs(int root, int f) {//遍历int Size = v[root].size();for (int i = 0; i < Size; i++) {edge e = v[root][i];if(e.to==f){continue;}if (used[e.to] == 0) {if (e.attribute == 1) {used[e.to] = used[root];dfs(e.to, root);}else {used[e.to] = -used[root];dfs(e.to, root);}}else {if (e.attribute == 1) {if (used[root] != used[e.to]) {wrong = 1;return;}}else {if (used[root] == used[e.to]) {wrong = 1;continue;}}}}}int main() {int i, j, k, t;scanf("%d%d", &n, &m);for (i = 1; i <= n; i++) {scanf("%d", &a[i]);}for (i = 1; i <= m; i++) {scanf("%d", &k);for (j = 1; j <= k; j++) {scanf("%d", &t);s[t].push_back(i);}}ConstructGraph();memset(used, 0, sizeof(used));for (i = 1; i <= m; i++) {if (wrong == 1) {printf("NO");return 0;}if (used[i] == 0) {used[i] = 1;dfs(i, 0);}else {continue;}}printf("YES");return 0;}
- codeforce 776 D The Door Problem(DFS遍历)
- codeforces-776D-The Door Problem(染色+建边+dfs)
- 【codeforces 776D】The Door Problem
- CodeForces-776D The Door Problem
- Codeforces 776D The Door Problem 二分图判定
- 【Codeforces 776 D The Door Problem】+ 并查集
- CF 776D The Door Problem TwoSAT 模板题
- CodeForces 776D The Door Problem【并查集】
- Codeforces 776D The Door Problem 【并查集】
- Codeforces Round #400 D. The Door Problem (建图+搜索)
- codeforces 776D The Door Problem(带权并查集)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) 776D. The Door Problem(待翻译)
- codeforces Round 400 D-The Door Problem
- codeforces round 400 D The Door Problem 2-SAT
- Codeforces-766D-The Door Problem(2-SAT-并查集解)
- The Door Problem CodeForces
- codeforce New Problem(dfs)
- DFS&&codeforce 598d
- c++第一次实验:简单计算问题体验
- 英文长句单词字典排序
- leetcode81~Search in Rotated Sorted Array II
- 选取相册是解决部分手机返回路径为空
- java rmi如何支持虚拟机(环境上仅有小网ip,而配置了大网ip的路由)
- codeforce 776 D The Door Problem(DFS遍历)
- 图论(3)-Freckles
- C++图形库libigl的Visual Studio配置方法
- PHP学习笔记——变量的声明;
- Qt4.8.7 移植到海思3520D
- MongoDB 3.4 高可用集群搭建(三)shard 分片
- SDM for face alignment 人脸对齐相关资料整理
- hdu5797
- android SubscriptionInfo更新流程