【WikiOI】【P1036】【商务旅行】【题解】【LCA】

传送门:www.wikioi.com/problem/1036/

滚回新校1week回来做点康复训练,话说进队爷们都在隔壁虐题,我等蒟蒻被题虐……

又是LCA,我TM就是链剖!!

Code:

//ID:zky
#include
#include
#include
#include
using namespace std;
const int maxn=30010;
typedef pair pii;
int w[maxn],fa[maxn],son[maxn],top[maxn],dep[maxn],siz[maxn],z;
vectorG[maxn];
void add(int u,int v){
	G[u].push_back(v);
	G[v].push_back(u);
}
void dfs(int u){
	son[u]=0;
	siz[u]=1;
	for(int i=0;i0?x:-x;}
int len(int u,int v){
	int ans=0;
	while(top[u]!=top[v]){
		if(dep[top[u]]>dep[top[v]]){
			int a=dep[u],b=dep[top[u]];
			ans+=abs(a-b)+1;
			u=fa[top[u]];
		}else{
			int a=dep[v],b=dep[top[v]];
			ans+=abs(a-b)+1;
			v=fa[top[v]];			
		}
	}
	ans+=abs(dep[u]-dep[v]);
	return ans;
}
int q[maxn];
int n,m;
int main(){
	scanf("%d",&n);
	for(int i=1;i


你可能感兴趣的:(【WikiOI】【P1036】【商务旅行】【题解】【LCA】)