Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集

本文介绍了一种使用并查集解决特定门状态问题的方法。该问题包含n个门及m组开关,每组开关控制一定数量的门的状态反转。文章详细阐述了如何通过并查集判断所有门状态能否统一为开启状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题意:给你n个门,和m组开关,每扇门都有两个开关控制,每个开关控制x扇门,如果选择了某组开关,则使这组开关里的每个开关控制的所有的门按状态取反,问你是否能使得所有的门状态为1 


做法:确实吊呀,因为每个门题目保证是由两组开关控制的,用x代表不动,x+m代表动这个开关。

那么当第i扇门关着,那就用u和v+m或者u+m和v。
如果是开着,那就用u和v或者u+m和u+m。
用并查集维护,最后判断一下i和i+m是不是在一个并查集,如果是,那就矛盾,NO。


///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \./                  \\`.
///        .'//                     |                     \\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\`.
///   .'//______.============-..   \ | /   ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scanf("%I64d",&x)
#define S_2(x,y) scanf("%I64d%I64d",&x,&y)
#define S_3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=1e19+10;
//const int dx[]={-1,0,1,0,1,-1,-1,1};
//const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=2e5+10;
const int maxx=1e6+10;
const double EPS=1e-10;
const double eps=1e-10;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
/*
void readString(string &s)
{
	static char str[maxx];
	scanf("%s", str);
	s = str;
}
*/
int n,m;
int fa[maxn];
int fi(int x)
{
    return fa[x]==x?x:fa[x]=fi(fa[x]);
}
void uion(int u,int v)
{
    int x1=fi(u);
    int x2=fi(v);
    if(x1!=x2)
        fa[x1]=x2;
}
int vis[maxn];
vector<int>G[maxn];
void solve()
{
    W(s_2(n,m)!=EOF)
    {
        FOR(0,maxn-1,i) G[i].clear();
        FOR(1,n,i)
            s_1(vis[i]);
        FOR(1,maxn-1,i)
            fa[i]=i;
        FOR(1,m,i)
        {
            int x,y;
            s_1(x);
            FOR(1,x,j)
            {
                s_1(y);
                G[y].pb(i);
            }
        }
        FOR(1,n,i)
        {
            int u=G[i][0],v=G[i][1];
            if(!vis[i])
            {
                uion(u,v+m);
                uion(v,u+m);
            }
            else
            {
                uion(u,v);
                uion(u+m,v+m);
            }
        }
        int f=0;
        FOR(1,m,i)
            if(fi(i)==fi(i+m)){f=1;break;}
        if(f)
        {
            puts("NO");
            continue;
        }
        else puts("YES");
    }
}

int main()
{
    //freopen( "in.txt" , "r" , stdin );
    //freopen( "data.txt" , "w" , stdout );
    int t=1;
    //init();
    //s_1(t);
    for(int cas=1;cas<=t;cas++)
    {
        //printf("Case #%d: ",cas);
        solve();
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值