# [bzoj4242]水壶【网格图曼哈顿最小生成树+树上倍增】

## bzoj4242 水壶

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<vector>
using namespace std;
#define fastcall __attribute__((optimize("-O3")))
#define IL __inline__ __attribute__((always_inline))
{
int s=0,w=1;
char c=getchar();
while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}
while(isdigit(c)){s=(s<<3)+(s<<1)+c-'0',c=getchar();}
x=s*w;
}
fastcall inline void write(int x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
const int MAXN=2010;
const int MAXM=200010;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
struct edge
{
int to,nxxt,w;
}e[MAXM<<1];
fastcall IL void add(int x,int y,int z)
{
e[++cnt].to=y;
e[cnt].w=z;
}
#define mp make_pair
#define pb push_back
vector<pair<int,int> >v[MAXN*MAXN];
pair<int,int> Q[MAXN*MAXN<<1];
int front=1,rear=0,n,m,p,T;
char mapp[MAXN][MAXN];
pair<int,int>vis[MAXN][MAXN];
fastcall IL bool check(int x,int y)
{
return (x>=1&&x<=n&&y>=1&&y<=m);
}
fastcall IL void bfs()
{
while(front<=rear)
{
pair<int,int>now=Q[front++];
for(register int k=0;k<4;k++)
{
int nowx=now.first+dx[k],nowy=now.second+dy[k];
if(!check(nowx,nowy))continue ;
if(mapp[nowx][nowy]=='#')continue ;
if(!vis[nowx][nowy].second)
vis[nowx][nowy]=mp(vis[now.first][now.second].first+1,vis[now.first][now.second].second),Q[++rear]=mp(nowx,nowy);
else if(vis[nowx][nowy].second^vis[now.first][now.second].second)
v[vis[now.first][now.second].first+vis[nowx][nowy].first].pb(mp(vis[now.first][now.second].second,vis[nowx][nowy].second));
}
}
}
int fafa[MAXM],deep[MAXM],fa[MAXM][19],len[MAXM][19];
fastcall inline int find(int x){return (x==fafa[x])?x:fafa[x]=find(fafa[x]);}
fastcall IL void un(int x,int y)
{
x=find(x),y=find(y);
if(x==y)return ;
fafa[x]=y;
}
fastcall IL void kruskal()
{
for(register int i=0;i<=n*m;i++)
{
for(register unsigned int j=0;j<v[i].size();j++)
{
if(find(v[i][j].first)^find(v[i][j].second))
{
un(v[i][j].first,v[i][j].second);
}
}
}
}
fastcall void init(int u,int father,int w)
{
fa[u][0]=father;
deep[u]=deep[father]+1;
len[u][0]=w;
for(register int i=1;i<=18;i++)
fa[u][i]=fa[fa[u][i-1]][i-1],len[u][i]=max(len[u][i-1],len[fa[u][i-1]][i-1]);
{
if(e[i].to==father)continue ;
init(e[i].to,u,e[i].w);
}
}
fastcall IL int solve(int u,int v)
{
if(find(u)^find(v))return -1;
int ans=0;
if(deep[u]<deep[v])swap(u,v);
for(register int i=18;~i;i--)
if(deep[fa[u][i]]>=deep[v])
{
ans=max(ans,len[u][i]);
u=fa[u][i];
}
if(u==v)return ans;
for(register int i=18;~i;i--)
{
if(fa[u][i]^fa[v][i])
{
ans=max(ans,max(len[u][i],len[v][i]));
u=fa[u][i],v=fa[v][i];
}
}
return max(ans,max(len[u][0],len[v][0]));
}
fastcall int main()
{
for(register int i=1;i<=p;i++)fafa[i]=i;
for(register int i=1;i<=n;i++)
{scanf("%s",mapp[i]+1);}
for(register int i=1;i<=p;i++)
{
int x,y;
Q[++rear]=mp(x,y),vis[x][y]=mp(0,i);
}
bfs();
kruskal();
for(register int i=1;i<=p;i++)
if(find(i)==i)init(i,0,0);
while(T--)
{
int u,v;
write(solve(u,v)),putchar(10);
}
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120