HDU 5652 India and China Origins 二分+并查集
India and China Origins
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5652
Description
A 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.
Input
There are multi test cases. the first line is a sinle integer T which represents the number of test cases.
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
Output
Single line at which year the communication got cut off.
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.
Sample Input
1
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
Sample Output
4
Hint
题意
给一个n*m的图
然后1表示有障碍物,中国在最上面,印度在最下面
然后有q个变化,每次变化都会有一个地方从0变成障碍物
问你最早什么时候,中国和印度不相连了。
题解:
比较显然的就是二分+判断是否连通
判断是否连通用并查集和bfs都可以
反正都是O(n)嘛
代码
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
const int maxn = 505;
int p[505*505],n,m,q;
char s[maxn][maxn];
int vis[maxn][maxn];
pair<int,int>d[maxn*maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int fi(int x)
{
return p[x]==x?x:p[x]=fi(p[x]);
}
void uni(int p1,int q1)
{
int x=fi(p1);
int y=fi(q1);
if(x!=y)
p[x]=y;
}
int check(int t)
{
for(int i=0;i<=n*m+1;i++)p[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[i][j]=='1')vis[i][j]=1;
else vis[i][j]=0;
for(int i=1;i<=t;i++)
vis[d[i].first][d[i].second]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int k=0;k<4;k++)
{
if(vis[i][j]==1)continue;
int xx = i+dx[k];
int yy = j+dy[k];
if(yy<=0||yy>m)continue;
if(xx==0)uni((i-1)*m+j,0);
else if(xx==n+1)uni((i-1)*m+j,n*m+1);
else
{
if(vis[xx][yy]==1)continue;
uni((i-1)*m+j,(xx-1)*m+yy);
}
}
}
}
int x = fi(0);
int y = fi(n*m+1);
if(x==y)return 1;
return 0;
}
void solve()
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&d[i].first,&d[i].second);
d[i].first++,d[i].second++;
}
if(check(q)){
printf("-1\n");
return;
}
int l = 0,r = q,ans = 0;
while(l<=r)
{
int mid = (l+r)/2;
if(!check(mid))r=mid-1,ans=mid;
else l=mid+1;
}
printf("%d\n",ans);
}
int main()
{
int t;scanf("%d",&t);
while(t--)solve();
}
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 二分+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 < ...
- HDU 5652 India and China Origins 二分优化+BFS剪枝
题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...
- 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 Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- 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 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
随机推荐
- Java八种基本类型
boolean 二进制位: true ,false byte 二进制位:8 -128 - 127 -2的7次方到2的7次方-1 char 二进制位:16 0 - 65535 short 二 ...
- windows环境下搭建Redis集群
转载请注明出处,原文章地址: https://www.cnblogs.com/tommy-huang/p/6240083.html Redis集群: 如果部署到多台电脑,就跟普通的集群一样:因为Red ...
- 在Linux(CentOS)中安装.netcore SDK
官方链接 :https://dotnet.microsoft.com/download/linux-package-manager/centos/sdk-current 可以直接根据官方链接,选择Li ...
- elk系列5之syslog的模块使用【转】
preface rsyslog是CentOs系统自带的的一个日志工具,那么我们就配置logstash来接受rsyslog的日志. logstash的syslog模块 linux-node2上操作log ...
- Mysql存储之ORM框架SQLAlchemy(一)
上一篇我们说了mysql存储的原生语句方式,因为原生语句每次写都比较的复杂,所以这里我们说一种引用实体类的方式来操作数据库. 什么是ORM ORM技术:Object-Relational Mappin ...
- 关于Java代码优化的35条建议
代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...
- 域名 DNS命令——dig
dig命令详解 1.查看域名的A记录 # dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.e ...
- C/C++面试题目一
C/C++开发工程师面试题目(一)(附答案分析) 推荐:自己根据在面试中碰到做过的一些题目以及总结的题目,希望对面试的同学有所帮助. 一. 选择题 1. 下列类中( )不是输入输出流类iostrea ...
- go的匿名组合
golang也提供了继承机制,但采用组合的文法,因此称为匿名组合.与其他语言不同, golang很清晰地展示出类的内存布局是怎样的. 一 非指针方式的组合 1)基本语法 type base stru ...
- for循环练习题(共六道题)
第一题: 假设一个简单的ATM机的取款过程是这样的:首先提示用户输入密码(password),最多只能输入三次,超过3次则提示用户“密码错误,请取卡”结束交易.如果用户密码正确,再提示用户输入取款金额 ...