3784: 树上的路径

3784: 树上的路径

Time Limit: 10 Sec   Memory Limit: 256 MB
Submit: 459   Solved: 152
[ Submit][ Status][ Discuss]

Description

给定一个N个结点的树,结点用正整数1..N编号。每条边有一个正整数权值。用d(a,b)表示从结点a到结点b路边上经过边的权值。其中要求a

Input

第一行两个正整数N,M
下面N-1行,每行三个正整数a,b,c(a,b<=N,C<=10000)。表示结点a到结点b有一条权值为c的边。

Output

共M行,如题所述.

Sample Input

5 10
1 2 1
1 3 2
2 4 3
2 5 4

Sample Output

7
7
6
5
4
4
3
3
2
1

HINT

N<=50000,M<=Min(300000,n*(n-1) /2 )


Source

[ Submit][ Status][ Discuss]

前k大路径??
借助超级钢琴的思路
如果我们还能弄出一堆区间,那么还是能用RMQ + 堆解决的
路径很多很多?? -> 点分治
事实上,子树上的路径无非分过根or不过根
对每棵子树建立一个dfs序,每个点能走到的点都是一个区间
事实上,每个点被用了logn次(因为我们点分治呀)
因此,每个点在不同重心下,可以由一个区间的信息转移出解
然后就能用超级钢琴的思路写了

O(nlog^n)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 5E4 + 50;
const int INF = ~0U>>1;

struct E{
	int to,w;
	E(int _to = 0,int _w = 0) {to = _to; w = _w;}
};

struct data1{
	int Max[16],pos[16];
	data1() {memset(Max,0,sizeof(Max)); memset(pos,0,sizeof(pos));}
};

struct data2{
	int sum,l,r,Root,va,po;
	data2(int _sum = 0,int _l = 0,int _r = 0,int _Root = 0,int _va = 0,int _po = 0) {
		sum = _sum; l = _l; r = _r; Root = _Root; va = _va; po = _po;
	}
	bool operator < (const data2 &b) const {return va < b.va;}
};

int n,m,dfs_clock,O,ma,dfn[maxn],out[maxn],siz[maxn],d[maxn];
bool Mark[maxn];

vector  v[maxn];
vector  v2[maxn];
priority_queue  Q;

void dfs2(int x,int from,int Siz)
{
	int Max = 0; siz[x] = 1;
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i].to;
		if (to == from || Mark[to]) continue;
		dfs2(to,x,Siz); siz[x] += siz[to];
		Max = max(Max,siz[to]);
	}
	Max = max(Max,Siz - siz[x]);
	if (Max < ma) ma = Max,O = x;
}

void dfs3(int x,int o,int from,int sum)
{
	dfn[x] = dfs_clock++;
	data1 New; New.Max[0] = sum;  New.pos[0] = dfn[x];
	v2[o].push_back(New);
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i].to;
		if (to == from || Mark[to]) continue;
		dfs3(to,o,x,sum + v[x][i].w);
	}
	out[x] = dfs_clock - 1;
}

void dfs4(int x,int o,int from,int l,int r,int va,int po)
{
	Q.push(data2(v2[o][dfn[x]].Max[0],l,r,o,va + v2[o][dfn[x]].Max[0],po));
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i].to;
		if (to == from || Mark[to]) continue;
		dfs4(to,o,x,l,r,va,po);
	}
}

void dfs1(int x,int Siz)
{
	if (Siz == 1) return;
	dfs_clock = 0; ma = INF;
	dfs2(x,0,Siz); int o = O;
	Mark[o] = 1; dfs3(o,o,0,0);
	for (int i = 1; i < 16; i++)
		for (int j = 0; j < dfs_clock; j++) {
			int k = j + (1<<(i-1));
			if (k >= dfs_clock) break;
			if (v2[o][j].Max[i-1] > v2[o][k].Max[i-1])
				v2[o][j].Max[i] = v2[o][j].Max[i-1],v2[o][j].pos[i] = v2[o][j].pos[i-1];
			else v2[o][j].Max[i] = v2[o][k].Max[i-1],v2[o][j].pos[i] = v2[o][k].pos[i-1];
		}
	for (int i = 0; i < v[o].size(); i++) {
		int to = v[o][i].to;
		if (Mark[to]) continue;
		int l = 0,r = dfn[to] - 1;
		int len = r - l + 1,va,po;
		int k = r - (1< v2[o][k].Max[d[len]])
			va = v2[o][l].Max[d[len]],po = v2[o][l].pos[d[len]];
		else va = v2[o][k].Max[d[len]],po = v2[o][k].pos[d[len]];
		dfs4(to,o,0,l,r,va,po);
	}
	for (int i = 0; i < v[o].size(); i++) {
		int to = v[o][i].to;
		if (Mark[to]) continue;
		dfs1(to,siz[to]);
	}
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m;
	int now = 0;
	for (int i = 1; i <= n; i++) {
		if (i >= (1<<(now+1))) ++now;
		d[i] = now;
	}
	for (int i = 1; i < n; i++) {
		int x,y,z; scanf("%d%d%d",&x,&y,&z);
		v[x].push_back(E(y,z));
		v[y].push_back(E(x,z));
	}
	dfs1(n/2,n);
	
	while (m--) {
		data2 k = Q.top(); Q.pop();
		printf("%d\n",k.va);
		if (k.l < k.po) {
			int l = k.l,r = k.po - 1;
			int len = r - l + 1,va,po;
			int K = r - (1< v2[k.Root][K].Max[d[len]])
				va = v2[k.Root][l].Max[d[len]],po = v2[k.Root][l].pos[d[len]];
			else va = v2[k.Root][K].Max[d[len]],po = v2[k.Root][K].pos[d[len]];
			Q.push(data2(k.sum,l,r,k.Root,va + k.sum,po));
		}
		if (k.po < k.r) {
			int l = k.po + 1,r = k.r;
			int len = r - l + 1,va,po;
			int K = r - (1< v2[k.Root][K].Max[d[len]])
				va = v2[k.Root][l].Max[d[len]],po = v2[k.Root][l].pos[d[len]];
			else va = v2[k.Root][K].Max[d[len]],po = v2[k.Root][K].pos[d[len]];
			Q.push(data2(k.sum,l,r,k.Root,va + k.sum,po));
		}
	}
	return 0;
}


你可能感兴趣的:(堆,RMQ,区间分裂,点分治)