HDU2586(最近公共祖先的Tarjan算法)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2586

 

题意:给一棵树,询问u和v之间的边权和。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 

using namespace std;
const int N = 50005;

vector v[N],w[N],query[N],num[N];
int pre[N],dist[N],ans[N];
bool vis[N];
int n;

void Init()
{
    for(int i=1; i<=n; i++)
    {
        v[i].clear();
        w[i].clear();
        query[i].clear();
        num[i].clear();
        pre[i] = i;
        dist[i] = 0;
        vis[i] = false;
    }
}

int Find(int x)
{
    if(pre[x] != x)
        pre[x] = Find(pre[x]);
    return pre[x];
}

void Union(int x,int y)
{
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    pre[y] = x;
}

void Tarjan(int cur,int val)
{
    vis[cur] = true;
    dist[cur] = val;
    int size = v[cur].size();
    for(int i=0;i


 

你可能感兴趣的:(图论)