Codeforces 776D The Door Problem 二分图判定
来源:互联网 发布:淘宝联盟无法提现 编辑:程序博客网 时间:2024/06/06 13:04
点击打开链接
题意:n个开关,m扇门(n,m<=1e5),每个开关控制若干个门,反转开关门状态变化,每个门正好被两个开关控制,问是否有可能把所有门的状态置为1?
注意到一个门只被两个开关控制 若门初始为0 则控制它的两个开关状态相反,门初始为1则 要求控制的两个开关状态相同,才能可以把门置为1.
以开关为顶点,(u,v)的边为被(u,v)控制的门的初始状态, dfs做二分图染色,判断每条边(每扇门)是否都能被满足即可
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=2e5+20;int n,m,d[N];//d[i] 第i扇门的状态 vector<int> p[N];//p[i]门i被那两个开关控制 vector<pair<int,int> > e[N];int c[N];//c[i]结点i的颜色 bool flag;void dfs(int u,int x){if(flag==false)return;if(c[u]!=-1){if(c[u]!=x)flag=false;return;}c[u]=x;//for(int i=0;i<e[u].size();i++){int v=e[u][i].first,w=e[u][i].second; int y=1-(x^w);dfs(v,y);}}int main(){while(cin>>m>>n){flag=true;memset(c,-1,sizeof(c));memset(e,0,sizeof(e));memset(p,0,sizeof(p));for(int i=1;i<=m;i++){scanf("%d",&d[i]);}for(int i=1;i<=n;i++){int x,k;scanf("%d",&k);for(int j=1;j<=k;j++){scanf("%d",&x);p[x].push_back(i);//门x被开关i控制}}for(int i=1;i<=m;i++){int u=p[i][0],v=p[i][1];e[u].push_back(make_pair(v,d[i]));//开关u和开关v状态的受门i影响 e[v].push_back(make_pair(u,d[i])); }for(int i=1;i<=n;i++){if(c[i]==-1)dfs(i,0);}if(flag)puts("YES");elseputs("NO");}return 0;}
0 0
- Codeforces 776D The Door Problem 二分图判定
- 【codeforces 776D】The Door Problem
- CodeForces-776D The Door Problem
- codeforces-776D-The Door Problem(染色+建边+dfs)
- 【Codeforces 776 D The Door Problem】+ 并查集
- CodeForces 776D The Door Problem【并查集】
- Codeforces 776D The Door Problem 【并查集】
- codeforces Round 400 D-The Door Problem
- The Door Problem CodeForces
- Codeforces Round #400 D. The Door Problem (建图+搜索)
- codeforces round 400 D The Door Problem 2-SAT
- Codeforces-766D-The Door Problem(2-SAT-并查集解)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)
- codeforces 776D The Door Problem(带权并查集)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) 776D. The Door Problem(待翻译)
- codeforce 776 D The Door Problem(DFS遍历)
- CF 776D The Door Problem TwoSAT 模板题
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D. The Door Problem【2-sat Tarjan+思维建图】
- Spring注解详解
- 0、Oracle学习路线分析(Oracle必读)
- redis 集群的部署
- Tomcat服务器集群与负载均衡实现
- Easyui-datagrid的column的type是combobox时,对valueField,textField,及formatter函数return值的理解
- Codeforces 776D The Door Problem 二分图判定
- Android 事件传递记录
- 网络/安全]阿里云服务器配置VPN详解
- 30个不可不知的容器技术工具和资源
- redhat6.5安装tongweb注意事项
- mysql按周分组,带跨年功能
- React绑定this的三种方式
- HttpClient4.5 SSL访问工具类
- Python的类方法,静态方法,实例方法的区别