首先,学习LCA(tanjar)算法~~推荐两篇博客,结合起来看很容易理解&.&
第一篇:LCA算法(较详细)
第二篇:LCA(思想部分较好)
例题:CODEVS 2370 小机房的树 点我点我~
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+9;
const int INF = -1;
struct node
{
int to,val;
node(int a=0,int b=0){to = a;val = b;}
};
vector<node> vec[MAX_N]; // 建树的边
vector<node> q[MAX_N];