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

注意到每扇门都有两个东西和它连接着,那么,如果第i扇门的状态是1,也就是已经打开了,那么连接它的两个按钮的状态应该是一样的,也就是必须是同时按,或者同时不按。然后0的话就是关闭的,所以连接它的两个按钮应该是一个按,一个不按,或者相反。

所以这就是一个带权并查集(一开始没想到,以为相同的,用并查集合并就好,然后不同的,建立一个图什么的,发现有点麻烦,然后推着推着,就是带权并查集了),然后就写了很久很久,一直wa

ps,并查集初始化的那个,不能光靠n,1--n  f[i] = i,不行的,因为m可能比n大。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
int fa[maxn], state[maxn], siz[maxn];
int tofind(int u) {
if (fa[u] == u) return u;
else {
int t = fa[u];
fa[u] = tofind(fa[u]);
siz[u] = (siz[t] + siz[u] + ) % ;
return fa[u];
}
}
bool tomerge(int x, int y, int val) {
int tx = x, ty = y;
x = tofind(x);
y = tofind(y);
if (x == y) {
if ((siz[tx] + siz[ty] + ) % != val) return false;
else return true;
} else {
fa[y] = x;
siz[y] = (val + siz[ty] + siz[tx]) % ;
siz[x] = ;
return true;
}
}
vector<int>e[maxn];
void work() {
int n, m;
cin >> n >> m;
for (int i = ; i <= n; ++i) {
cin >> state[i];
}
for (int i = ; i <= maxn - ; ++i) {
fa[i] = i;
siz[i] = ;
}
for (int i = ; i <= m; ++i) {
int x;
cin >> x;
for (int j = ; j <= x; ++j) {
int pos;
cin >> pos;
e[pos].push_back(i);
}
}
for (int i = ; i <= n; ++i) {
if (!tomerge(e[i][], e[i][], state[i])) {
cout << "NO" << endl;
// cout << i << endl;
return;
}
}
cout << "YES" << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

最新文章

  1. linux驱动中printk的使用注意事项
  2. 【大数据】Summingbird(Storm + Hadoop)的demo运行
  3. css与js后边有?v=20160101
  4. 160921、React入门教程第一课--从零开始构建项目
  5. [转][Unreal3教程]引擎使用操作入门教程
  6. Js验证userAgent是否来自手机端
  7. 《灰帽Python-黑客和逆向工程师的Python编程》学习记录
  8. Epplus 使用的简单介绍
  9. unity 与 android 协调工作 注意事项
  10. linux中mysql完整卸载命令操作
  11. 【Demo 0009】Java基础-异常
  12. 【Zookeeper】源码分析之Watcher机制(一)
  13. 海康/大华 IpCamera RTSP地址和格式
  14. JAVA模板方法
  15. ARM的GPIO配置
  16. 解析Visual C# 7.2中的private protected访问修饰符
  17. Python开发 文件操作
  18. linux中安装和配置 jdk
  19. react学习(四)之设置 css样式 篇
  20. iOS UI基础-10.0 QQ聊天布局之键盘及文本使用

热门文章

  1. linux以及git和maven常用命令
  2. HTML canvas
  3. C++ HOJ 火车进站
  4. HDU 6122 今夕何夕 【数学公式】 (2017&quot;百度之星&quot;程序设计大赛 - 初赛(A))
  5. iOS 如何改变表视图分割线在iOS7中的默认偏移
  6. POJ1511 Invitation Cards —— 最短路spfa
  7. How to create a List of ValueTuple?
  8. Lesson one of python
  9. html5--6-4 CSS选择器
  10. hdu 4022 Bombing(map,multiset)