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 ;
}

D. The Door Problem 带权并查集的更多相关文章

  1. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  2. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  3. Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理

    题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★   输入文件:marbles.in   输出 ...

  4. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  5. Exclusive-OR(带权并查集)

    Exclusive-OR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. poj 1733 Parity game(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...

  7. bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...

  8. CCPC-Wannafly Winter Camp Day3 Div1 - 石头剪刀布 - [带权并查集]

    题目链接:https://zhixincode.com/contest/14/problem/I?problem_id=211 样例输入 1  3 5 2 1 1 2 1 2 1 1 2 3 2 1 ...

  9. hihoCoder 1515 分数调查(带权并查集)

    http://hihocoder.com/problemset/problem/1515 题意: 思路: 带权并查集的简单题,计算的时候利用向量法则即可. #include<iostream&g ...

随机推荐

  1. ermissions on /usr/local/mongodb/conf/keyFile are too open

    ermissions on /usr/local/mongodb/conf/keyFile are too open > rs.initiate(cfg); { "ok" : ...

  2. Android:在子线程中更新UI的三种方式

    ①使用Activity中的runOnUiThread(Runnable) ②使用Handler中的post(Runnable) 在创建Handler对象时,必须先通过Context的getMainLo ...

  3. C++文件IO操作的简单示例

    CppIODemo1.cpp #include <iostream> #include <fstream> #include <chrono> #define IN ...

  4. Mac JDK 多版本共存

    1.    安装各JDK版本,安装后通过Java -version检测是否安装好    2.    打开~/.bash_profile,没有的话创建    vim ~/.bash_profile   ...

  5. linux 设备驱动程序中的一些关联性思考

    首先,个人感觉设备驱动程序与应用程序中的文件操作隔得有点远,用户空间不论是直接使用系统调用还是库函数都是通过系统调用的接口进入内核空间代码的.但是看过一个博客的分析整个过程,感觉中间层太过麻烦,必须经 ...

  6. ios很好的开源库

    Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库,持续更新.. 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD ...

  7. bzoj4103: [Thu Summer Camp 2015]异或运算

    对于每个询问暴力枚举x~y,然后在Trie去找第k大,开始我写了个二分答案然后算比当前答案大的个数,打了个第10个点的表就跑出19s+比bzoj垫底还慢4s+ 然而不用二分,直接1000个点一起在树上 ...

  8. 获取cookie值

    function get_cookie(Name) { var search = Name + "=" var returnvalue = ""; if (do ...

  9. codeforces 672C C. Recycling Bottles(计算几何)

    题目链接: C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  10. Top的VIRT是什么

    Top命令监控某个进程的资源占有情况  下面是各种内存: VIRT:virtual memory usage 1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等     2.假如进程申请1 ...