非诚勿扰

小明家门前有棵又高又大的苹果树,树上有好多好多的枝杈,许多枝杈上都住着蜗牛,每个蜗牛都有自己的编号,相邻的两蜗牛间的距离都为1。快过年了,蜗牛们无聊,就去“XX电影院”(广告位再次招租,详情请看1002买票)看电影,要看大片!在众多选择下,众蜗牛商议,为了帮助“非诚勿扰”票房冲4亿,大家集体捧“非诚勿扰”的场。

(此处省略n字)

电影是好看,但不少蜗牛回家后心里就嘀咕了:“咱也老大不小了,还没对象,要不咱也去相个亲吧,怎么说咱也是有房有车啊(蜗牛壳,和一路跑还流鼻涕状液体的‘拖拉机’)!”

那先找媒婆吧,媒婆的编号好象是,k号吧。蜗牛们虽说有车,但相当耗油,而且越早到媒婆那,就显得越有诚意(“非诚勿扰”嘛),蜗牛们想走最短的路线去媒婆那。给出苹果树地图和要去相亲的蜗牛编号和媒婆蜗牛编号,请大家帮忙算一下他们之间的最短距离。如下图:

0-1 相邻

0-3 相邻

0-4 相邻

1-2 相邻

1-5 相邻

4-6 相邻,相邻编号蜗牛的距离都是1

所以0-1之间的最短距离是1,1-3之间的最短距离是2,2-3之间的最短距离是3,5-6之间的最短距离是4。。。
在这里插入图片描述
Input

有多组数据,每组数据n+m+1行:第一行一个整数n(1<=n<=100)表示节点数;接下来n-1行,每行都有两个整数
a,b(0<=a,b<n),表示a,b相邻;接下来一行,一个整数m(1<=m<=500),表示询问个数;最后m行,
每行都有两个整数a, b(0<=a,b<n),表示要求最短距离的两个编号。

Output

每组输入对应一组输出,每组输出都有m行,每行一个输出,对应于输入的询问(保证有解)。

Sample Input

7
0 1
0 3
0 4
1 2
1 5
4 6
4
0 1
1 3
2 3
5 6

Sample Output

1
2
3
4
#include<bits/stdc++.h>
using namespace std;
const int MAXN=200005;
int stk[MAXN],top,nexts[MAXN],n,m,que,a1,b1;
struct tree
{
	int fa,ch[2],size,lazy;
}t[MAXN];
int nextss(int x,int y)
{
	if(x+y>n)return n+1;
	return x+y;
}
void update(int root)
{
	t[root].size=t[t[root].ch[0]].size+t[t[root].ch[1]].size+1;
	return;
}
void pushdown(int root)
{
	if(t[root].lazy)
	{
		swap(t[root].ch[0],t[root].ch[1]);
		t[root].lazy=0;
		t[t[root].ch[0]].lazy^=1;
		t[t[root].ch[1]].lazy^=1;
	}
	return;
}
bool isroot(int root)
{
	return root!=t[t[root].fa].ch[0]&&root!=t[t[root].fa].ch[1];
}
void rot(int root)
{
	int fa=t[root].fa;
	int gfa=t[fa].fa;
	int t1=(root!=t[fa].ch[0]);
	int t2=(fa!=t[gfa].ch[0]);
	int ch=t[root].ch[1^t1];
	t[ch].fa=fa;
	if(!isroot(fa))t[gfa].ch[0^t2]=root;
	t[root].fa=gfa;
	t[root].ch[1^t1]=fa;
	t[fa].fa=root;
	t[fa].ch[0^t1]=ch;
	update(fa);
	return;
}
void splay(int root)
{
	stk[++top]=root;
	for(int i=root;!isroot(i);i=t[i].fa)
	{
		stk[++top]=t[i].fa;
	}
	while(top)
	{
		pushdown(stk[top--]);
	}
	while(!isroot(root))
	{
		int fa=t[root].fa;
		int gfa=t[fa].fa;
		if(!isroot(fa))
		{
			root==t[fa].ch[0]^fa==t[gfa].ch[0]?rot(root):rot(fa);
		}
		rot(root);
	}
	update(root);
	return;
}
void access(int root)
{
	int temp=0;
	while(root)
	{
		splay(root);
		t[root].ch[1]=temp;
		update(root);
		temp=root;
		root=t[root].fa;
	}
	return;
}
void makeroot(int root)
{
	access(root);splay(root);t[root].lazy^=1;
	return;
}
void link(int root1,int root2)
{
	makeroot(root1);t[root1].fa=root2;
	return;
}
void cut(int root1,int root2)
{
	makeroot(root1);access(root2);splay(root2);
	if(t[root2].ch[0]==root1)t[root2].ch[0]=t[root1].fa=0;
	return;
}
void work(int root1,int root2)
{
	makeroot(root1);access(root2);splay(root2);
	return;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
    {
        memset(t,0,sizeof(tree)*(n+10));
        for(int i=1;i<n;++i)
        {
            scanf("%d %d",&a1,&b1);
            link(++a1,++b1);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d %d",&a1,&b1);
            work(++a1,++b1);
            printf("%d\n",t[b1].size-1);
        }
    }
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值