codeforces-776D-The Door Problem(染色+建边+dfs)
来源:互联网 发布:苹果电脑软件怎么安装 编辑:程序博客网 时间:2024/06/06 05:18
问题链接:http://codeforces.com/problemset/problem/776/D
问题大意:有n扇门,m把锁,每个门由两把锁控制,给你每个门的初始状态以及每把锁
所能控制的x个门,问你怎样使用锁使得可以让所有门都是开着的状态
题目思路:似乎像是开灯问题,但是不同的是,假如有两把锁控制了一扇关着的门,且这
两把锁并没有控制其他的门,这样的话不能使用状态的连锁反应来求解了,后来查了题解
也是各种姿势写这道题,这里使用建边的方法来完成,具体思路如下:
首先将每扇门所被控制的呢两把锁建边,该边的颜色就是灯的初始状态;
然后你会思考这样一件事,一定会有两种状态:
1.该门是关着的,则肯定需要对其中一个开关进行操作(肯定不能两个开关都改变)
这样的话你很容易想到,这两个开关最后可以看做分在了两个不同的阵营。假如此前所遍历
的结果使得这两把开关放在了同一阵营的话,则肯定最后不能符合条件。
2.该门是开着的,此时要么两个开关都进行操作一次,要么都不操作,因此,这两个开关必
定是属于同一阵营的
#include<stdio.h>#include<algorithm>#include<vector>using namespace std;#define maxn 100005int n,m,tot;int head[maxn];int a[maxn],vis[maxn];vector<int>q[maxn];struct node{int to;int next;int c;}e[maxn*2];void add(int u,int v,int c){e[++tot].to=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot;}bool dfs(int x,int y){if(vis[x])return vis[x]==y;vis[x]=y;for(int i=head[x];i;i=e[i].next){if(!dfs(e[i].to,e[i].c?y:y^3))return 0;}return 1;}int main(){int i,j,x,num;scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=m;i++){scanf("%d",&num);for(j=1;j<=num;j++){scanf("%d",&x);if(q[x].size()>0){add(i,q[x][0],a[x]);add(q[x][0],i,a[x]);}elseq[x].push_back(i);}}for(i=1;i<=m;i++){if(!vis[i]){if(!dfs(i,1)){ printf("NO\n"); return 0;}}}printf("YES\n");}
0 0
- codeforces-776D-The Door Problem(染色+建边+dfs)
- codeforce 776 D 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】+ 并查集
- CodeForces 776D The Door Problem【并查集】
- Codeforces 776D The Door Problem 【并查集】
- Codeforces Round #400 D. 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 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)
- The Door Problem CodeForces
- codeforces round 400 D The Door Problem 2-SAT
- Codeforces-766D-The Door Problem(2-SAT-并查集解)
- CF 776D The Door Problem TwoSAT 模板题
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- C#中根据经纬度计算两点的距离
- 又可以重新写博客了!
- windows下python模块构建发布
- Android与Unity交互调用mUnityPlayer.quit()方法退出返回上一个Activity界面
- IP包分段
- codeforces-776D-The Door Problem(染色+建边+dfs)
- Lucene初级认识
- http、https 等 常用默认端口号
- 队列(Queue)-- 数组实现(C++ )
- 7.3图的遍历(广度优先)-理论
- Oracle instant client + sqlplus/navicat/PLsql 连接数据库
- 剑指Offer:替换空格
- myeclipse项目转到eclipse上
- String 的 equals 和 ==