首页 > 其他分享 >CF776D The Door Problem

CF776D The Door Problem

时间:2023-08-15 19:14:41浏览次数:36  
标签:Door int 合并 fa 钥匙 CF776D 两把 Problem 扇门

题目大意

给定门和钥匙的数量,每把钥匙控制 \(k_i\) 扇门,每扇门被两把钥匙控制。

给定初始时每扇门的状态,求是否存在一种方法使得所有的门都打开。

思路

扩展域并查集。

考虑分类讨论:

  • 对于开着的门,要么两把钥匙都用,要么两把钥匙都不用;
  • 对于关着的门,两把钥匙只能用一把。

那么我们就可以用并查集来进行维护了。

设 \(i\) 为用第 \(i\) 把钥匙,\(i+m\) 为不用第 \(i\) 把钥匙。

对于一扇门,我们记它的两把钥匙为 \(u,v\)。

如果这扇门开着,我们将 \(u\) 与 \(v\) 合并,\(u + m\) 与 \(v+m\) 合并;

如果这扇门关着,我们将 \(u\) 与 \(v+m\) 合并,\(u + m\) 与 \(v\) 合并。

最后,我们检查 \(i\) 与 \(i + m\) 是否在同一集合,如果有在同一集合的,输出 NO,否则输出 YES

Code

#include <bits/stdc++.h>

using namespace std;

const int N = 200500;

int n,m,k[N];
int state[N];
vector<int> key[N];

int fa[N << 1];

class DSU{
public:
    int Find(int x) {
        if(fa[x] == x)
            return x;
        return fa[x] = Find(fa[x]);
    }

    void Merge(int x,int y) {
        x = Find(x);
        y = Find(y);

        fa[x] = y;
        return ;
    }

    bool Check(int x,int y) {
        x = Find(x);
        y = Find(y);

        if(x == y)
            return 1;
        return 0;
    }
}dsu;

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 1;i <= n; i++) 
        cin >> state[i];

    for(int i = 1,num,x;i <= m; i++) {
        cin >> num;
        fa[i] = i;
        fa[i + m] = i + m;
        for(int j = 1;j <= num; j++) {
            cin >> x;
            key[x].push_back(i);
        }
    }

    for(int i = 1;i <= n; i++) {
        int u = key[i][0],v = key[i][1];

        if(state[i]) {
            dsu.Merge(u,v);
            dsu.Merge(u + m,v + m);
        }
        else {
            dsu.Merge(u,v + m);
            dsu.Merge(u + m,v);
        }
    }

    for(int i = 1;i <= m; i++) {
        if(dsu.Check(i,i + m)) {
            cout << "NO\n";
            return 0;
        }
    }
    cout << "YES\n";
    return 0;
}

标签:Door,int,合并,fa,钥匙,CF776D,两把,Problem,扇门
From: https://www.cnblogs.com/baijian0212/p/solution-cf776d.html

相关文章

  • 胡萝卜问题 Carrot Problems
    Refhttps://www.atvbt.com/the-carrot-problem/......
  • AT_apc001_g Colorful Doors 题解
    模拟赛做到的题,场上写贪心爆栈了qwq首先在首尾加上两个\(1\)表示进出,将两段路中间的间隔作为传送门,恰好有\(2\timesN\)个传送门,根据两段路的经过情况给传送门分类别:00:用\(N\)表示,称为无用点,不到达该点。10:用\(S\)表示,称为起点,需要通过向右走走到一次。01:用\(T\)......
  • more and more construction problem,what should i do ?
    一些构造CF1464FShowingOff显然原图连边会形成若干内向基环树森林,所有在同一个环上的点\(S\)是相同的,注意到原图是二分图,因此所有环都是偶环。一个重要观察是,我们可以让所有环的长度都是2,因为总可以把长度\(>2\)的环拆成若干个二元环,而且在\(S_{i,j}\geq2\)的限制......
  • Random Walk Problem
    Notice:ThisarticleisjustashortdiscussiononRandomWalkproblem,Icompute\(E(X^2)\)inthisarticle.AfterIreadsomematerials,fromaprogrammer'sperspective,IhavefoundthatthisproblemisnotjustsimpleasIthink,andit's......
  • An Easy Problem(二分)
    GDCPCA题原题链接:https://cpc.csgrandeur.cn/csgoj/problemset/problem?pid=1168类似的题目及视频解释链接:题目:https://www.acwing.com/problem/content/description/4083/相关视频讲解https://www.acwing.com/video/3568/本题可以转化成二维数组,每一行数都是呈现递增的,所以......
  • Backdoor:Win32/Noancooe 使用IDA进行恶意软件分析
    Backdoor:Win32/Noancooe先看下微软官方怎么说这个恶意软件:DetectedbyMicrosoftDefenderAntivirusAliases:Trojan-Ransom.Win32.Foreign.muyq(Kaspersky)SummaryWindowsDefenderdetectsandremovesthisthreat.Thisthreatcangiveamalicioushackerunauthorize......
  • 【每日一题】Problem 653B. Bear and Compressing
    原题解决思路根据当前字符串的首字符进行深度递归即可误区字符串是从头开始匹配的,因此只需要对首字符进行替换#include<bits/stdc++.h>intdfs(std::map<char,std::vector<std::string>>&r,charc,intn,inttarget){if(n==target){retu......
  • 【每日一题】Problem 626B. Cards
    原题解决思路找规律对于n:0:0形式的,只有一种结果,是第一个元素对于m:n:t形式的,三个元素都是可能的对于1:n:0形式的,可以发现,第二种元素是永远不可能的1:n:0可以变成1:n-1:0和0:n-1:1,而这本质上还是1:n:0最终,该形式只有两种倒数第二形态,1:2:0,1:1:0(不考虑一......
  • 【每日一题】Problem 602B. Approximating a Constant Range
    原题解决思路设\([a_l,a_r]\)满足要求,而加入\(a_{r+1}\)则不满足要求,那么根据题目中相邻两数差不超过1,\(a_{r+1}-min([a_l,a_r])=2\quador\quadmax([a_l,a_r])-a_{r+1}\)成立。当有多个\(a_i=(min([a_l,a_r])\quador\quadmax([a_l,a_r]))\)时,取最右边......
  • [AGC024F] Simple Subsequence Problem
    ProblemStatementYouaregivenaset$S$ofstringsconsistingof0and1,andaninteger$K$.Findthelongeststringthatisasubsequenceof$K$ormoredifferentstringsin$S$.Iftherearemultiplestringsthatsatisfythiscondition,findthelexic......