bzoj1832: [AHOI2008]聚会

题解:三个点 对任选两个点求Lca 然后对于三种情况取最优的即可

#include <bits/stdc++.h>
#define ll long long
const int MAXN=5e5+10;
using namespace std;
ll read(){  
    ll x=0,f=1;char ch=getchar();  
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}  
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();  
    return f*x;  
}  
vector<int>vec[MAXN];
int fa[MAXN],dep[MAXN],son[MAXN],num[MAXN];
void dfs1(int v,int pre,int deep){
	dep[v]=deep;num[v]=1;fa[v]=pre;
	for(int i=0;i<vec[v].size();i++){
		if(vec[v][i]!=pre){
			dfs1(vec[v][i],v,deep+1);
			num[v]+=num[vec[v][i]];
			if(son[v]==-1||num[vec[v][i]]>num[son[v]])son[v]=vec[v][i];
		}
	}
}
int tp[MAXN];
void dfs2(int v,int td){
	tp[v]=td;
	if(son[v]!=-1)dfs2(son[v],td);
	for(int i=0;i<vec[v].size();i++){
		if(vec[v][i]!=son[v]&&vec[v][i]!=fa[v])dfs2(vec[v][i],vec[v][i]);
	}
}
int Lca(int u,int v){
	int uu=tp[u];int vv=tp[v];
	while(uu!=vv){
		if(dep[uu]<dep[vv])swap(u,v),swap(uu,vv);
		u=fa[uu];uu=tp[u];
	}
	if(dep[u]>dep[v])swap(u,v);
	return u;
}
int pos,vul;
void slove(int x,int y,int z){
	int lca1=Lca(x,y);pos=lca1;vul=dep[x]+dep[y]-2*dep[lca1];
	int lca2=Lca(lca1,z);vul+=(dep[lca1]+dep[z]-2*dep[lca2]);
	int vul2;
	lca1=Lca(x,z);vul2=dep[x]+dep[z]-2*dep[lca1];
	lca2=Lca(lca1,y);vul2+=(dep[lca1]+dep[y]-2*dep[lca2]);
	if(vul2<vul)vul=vul2,pos=lca1;
	lca1=Lca(y,z);vul2=dep[y]+dep[z]-2*dep[lca1];
	lca2=Lca(lca1,x);vul2+=(dep[lca1]+dep[x]-2*dep[lca2]);
	if(vul2<vul)vul=vul2,pos=lca1;
}
int main(){
	int n,m;n=read();m=read();
	int u,v;
	for(int i=1;i<=n;i++)son[i]=-1;
	for(int i=1;i<n;i++)u=read(),v=read(),vec[u].push_back(v),vec[v].push_back(u);
	dfs1(1,0,0);dfs2(1,1);
	int x,y,z;
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		slove(x,y,z);
		printf("%d %d\n",pos,vul);
	}
	return 0;
}

 

1832: [AHOI2008]聚会

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1719  Solved: 738
[Submit][Status][Discuss]

Description

Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们的每一次聚会选择一个合适的地点。

Input

第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市编号以及小YY所在的城市编号。

Output

一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所花费的费用。

Sample Input

6 4
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6

Sample Output

5 2
2 5
4 1
6 0

数据范围:
100%的数据中,N<=500000,M<=500000。
40%的数据中N<=2000,M<=2000。
posted @ 2018-06-17 23:48  wang9897  阅读(157)  评论(0编辑  收藏  举报