[POJ 2763] Housewife Wind【树的DFS序+树状数组+LCA】

  • 2018-01-21
  • 0
  • 0


Time Limit: 4000MS Memory Limit: 65536K


After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.

Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!'

At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.

Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?


The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.

The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.

The following q lines each is one of the following two types:

Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.


For each message A, print an integer X, the time required to take the next child.

Sample Input

3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3

Sample Output



POJ Monthly--2006.02.26,zgl & twb


DFS 序的用处就是将子树转化为连续区间,而区间问题一般可以用高级数据结构来维护。

首先,我们可以发现必须求出 LCA,这里我用了倍增的思想进行求解,详见[POJ 1986] Distance Queries【倍增LCA / TarjanLCA】

与此同时,在 DFS 的过程中我们可以求出每个节点的 DFS 序


注意细节,就可以 1A 啦 ~(^_^)~ (这题的数据范围终于是对的了。。)

Code: O((n+q)logn) [12892K, 1219MS]

using namespace std;

inline int getint(){
	char ch; int num;
	while(!isdigit(ch = getchar()));
	num = ch - '0';
	while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
	return num;

int n, q, s;
int fa[100010], dep[100010], jmp[17][100010];
int dfn[100010], dfstime = 0;
int upp[100010];
int dis[100010];  // The distance to the root with the subscription of dfn[]

struct Edge{
	int np, cost;
	Edge *nxt;

struct Graph{
	Edge *V[100010], E[200010];
	int tope;
	inline void addedge(int u, int v, int w){
		E[++tope].np = v, E[tope].cost = w;
		E[tope].nxt = V[u], V[u] = &E[tope];
} G;

struct BIT{
	int node[100010];
	inline void add(int u, int v) {while(u < 100010) node[u] += v, u += u & -u;}
	inline int query(int u) {int res = 0; while(u) res += node[u], u -= u & -u; return res;}
} b;

inline void DFS(int u, int dist){
	dep[u] = dep[fa[u]] + 1;
	dis[dfn[u] = ++dfstime] = dist;
	for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
		if(ne->np != fa[u]) fa[ne->np] = u, DFS(ne->np, dist + ne->cost);
	upp[u] = dfstime;

inline void Init_jmp(){
	for(register int i = 1; i <= n; i++) jmp[0][i] = fa[i];
	for(register int ex = 1; ex < 17; ex++)
		for(register int i = 1; i <= n; i++)
			jmp[ex][i] = jmp[ex - 1][jmp[ex - 1][i]];

inline int Get_LCA(int u, int v){
	if(dep[u] < dep[v]) swap(u, v);
	int ddep = dep[u] - dep[v];
	for(register int ex = 16; ex >= 0; ex--)
		if(ddep & (1 << ex)) u = jmp[ex][u];
	if(u == v) return v;
	for(register int ex = 16; ex >= 0; ex--)
		if(jmp[ex][u] != jmp[ex][v])
			u = jmp[ex][u], v = jmp[ex][v];
	return fa[v];

int main(){
	n = getint(), q = getint(), s = getint();
	for(register int i = 1; i < n; i++){
		int u = getint(), v = getint(), w = getint();
		G.addedge(u, v, w), G.addedge(v, u, w);
	DFS(1, 0), Init_jmp();
	for(register int i = 2; i <= n; i++) b.add(i, dis[i] - dis[i - 1]);  // Region modification & Single query
	for(register int i = 1; i <= q; i++){
		int op = getint();
			int u = getint(), lca = Get_LCA(s, u);
			printf("%d\n", b.query(dfn[s]) + b.query(dfn[u]) - (b.query(dfn[lca]) << 1));
			s = u;
			int e = getint() << 1, w = getint();
			if(G.E[e].np == fa[G.E[e - 1].np]) e--;
			b.add(dfn[G.E[e].np], w - G.E[e].cost), b.add(upp[G.E[e].np] + 1, G.E[e].cost - w);
			G.E[e].cost = w;
	return 0;






OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.


- Theme by Qzhai