Codeforces 776D 并查集
来源:互联网 发布:淘宝 拍摄脸部 编辑:程序博客网 时间:2024/05/21 08:45
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 ifri = 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.
3 31 0 12 1 32 1 22 2 3
NO
3 31 0 13 1 2 31 22 1 3
YES
3 31 0 13 1 2 32 1 21 3
NO
In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1 — unlocked).
After toggling switch 3, we get [0, 0, 0] that means all doors are locked.
Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.
It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked.
题意:给你n扇门,m种操作,n扇门开始的状态,0代表关,1代表开,m个操作,每个操作会使k扇门翻转,问能不能将所有门打开。
每扇门都严格有两种操作包含它。
题解:假设一个门的操作是a和b。令k代表是使用k操作,k+m表示不用。
那么当第i扇门关着,那就用a和b+m或者a+m和b。
如果是开着,那就用a和b或者a+m和b+m。
用并查集维护,最后判断一下i和i+m是不是在一个并查集,如果是,那就矛盾,NO。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;vector<int>sp[200005];int pre[400005],yes[200005];int find(int x){ int r=x; while(pre[r]!=r) r=pre[r]; int i=x,j; while(i!=r){ j=pre[i]; pre[i]=r; i=j; } return r; }void link(int a,int b){if(find(a)!=find(b))pre[find(a)]=find(b);}int main(){int x,y,n,m,i,j;scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%d",&yes[i]);for(i=1;i<=m;i++){scanf("%d",&x);for(j=1;j<=x;j++){scanf("%d",&y);sp[y].push_back(i);}}for(i=1;i<=400000;i++)pre[i]=i;for(i=1;i<=n;i++){int a=sp[i][0],b=sp[i][1];if(!yes[i]){link(a,b+m);link(a+m,b);}else{link(a,b);link(a+m,b+m);}}for(i=1;i<=m;i++){if(find(i)==find(i+m)){printf("NO\n");return 0;}}printf("YES\n");return 0;}
- Codeforces 776D 并查集
- 【Codeforces 776 D The Door Problem】+ 并查集
- CodeForces 776D The Door Problem【并查集】
- Codeforces 776D The Door Problem 【并查集】
- Codeforces 366D 贪心+并查集
- Codeforces 371D. Vessels【并查集】
- codeforces 366D 并查集
- codeforces 593D(并查集 + LCA)
- codeforces 691D 并查集
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 766D 并查集
- codeforces 766D 并查集进阶
- CodeForces 25D(并查集)
- Codeforces 292D Connected Components (并查集)
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- 【并查集分块】Codeforces 475D CGCDSSQ
- CodeForces 151D Quantity of Strings(并查集)
- codeforces 593 D. Happy Tree Party (LCA + 并查集)
- MFC在命令行窗口输出
- Unity 通过LineRenderer绘制两点之间的直线
- 《写给大家看的设计书》总结
- Android Studio设置自动导包
- 实现开发环境、测试环境、生产环境配置自动切换
- Codeforces 776D 并查集
- 中介者模式
- YTKNetwork 使用高级教程
- 第六十九篇:从ADAS到自动驾驶(二):ADAS的功能及发展
- 《智能时代》读书笔记:这是最好的时代,也是最坏的时代
- 设置约束后 如何获取正确的frame
- 【年终总结】吾尽吾心,终亦不悔;天道酬勤,何事难为?
- 输出文件的最后几行,可以对付超大文件
- YTKNetwork 使用基础教程