标签:
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define mod 1000000007 int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } int l,k; char a[510][510]; struct is { int l,r; }ch[250010]; int father[500010]; int vis[510][510]; int xz[4]={1,0,-1,0}; int yz[4]={0,1,0,-1}; int find_father(int x) { return x==father[x]? x:father[x]=find_father(father[x]); } void hebing(int x,int y) { int xx=find_father(x); int yy=find_father(y); if(xx!=yy) father[xx]=yy; } int number(int x,int y) { return (x-1)*k+y; } int check(int ff) { memset(vis,0,sizeof(vis)); for(int i=0;i<=l*k;i++) father[i]=i; father[399999]=399999; father[400000]=400000; for(int i=1;i<=l;i++) for(int t=1;t<=k;t++) if(a[i][t]==‘1‘) vis[i][t]=1; else vis[i][t]=0; for(int i=1;i<=ff;i++) vis[ch[i].l+1][ch[i].r+1]=1; for(int i=1;i<=l;i++) for(int t=1;t<=k;t++) { if(vis[i][t])continue; for(int j=0;j<4;j++) { int xx=i+xz[j]; int yy=t+yz[j]; if(yy<=0||yy>k||vis[xx][yy])continue; if(xx==0)hebing(number(i,t),399999); else if(xx==l+1)hebing(number(i,t),400000); else hebing(number(i,t),number(xx,yy)); } } int ans1=find_father(399999); int ans2=find_father(400000); if(ans1==ans2) return 1; return 0; } int main() { int x,y,i,t; scanf("%d",&x); while(x--) { scanf("%d%d",&l,&k); for(i=1;i<=l;i++) scanf("%s",a[i]+1); scanf("%d",&y); for(t=1;t<=y;t++) scanf("%d%d",&ch[t].l,&ch[t].r); if(!check(0)) { printf("-1\n"); continue; } int st=1,en=y,mid; while(st<en) { mid=(st+en)>>1; if(check(mid)) st=mid+1; else en=mid; } printf("%d\n",st); } return 0; }
hdu 5652 India and China Origins 并查集+二分
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5352386.html