并查集(逆序处理):HDU 5652 India and China Origins
India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 676 Accepted Submission(s): 227
long time ago there are no himalayas between India and China, the both
cultures are frequently exchanged and are kept in sync at that time, but
eventually himalayas rise up. With that at first the communation
started to reduce and eventually died.
Let's
assume from my crude drawing that the only way to reaching from India
to China or viceversa is through that grid, blue portion is the ocean
and people haven't yet invented the ship. and the yellow portion is
desert and has ghosts roaming around so people can't travel that way.
and the black portions are the location which have mountains and white
portions are plateau which are suitable for travelling. moutains are
very big to get to the top, height of these mountains is infinite. So if
there is mountain between two white portions you can't travel by
climbing the mountain.
And at each step people can go to 4 adjacent positions.
Our
archeologists have taken sample of each mountain and estimated at which
point they rise up at that place. So given the times at which each
mountains rised up you have to tell at which time the communication
between India and China got completely cut off.
For each test case, the first line contains two space seperated integers N,M. next N lines consists of strings composed of 0,1 characters. 1 denoting that there's already a mountain at that place, 0 denoting the plateau. on N+2 line there will be an integer Q denoting the number of mountains that rised up in the order of times. Next Q lines contain 2 space seperated integers X,Y denoting that at ith year a mountain rised up at location X,Y.
T≤10
1≤N≤500
1≤M≤500
1≤Q≤N∗M
0≤X<N
0≤Y<M
print -1 if these two countries still connected in the end.
Hint:
From the picture above, we can see that China and India have no communication since 4th year.
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int fa[maxn];
bool out[maxn];
int X[maxn],Y[maxn];
int map[][];
char s[];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} int main(){
int T,R,C,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&R,&C);
for(int i=C+;i<=(R+)*C;i++)fa[i]=i;
for(int i=;i<=C;i++)fa[i]=;
for(int i=C*(R+)+;i<=C*(R+);i++)fa[i]=C*(R+)+; for(int i=;i<=R;i++){
scanf("%s",s+);
for(int j=;j<=C;j++)
map[i][j]=s[j]-'';
} scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&X[i],&Y[i]);X[i]++;Y[i]++;
map[X[i]][Y[i]]=;
} for(int i=;i<=R;i++)
for(int j=;j<C;j++){
if(map[i][j])continue;
if(map[i-][j]==)fa[find((i-)*C+j)]=find(i*C+j);
if(map[i+][j]==)fa[find((i+)*C+j)]=find(i*C+j);
if(map[i][j+]==)fa[find(i*C+j+)]=find(i*C+j);
if(map[i][j-]==)fa[find(i*C+j-)]=find(i*C+j);
} int ans=;
for(int i=m;i>=;i--){
if(find()==find(C*(R+)+)){
ans=i+;
break;
}
map[X[i]][Y[i]]=;
if(map[X[i]-][Y[i]]==)fa[find((X[i]-)*C+Y[i])]=find(X[i]*C+Y[i]);
if(map[X[i]+][Y[i]]==)fa[find((X[i]+)*C+Y[i])]=find(X[i]*C+Y[i]);
if(map[X[i]][Y[i]+]==)fa[find(X[i]*C+Y[i]+)]=find(X[i]*C+Y[i]);
if(map[X[i]][Y[i]-]==)fa[find(X[i]*C+Y[i]-)]=find(X[i]*C+Y[i]);
}
if(ans==m+)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
并查集(逆序处理):HDU 5652 India and China Origins的更多相关文章
- hdu 5652 India and China Origins 并查集+二分
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5652 India and China Origins 二分+并查集
India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...
- HDU 5652 India and China Origins(并查集)
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- HDU 5652 India and China Origins(经典并查集)
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- (hdu)5652 India and China Origins 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
- hdu 5652 India and China Origins 二分+bfs
题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...
随机推荐
- Linux下长时间ping网络加时间戳并记录到文本(转)
[root@test ~]# ping 192.168.2.1 -c 10 PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.64 bytes f ...
- Eclipse清除SVN密码
1. Eclipse/Subclipse并不会记录用户名及密码, SVN密码是由SVN客户端(JavaHL or SVNKit)负责维持的 2. 可查看Eclipse的Preferences中SVN属 ...
- HTML5 文件域+FileReader 分段读取文件(四)
一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...
- SQL Server死锁日志各字段含义
使用跟踪标记 1204 --打开跟踪标记 DBCC TRACEON (1204,-1) --关闭跟踪标记 DBCC TRACEOFF (1204,-1) 处于死锁状态时,跟踪标记 1204 在等待的线 ...
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
http://blog.csdn.net/agileclipse/article/details/17161225 详情请点击链接查看
- 简单的SqlHelper
namespace Login { class SqlHelper { //连接数据库的字符串 //static string dataConnection = "server=***-PC ...
- magic_quotes_gpc、mysql_real_escape_string、addslashes的区别及用法
本篇文章,主要先重点说明magic_quotes_gpc.mysql_real_escape_string.addslashes 三个函数方法的含义.用法,并举例说明.然后阐述下三者间的区别.关系.一 ...
- css3基础教程十六变形与动画animation
前面我们讲过的变形与动画一般都是通过鼠标的单击.获得焦点,被点击或对元素进行一定改变后以后触发效果的,那么有没有像Flash一样自动播放的动画效果呢?答案当然是肯定的,这就是我们今天要讲到的anima ...
- 仿小米网jQuery全屏滚动插件fullPage.js
演 示 下 载 简介 如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面,QQ浏 ...
- 《service》-“linux命令五分钟系列”之二
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...