关闭

【LCA】Tree

350人阅读 评论(0) 收藏 举报

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


这道题找任意两节点之间的距离。可以先任意找一个点作根(此任意要注意,必须是存在的点。。。我一开始指定0为根,结果有的数据0不存在)

建树的时候同时维护first,depth,dfsnum,为LCA转化为RMQ做准备,还要维护dist,为求两节点间距离做准备。

建一棵树之后,把LCA转化成RMQ来做,详见我转载的一篇文章。

两节点间距离等于两节点到根的距离之和减去最近公共祖先到根的距离的二倍。

血的教训。开数组一定要谨慎。depth,dfsnum的大小一定是n*2-1。参见那篇文章


#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;	
};
node* head[50010];
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;
	for (node* nxt=head[l];nxt;nxt=nxt->next)
	{
		//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;
	tmp->next = head[a];
	head[a] = tmp;
}

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++)
	{
		head[i] = 0;
		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++)//////////////
	{
		if (head[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;	
}

附上datamaker,提供了一个生成树的思路(最完美的应该是用最小生成树),但是有个缺陷,可能会生成一片森林。。。将就用了

#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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:327380次
    • 积分:7193
    • 等级:
    • 排名:第3098名
    • 原创:419篇
    • 转载:14篇
    • 译文:0篇
    • 评论:32条
    文章分类
    最新评论