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 ...
随机推荐
- ARTS第六周打卡
Algorithm : 做一个 leetcode 的算法题 1.合并两个排序链表 2.树的子结构 3.二叉树的镜像 4.包含Min函数的栈 5.栈的压入.弹出 6.二叉搜索树的后序遍历 7.从上往下打 ...
- java poi3.10.1基本excel使用
效果: 代码: //时间 String checkTime = "yyyy/MM/dd"; //人员查询 Set preParticipantNames = new HashSet ...
- PAT(B) 1013 数素数(Java)
题目链接:1013 数素数 代码 /** * Score: 20 * Run Time: 124ms * @author wowpH * @version 1.0 */ import java.uti ...
- 1231: 删除字符串中指定的字符(Java)
WUSTOJ 1231: 删除字符串中指定的字符 题目 原题链接 Description 明天就要英语考试了,小明明正在挑灯夜战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词 ...
- S03_CH12_基于UDP的QSPI Flash bin文件网络烧写
S03_CH12_基于UDP的QSPI Flash bin文件网络烧写 12.1概述 为了满足不同的需求,本例程在"基于TCP的QSPI Flash bin文件网络烧写"上进行修改 ...
- 跟我一起学编程—《Scratch编程》第24课:幸运大转盘
同学你好,欢迎来到<跟我一起学编程>,我是包老师.这是<Scratch3.0编程>课程的第24课,我这节课教你做一个抽奖游戏:幸运大转盘. 学习目标: 1. 能够熟练使用造型工 ...
- MogileFS表说明
MogileFS大致的表说明如下 checksum:用来存放文件的校验和class:文件分类定义device:主机上的可用设备定义,包括设备可用空间,使用的权重等信息domain:域定义信息file: ...
- Lieges of Legendre CodeForces - 603C (博弈论,SG找规律)
大意: 给定$n$堆石子, 两人轮流操作, 每次操作两种选择 $(1)$任选非空堆拿走一个石子 $(2)$任选石子数为$2x(x>0)$的一堆, 替换为$k$堆$x$个石子. ($k$给定) 最 ...
- mysql 查询字段为空显示默认值
IFNULL() 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值. IFNULL() 函数语法格式为: IFNULL(exp ...
- 判断pdf文件是否正常可用
之前在下载港交所的文件的时候会碰到有些文件异常的情况,文件下载下来,但是不能打开.查到其他的方法不是判断错误就是很麻烦. 整理出一个非常简单的方法,将文件以二进制方式打开,然后判断文件的开头是否符 ...