并查集(逆序处理):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 < ...
随机推荐
- Python_oldboy_自动化运维之路(一)
python简介: Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...
- junit 测试注解
* @Test: 将一个 普通的方法修饰成为一个测试方法* @BeforeClass: 他会在所有的方法运行前被执行,static修饰* @AfterClass 他会在所有方法运行结束后被执行,sta ...
- Quartz Quick Start Guide
Welcome to the QuickStart guide for Quartz. As you read this guide, expect to see details of: Downlo ...
- jquery实现很简单的DIV拖动
今天用jquery实现了一个很简单的拖动...实现思路很简单 如下: 在thickbox 弹出层内实现拖拽DIV,那么得进行一下相对宽高的运算:必须加上相对于可见窗口宽高和弹出层宽高之间的差: ...
- 浅谈html5某些新元素的用途
大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...
- SGU 174.wall
题意: 判断给出的线段是否组成了多边形. Solution: 简单题,并查集+hash 这里用map实现 code #include <iostream> #include <cst ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- jquery插件dataTables自增序号。
dataTables官网提供了一种方式,使用后没有达到预期效果(js报错),没有深究原因.如果需要,可以按照下面的方式来. $('#dataList').dataTable({ "langu ...
- 使用jQuery实现跨域提交表单数据
我们在WEB开发中有时会遇到这种情况,比如要从A网站收集用户信息,提交给B网站处理,这个时候就会涉及到跨域提交数据的问题.本文将给您介绍如何使用jQuery来实现异步跨域提交表单数据. 在jQue ...
- 理解Python的*args, **kwargs
1 # coding=utf-8 2 def speak(*args, **kwargs): 3 print args, kwargs 4 5 6 a = 1 7 b = 2 8 c = 3 9 sp ...