CF46F Hercule Poirot Problem
题意:
有n个房间和m扇门,每扇门有且仅有一把钥匙
有k个人度过了两天,在第一天开始的时候所有的门都是关闭的,在第二天结束的时候,所有的门也都是关闭的
在这两天内,每个人可以执行如下操作若干次:
关上一扇门(前提:他有这扇门的钥匙,且这扇门与当前房间相连)
打开一扇门(前提:他有这扇门的钥匙,且这扇门与当前房间相连)
将自己手上的一些钥匙给与他同处于一间房里的其他人
走向一个与当前房间相连、且门是打开的房间
给出第一天开始前和第二天结束后每个人的位置及拥有的钥匙
判断是否可行
题解:
直接想不太好做。
转化一下:
可以发现,如果有解,一定可以把能打开的门都打开,在关回去。
而开和关是互逆的,所以,可以理解为两天都把能打开的门都打开。
如果都打开后,两天结果一样,则有解。
一样就是指打开的门相同,每个房间,人,钥匙所属集合分别相同。
这样就好做了。
暴力枚举能打开的边,用并查集维护每个集合的钥匙,暴力合并。
可以发现,枚举\(m\)次就够了。每次暴力合并复杂度为\(O(m)\),总时间复杂度为\(O(m^2)\)。
代码:
#include <stdio.h>
#include <map>
using namespace std;
#define ull unsigned long long
#define se 13131
int u[1005],v[1005],fa[1005],n,m,k;
bool zt[1005][1005];
int getv(int x) {
if (x == fa[x]) return x;
fa[x] = getv(fa[x]);
return fa[x];
}
void merge(int x, int y) {
x = getv(x);
y = getv(y);
if (x == y) return;
if (x < y) {
int t = x;
x = y;
y = t;
}
fa[x] = y;
for (int i = 1; i <= m; i++) {
if (zt[x][i]) zt[y][i] = true;
}
}
ull ha(char zf[1005]) {
ull rt = 0;
for (int i = 0; zf[i] != 0; i++) rt = rt * se + zf[i];
return rt;
}
map < ull,int > mp;
char zf[1005];
int wz[1005],rw[1005],yw[1005];
int w2[1005],r2[1005],y2[1005];
bool b1[1005],b2[1005];
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) scanf("%d%d", &u[i], &v[i]);
for (int i = 1; i <= k; i++) {
int a,s;
scanf("%s%d%d", zf, &a, &s);
rw[i] = a;
mp[ha(zf)] = i;
for (int j = 0; j < s; j++) {
int b;
scanf("%d", &b);
zt[a][b] = 1;
yw[b] = a;
}
}
for (int a = 1; a <= m; a++) {
for (int i = 1; i <= m; i++) {
if (!b1[i] && (zt[getv(u[i])][i] || zt[getv(v[i])][i])) {
merge(u[i], v[i]);
b1[i] = true;
}
}
}
for (int i = 1; i <= n; i++) wz[i] = getv(i);
for (int i = 1; i <= m; i++) yw[i] = getv(yw[i]);
for (int i = 1; i <= k; i++) rw[i] = getv(rw[i]);
for (int i = 1; i <= n; i++) {
fa[i] = i;
for (int j = 1; j <= m; j++) zt[i][j] = 0;
}
for (int i = 1; i <= k; i++) {
int a,s;
scanf("%s%d%d", zf, &a, &s);
r2[mp[ha(zf)]] = a;
for (int j = 0; j < s; j++) {
int b;
scanf("%d", &b);
zt[a][b] = 1;
y2[b] = a;
}
}
for (int a = 1; a <= m; a++) {
for (int i = 1; i <= m; i++) {
if (!b2[i] && (zt[getv(u[i])][i] || zt[getv(v[i])][i])) {
merge(u[i], v[i]);
b2[i] = true;
}
}
}
for (int i = 1; i <= n; i++) w2[i] = getv(i);
for (int i = 1; i <= m; i++) y2[i] = getv(y2[i]);
for (int i = 1; i <= k; i++) r2[i] = getv(r2[i]);
bool zd = false;
for (int i = 1; i <= n; i++) {
if (wz[i] != w2[i]) zd = true;
}
for (int i = 1; i <= n; i++) {
if (rw[i] != r2[i]) zd = true;
}
for (int i = 1; i <= n; i++) {
if (yw[i] != y2[i]) zd = true;
}
for (int i = 1; i <= m; i++) {
if (b1[i] != b2[i]) zd = true;
}
if (zd) printf("NO");
else printf("YES");
return 0;
}
CF46F Hercule Poirot Problem的更多相关文章
- Codeforces 图论题板刷(2000~2400)
前言 首先先刷完这些在说 题单 25C Roads in Berland 25D Roads not only in Berland 9E Interestring graph and Apples ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
随机推荐
- Redis慢日志取出来
http://blog.chinaunix.net/uid-31396856-id-5758295.htmlhttps://blog.51cto.com/legehappy/2151986?sourc ...
- (三)easyUI之树形组件
一.同步树 1.1 概念 所有节点一次性加载完成 1.2 案例 1.2.1 数据库设计 1.2.2 编码 index.jsp <%@ page language="java" ...
- (四)springmvc之获取servlet原生对象
一.使用DI注入的方式 <a href="<%=request.getContextPath()%>/servletObj_1">DI注入的方式</a ...
- C#使用HtmlAgilityPack快速爬虫
HtmlAgilityPack真是一把网抓利器,可以迅速地从网页抓到想要的文本或数据,使用起来十分方便,引用时在NuGet安装添加并在头部引用using HtmlAgilityPack;即可. 针对网 ...
- rabbitMQ 安装,基于Windows环境
参考文章:https://www.cnblogs.com/ericli-ericli/p/5902270.htmlRabbit MQ 是建立在Erlang OTP平台上,安装前需先安装Erlang.h ...
- Marketing Cloud的contact merge机制
Marketing Cloud的contact支持多种多样的数据源,如下图所示: SAP Hybris Commerce SAP ERP SAP Cloud for Customer SAP Gigy ...
- 案例:selenium实现登录处理弹窗
func.py https://www.cnblogs.com/andy9468/p/10899508.html main.py中 # 导入webdriver import os import tim ...
- Image Processing and Analysis_15_Image Registration:Multi-modal volume registration by maximization of mutual information——1996
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- zabbix low-level discovery 监控mysql
当一台服务器上MySQL有多个实例的时候,MySQL占用多个不同端口.利用zabbix的low-level discovery可以轻松监控. 思路参考:http://dl528888.blog.51c ...
- 解决Centos /boot过小无法更新内核
Centos7默认安装时,/boot目录设置只有150M左右,这样编译几个版本的内核/boot空间就不够用了.报错大致如下: Disk Requirements: At least 3MB more ...