您的位置:首页 > 其它

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

2017-10-26 17:10 387 查看
传送门

题意:给你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();
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐