1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define maxn 200005 #define maxm 4005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> using namespace std; inline void read(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; int a[maxm][maxm],n,m,p,T; int fa[maxn][21],Max[maxn][21],lg[maxn],deep[maxn]; struct zyq{ int to,w; zyq(int a=0,int b=0) {to=a;w=b;} }; int ecnt; vector<zyq>to[200005]; struct node{ int x,y,id; node (int a=0,int b=0,int c=0) { x=a;y=b;id=c; } }g[maxn]; vector<node>e[4000005]; inline void dfs(int x,int pre,int w) { int i;deep[x]=deep[pre]+1;fa[x][0]=pre;Max[x][0]=w; for (i=1;i<=20;i++) { fa[x][i]=fa[fa[x][i-1]][i-1]; Max[x][i]=max(Max[x][i-1],Max[fa[x][i-1]][i-1]); } for (auto tmp:to[x]) if (tmp.to^pre) dfs(tmp.to,x,tmp.w); } inline int query(int x,int y) { int ans=0,i; if (deep[x]<deep[y]) swap(x,y); while (deep[x]>deep[y]) ans=max(ans,Max[x][lg[deep[x]-deep[y]]]),x=fa[x][lg[deep[x]-deep[y]]]; if (x==y) return ans; for (i=lg[deep[x]];i>=0;i--) if (fa[x][i]^fa[y][i]) { ans=max(ans,max(Max[x][i],Max[y][i])); x=fa[x][i],y=fa[y][i]; } return max(ans,max(Max[x][0],Max[y][0])); } int fx[5]={0,1,-1,0,0},fy[5]={0,0,0,1,-1}; int vis[maxm][maxm],dis[maxm][maxm]; int father[maxn]; inline int getfa(int x) {return x==father[x]?x:father[x]=getfa(father[x]);} inline bool cmp(node x,node y) {return x.id<y.id;} queue<node>q; signed main(void){ int i,j,k,xx,yy,x,y,z;char ch; read(n);read(m);read(p);read(T); for (i=1;i<=n;i++) { ch=getchar();while (ch!='.'&&ch!='#') ch=getchar(); for (j=1;j<=m;j++) a[i][j]=(ch=='#'),ch=getchar(); } for (i=2;i<=p;i++) lg[i]=lg[i/2]+1; for (i=1;i<=p;i++) read(g[i].x),read(g[i].y),g[i].id=i,q.push(node(g[i].x,g[i].y,i)),vis[g[i].x][g[i].y]=i; for (i=1;i<=p;i++) father[i]=i; while (!q.empty()) { auto tmp=q.front();q.pop();
for (i=1;i<=4;i++) { xx=tmp.x+fx[i];yy=tmp.y+fy[i]; if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&!a[xx][yy]) { if (vis[xx][yy]) { x=tmp.id,y=vis[xx][yy];
assert(dis[xx][yy]+dis[tmp.x][tmp.y]<=4000000); e[dis[xx][yy]+dis[tmp.x][tmp.y]].push_back(node(x,y)); } else vis[xx][yy]=tmp.id,dis[xx][yy]=dis[tmp.x][tmp.y]+1,q.push(node(xx,yy,tmp.id)); } } } for (i=0;i<=n*m;i++) { for (auto tmp:e[i]) { x=tmp.x,y=tmp.y,z=i; xx=getfa(x),yy=getfa(y); if (xx^yy) { to[x].push_back(zyq(y,z)); to[y].push_back(zyq(x,z)); father[xx]=yy; } } } for (i=1;i<=p;i++) if (father[i]==i) dfs(i,0,0); while (T--) { read(x);read(y); if (getfa(x)!=getfa(y)) puts("-1"); else printf("%d\n",query(x,y)); } return 0; }
|