# 【LCA】Tree

ural 1471，调了好久，终于发现是数组开小了。。。一开始crash了好久，改了几个数组。又crash了好久。。。最终发现漏了一个数组没有修改

## 1471. Tree

Time Limit: 2.0 second
Memory Limit: 64 MB
A weighted tree is given. You must find the distance between two given nodes.

### Input

The first line contains the number of nodes of the tree n (1 ≤ n ≤ 50000). The nodes are numbered from 0 to n – 1.Each of the next n – 1 lines contains three integers u, v, w, which correspond to an edgewith weight w (0 ≤ w ≤ 1000) connecting nodes u and v.The next line contains the number of queries m (1 ≤ m ≤ 75000).In each of the next m lines there are two integers.

### Output

For each query, output the distance between the nodes with the given numbers.

### Sample

input output
3
1 0 1
2 0 1
3
0 1
0 2
1 2

1
1
2


#pragma comment(linker, "/STACK:16777216")

#include <cstdlib>
#include <iostream>
using std::cout;
using std::cin;

long n = 0;
long S = 0;
long T = 0;
long _dis = 0;
long ans = 0;

struct node
{
long index;
long val;
node* next;
};
bool vis[50010];//////////////////
long dist[50010];
//long dfsnum[50010];
//long depth[50010];
//long first[50010];
long dfsnum[100010];
long depth[100010];
long first[50010];

long st[100010][18];
long top = 0;

void dfs(long l,long f,long dep)
{
dfsnum[++top] = l;
depth[top] = dep;
first[l] = top;

dist[l] = _dis;
{
//if (nxt->index == f) continue;////
if (!vis[nxt->index])
{
vis[nxt->index] = true;
_dis += nxt->val;
dfs(nxt->index,l,dep+1);
_dis -= nxt->val;

dfsnum[++top] = l;
depth[top] = dep;
}
}

}

inline long getint() //这个getchar的输入对大数据量输入非常有用，甚至可以挽救效率不高的算法
{
long ret = 0;
char tmp;
while (!isdigit(tmp = getchar()));
do {
ret = (ret << 3)+(ret << 1) + tmp - '0';
} while (isdigit(tmp = getchar()));
return ret;
}

inline void insert(long a,long b,long c)
{
node* tmp = new node;
tmp->index = b;
tmp->val = c;
}

inline long lg2(long a)
{
long ans = 0;
while (a>>=1)
{
ans++;
}
return ans;
}

inline long rmq(long a,long b)
{
if (b < a)
{
long tmp = b;
b = a;
a = tmp;
}
long k = lg2(b-a+1);
if (depth[st[a][k]]<depth[st[b-(1<<k)+1][k]])
return st[a][k];
return st[b-(1<<k)+1][k];
}

inline void rmqinit()
{
for (long i=1;i<top+1;i++)
{
st[i][0] = i;
#ifdef Debug
//	std::cerr << st[i][0] << std::endl;
#endif
}
for (long j=1;(1<<j)<=top;j++)
for (long i=1;i<=top-(1<<j)+1;i++)
{
if (depth[st[i][j-1]]<depth[st[i+(1<<(j-1))][j-1]])
st[i][j] = st[i][j-1];
else st[i][j] = st[i+(1<<(j-1))][j-1];
#ifdef Debug
//			if (st[i][j] > 40000)
//				std::cerr << i << " " << j << " " << st[i][j] << std::endl;
#endif
}
}

int main()
{
#ifdef Debug
memset(st,0,sizeof(st));
#endif

freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
n = getint();
top = 0;
for (long i=1;i<n+1;i++)
{
vis[i] = false;
}

for (long i=1;i<n;i++)
{
long a = getint();
long b = getint();
long c = getint();
insert(a,b,c);
insert(b,a,c);
}
for (long i=0;i<n+1;i++)//////////////
{
{
vis[i] = true;///////////
dfs(i,0,0);/////
break;///
}
}
rmqinit();
long m = getint();
for (long i=1;i<m+1;i++)
{
S = getint();
T = getint();
long cfa = dfsnum[rmq(first[S],first[T])];
//#ifdef Debug
//if (dist[S]+dist[T]-2*dist[cfa] == 3003907)
printf("%ld\n",dist[S]+dist[T]-2*dist[cfa]);
//#endif
}
return 0;
}


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <map>
using std::pair;
using std::map;
using std::make_pair;
map<pair<long,long>,bool> hash;
long n;
const long maxn = 50000;
const long maxm = 75000;
const long maxw = 1000;

int main()
{
srand(time(0));
freopen("tree.in","w",stdout);
//	n = rand()%maxn + 2;
n = maxn;
printf("%ld\n",n);
for (long i=1;i<n;i++)
{
long a=1;long b=1;long c;
while (a == b || hash[make_pair(a,b)] || hash[make_pair(b,a)])
{
a = rand()%n ;
b = rand()%n ;
}
hash[make_pair(a,b)] = true;
c = rand()%maxw;
printf("%ld %ld %ld\n",a,b,c);
}
//	long m = rand()%maxm+1;
long m = maxm;
printf("%ld\n",m);
for (long i=1;i<m+1;i++)
{
long a;long b;
a = rand()%n + 1;
b = rand()%n + 1;
printf("%ld %ld\n",a,b);
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：【LCA】Tree 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)