ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
http://hihocoder.com/problemset/problem/1291
前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时间看这一题。不过打过之前一场BestCoder的应该都会有点思路,虽然BC那题是二维,这题是三维的,但是思路应该是一样的,没错,就是离线加并查集。
正过来考虑的时候,发现第一个要求相邻块是好处理的,但是第二个要求能否到达(1000, 1000, 1000)这个条件似乎比较难判断,当时BC上的题根据题意是可以二分加搜索,但是这题的条件是不能二分的。
离线并查集的话,比较好想(如果做过BC那题的话),就是先算上所有块,搜索一遍全图,把非块的用并查集联通起来。然后倒着来拆块,看要拆的块是否和边界(这里取了(0, 0, 1)这个边界非块)的非块联通(有公共的根),然后将拆掉块的地方和周围非块的地方联通。直到某一处的块与边界不联通,那么就是No。否则最后就是Yes。
本地用dfs爆栈了,改bfs应该可以解决。不过直接交上去,评测机用dfs不会爆栈。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <vector> using namespace std; const int maxN = ;
const int maxX = ;
int ufs[**];
int n, x[maxN], y[maxN], z[maxN];
int xx[] = {-, , , , , };
int yy[] = { , , -, , , };
int zz[] = { , , , , -, }; int findRoot(int x)
{
if (ufs[x] == x) return x;
int fa = findRoot(ufs[x]);
ufs[x] = fa;
return fa;
} void mergeUfs(int x, int y)
{
int fx, fy;
fx = findRoot(x);
fy = findRoot(y);
ufs[fx] = fy;
} bool inSameUfs(int x, int y)
{
int fx, fy;
fx = findRoot(x);
fy = findRoot(y);
if (fx == fy) return true;
else return false;
} inline int Hash(int x, int y, int z)
{
return x*maxX*maxX+y*maxX+z;
} void dfs(int x, int y, int z)
{
int t = Hash(x, y, z), tt;
if (ufs[t] == - || ufs[t] != -) return;
if (ufs[t] == -) ufs[t] = t;
for (int i = ; i < ; ++i)
{
if (x+xx[i] < || y+yy[i] < || z+zz[i] <= ) continue;
if (x+xx[i] >= maxX || y+yy[i] >= maxX || z+zz[i] >= maxX) continue;
tt = Hash(x+xx[i], y+yy[i], z+zz[i]);
if (ufs[tt] != -) continue;
dfs(x+xx[i], y+yy[i], z+zz[i]);
mergeUfs(t, tt);
}
} bool judge(int x, int y, int z)
{
bool flag = false;
int t, tt, to;
for (int i = ; i < ; ++i)
{
tt = Hash(x+xx[i], y+yy[i], z+zz[i]);
if (ufs[tt] == -) flag = true;
}
if (!flag) return false; flag = false;
t = Hash(x, y, z);
to = Hash(, , );
ufs[t] = t;
for (int i = ; i < ; ++i)
{
tt = Hash(x+xx[i], y+yy[i], z+zz[i]);
if (ufs[tt] == -) continue;
if (findRoot(to) == findRoot(tt)) flag = true;
mergeUfs(t, tt);
}
return flag;
} void input()
{
memset(ufs, -, sizeof(ufs));
int t;
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
scanf("%d%d%d", &x[i], &y[i], &z[i]);
t = Hash(x[i], y[i], z[i]);
ufs[t] = -;
}
for (int i = ; i < maxX; ++i)
for (int j = ; j < maxX; ++j)
{
t = Hash(i, j, );
ufs[t] = -;
}
for (int k = ; k < maxX; ++k)
for (int i = ; i < maxX; ++i)
for (int j = ; j < maxX; ++j)
dfs(i, j, k);
} void work()
{
for (int i = n-; i >= ; i--)
{
if (!judge(x[i], y[i], z[i]))
{
printf("No\n");
return;
}
}
printf("Yes\n");
} int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
input();
work();
}
return ;
}
ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)的更多相关文章
- hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护
/** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...
- ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)
http://hihocoder.com/problemset/problem/1139 这题提示上写的是二分,但是感觉不二分应该也可以,至少题目是AC的... 二分的思想就是二分答案的值,看能不能在 ...
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- ACM学习历程—Hihocoder 1288 Font Size(暴力 || 二分)
http://hihocoder.com/problemset/problem/1288 这题是这次微软笔试的第一题,关键的是s的上限是min(w, h),这样s的范围只有到1000,这样就可以直接暴 ...
- ACM学习历程—Hihocoder [Offer收割]编程练习赛1
比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...
- ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strange storehouse in PKU. In this ...
- ACM学习历程—Hihocoder 1164 随机斐波那契(数学递推)
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 大家对斐波那契数列想必都很熟悉: a0 = 1, a1 = 1, ai = ai-1 + ai-2,(i > 1). ...
- ACM学习历程—Hihocoder 1178 计数(位运算 && set容器)(hihoCoder挑战赛12)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召 ...
随机推荐
- PAT 天梯赛 L1-034. 点赞 【MAP】
题目链接 https://www.patest.cn/contests/gplt/L1-034 AC代码 #include <cstdio> #include <cstring> ...
- delphi 中 unicode 转汉字 函数
近期用到这个函数,无奈没有找到 delphi 自带的,网上找了下 有类似的,没有现成的,我需要的是 支持 “\u4f00 ” 这种格式的,即前面带标准的 “\u” 于是改造了一下. 下面是 解码 函 ...
- 跨平台移动开发 Xuijs超轻量级的框架 Dom与Event简洁代码实现文本展开收起
Dom与Event简洁代码实现文本展开收起 Xuijs超轻量级的框架 Dom与Event实现文本展开收起 效果图 示例代码 <!DOCTYPE html PUBLIC "-//W3C/ ...
- library-type:fr-unstanded vs fisrt-stand vs second-stanrd
建库时是否是链特异性建库. Tophat2: --library-type The default is unstranded (fr-unstranded). If either fr-firsts ...
- awk之腾迅面试题1
1.题目如下: 3 5 6 72 3 1 04 5 6 92 3 4 42 2 1 04 5 0 9假如把2列和3列的值作为新的第5列,第5列的平均值为avg5,求第5列中大于avg5的行数. aw ...
- maven 中pom.xml文件依赖包从本地加载如何配置?
比如我现在有一个需求是:项目中要加载ueditor的jar架构包,并且用maven构建的项目 那么在pom.xml文件中如配置: 说明:${project.basedir} 是maven 自带(内置) ...
- java基础(2)-面向对象(1)
面向对象 面向对象思想 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程:强调的是功能行为 面向对象:将功能封装进对象,强调具备了功能的对象 面向对象是基于面向过程的 面向对象举例 ...
- Codeforces Round #371 (Div. 2) A ,B , C 水,水,trie树
A. Meeting of Old Friends time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 配置网卡为vlan trunk
http://www.microhowto.info/tutorials/802.1q.html Configure an Ethernet interface as a VLAN trunk hos ...
- DB处理大量数据处理日志报错问题
因为当插入.更新或删除大批量数据的时候,有时候会出现事务日志满的问题,所以解决步骤 1.连接到当前数据库 db2 connect to uppdb 2.查看数据库配置文件 db2 get db cf ...