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能召 ...
随机推荐
- Python3 进程 线程 同步锁 线程死锁和递归锁
进程是最小的资源单位,线程是最小的执行单位 一.进程 进程:就是一个程序在一个数据集上的一次动态执行过程. 进程由三部分组成: 1.程序:我们编写的程序用来描述进程要完成哪些功能以及如何完成 2.数据 ...
- $《第一行代码:Android》读书笔记——第6章 数据持久化
主要讲述了Android数据持久化的三种方式:文件存储.SharedPreference存储.SQLite数据库存储. (一)文件存储 其实Android中文件存储方式和Java的文件操作类似,就是用 ...
- octotree神器 For Github and GitLab 火狐插件
Code tree for GitHub and GitLabExtension to show code tree for GitHub and GitLab. Useful for develop ...
- 【转载】linux获取mac地址
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...
- Windows下MarialDB使用
命令行控制启动和关闭:mysqld --console #这样启动ctrl+c即为关闭 启动:双击mysqld.exe即可 #此为后台启动 关闭:mysqladmin -uroot -pr ...
- com.android.dex.DexIndexOverflowException: Cannot merge new index 66299 into a non-jumbo instruction
打包时控制台输出: Error:Execution failed for task ':app:transformClassesWithDexForAll32Release'. > com.an ...
- 跨平台移动开发 Adobe Edge制作HTML5圣诞音乐贺卡DEMO
1.新建项目 2.添加背景,图片,音频 var au_to_play=new Audio(); au_to_play.src="audio/lap.mp3"; //指定文件名,这里 ...
- node拦截器设置
node的拦截器主要目的是用户登录的时候为用户存了一个session,用户登录后的其他操作都要经过拦截器,对比session的值,并把session的过期时间延长. 拦截器主要是在路由文件routes ...
- Cocos2d-x项目移植到WP8系列之五:播放MP3
原文链接: http://www.cnblogs.com/zouzf/p/3972549.html 这一块的细节还是不太了解,只是东凑西拼能跑起来而已 1.网上下载lamb库 生成需要的lib库,详情 ...
- vm+ubuntu联网
在vm下刚装了ubuntu,就是上不了网,确认以下配置后方可以 1.我的电脑开机自动把VM的相关服务都关闭了,需要手动打开 在控制面板中搜索服务,手动启动vm服务 2.在适配器里启用vm网卡 3.使用 ...