HDU3078 Network [2016年6月计划 树上问题05]

Network

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1293    Accepted Submission(s): 575

Problem Description

The
ALPC company is now working on his own network system, which is
connecting all N ALPC department. To economize on spending, the backbone
network has only one router for each department, and N-1 optical fiber
in total to connect all routers.
The usual way to measure connecting
speed is lag, or network latency, referring the time taken for a sent
packet of data to be received at the other end.
Now the network is on
trial, and new photonic crystal fibers designed by ALPC42 is trying
out, the lag on fibers can be ignored. That means, lag happened when
message transport through the router. ALPC42 is trying to change routers
to make the network faster, now he want to know that, which router, in
any exactly time, between any pair of nodes, the K-th high latency is.
He needs your help.

Input

There are only one test case in input file.
Your
program is able to get the information of N routers and N-1 fiber
connections from input, and Q questions for two condition: 1. For some
reason, the latency of one router changed. 2. Querying the K-th longest
lag router between two routers.
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000.
Then n integers in second line refer to the latency of each router in the very beginning.
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y.
Then
q lines followed to describe questions, three numbers k, a, b for each
line. If k=0, Telling the latency of router a, Ta changed to b; if
k>0, asking the latency of the k-th longest lag router between a and b
(include router a and b). 0<=b<100000000.
A blank line follows after each case.

Output

For
each question k>0, print a line to answer the latency time. Once
there are less than k routers in the way, print "invalid request!"
instead.

Sample Input

5 5
5 1 2 3 4
3 1
2 1
4 3
5 3
2 4 5
0 1 2
2 2 3
2 1 4
3 3 5

Sample Output

3
2
2
invalid request!

Source

2009 Multi-University Training Contest 17 - Host by NUDT

Recommend

lcy   |   We have carefully selected several similar problems for you:  3071 3070 3072 3073 3074

找树上两个点路径上的第k大节点

根据lca分别从起点和终点网上找点权,记下来,排序

lca预处理nlogn  lca查询logn  找路径 logn(n个节点的树最多logn层) 排序logn * logn

因此总复杂度(m + n)log^2n

不会超时

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
inline void read(int &x){char ch = getchar();char c = ch;x = 0;while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();if(c == ‘-‘)x = -x;}
inline void swap(int &a, int &b){int tmp = a;a = b;b = tmp;}

const int MAXN = 80000 + 10;

struct Edge{int u,v,next;}edge[MAXN << 1];
int head[MAXN],cnt,n,m,w[MAXN],p[20][MAXN],deep[MAXN],fa[MAXN],num[MAXN],b[MAXN];
inline void insert(int a,int b){edge[++cnt] = Edge{a, b, head[a]},head[a] = cnt;}

int llog2[MAXN],pow2[30];

void dfs(int u)
{
	for(int pos = head[u];pos;pos = edge[pos].next)
	{
		int v = edge[pos].v;
		if(b[v])continue;
		b[v] = true,deep[v] = deep[u] + 1,p[0][v] = u,fa[v] = u,dfs(v);
	}
}

inline void yuchuli()
{
	b[1] = true,deep[1] = 0,dfs(1);
	register int i;
	for(i = 1;i <= llog2[n];i ++)
		for(int j = n;j >= 1;j --)
			p[i][j] = p[i - 1][p[i - 1][j]];
}

inline int lca(int va, int vb)
{
	register int i;
	if(deep[va] < deep[vb])swap(va, vb);
	for(i = llog2[n];i >= 0;i --)
		if(deep[va] - pow2[i] >= deep[vb])
			va = p[i][va];
	if(va == vb)return va;
	for(i = llog2[n];i >= 0;i --)
		if(p[i][va] != p[i][vb])
			va = p[i][va],vb = p[i][vb];
	return p[0][va];
}

int ans[MAXN],rank;

inline void work(int s, int t, int k)
{
	int anc = lca(s, t);
	int now = s;
	register int i = 1;
	num[i] = w[now];
	while(now != anc)
		num[++i] = w[(now = fa[now])];
	now = t;
	while(now != anc)
		num[++i] = w[now], now = fa[now];
	std::sort(num + 1, num + 1 + i);
	if(i >= k)
		ans[++rank] = num[i - k + 1];
	else rank++;
}

int main()
{
	register int i,tmp1,tmp2,tmp3;
	llog2[0] = -1,pow2[0] = 1;
	for(int i = 1;i <= MAXN;i ++)llog2[i] = llog2[i >> 1] + 1;
	for(int i = 1;i <= 25; i++)pow2[i] = (pow2[i - 1] << 1);
	read(n),read(m);
	for(i = 1;i <= n;i ++)read(w[i]);
	for(i = 1;i < n;i ++)read(tmp1),read(tmp2),insert(tmp1, tmp2),insert(tmp2, tmp1);
	yuchuli();
	for(i = 1;i <= m;i ++)
	{
		read(tmp1),read(tmp2),read(tmp3);
		if(tmp1)work(tmp2, tmp3, tmp1);
		else w[tmp2] = tmp3;
	}
	for(int i = 1;i < rank;i ++)
		if(ans[i])
			printf("%d\n", ans[i]);
		else
			printf("invalid request!\n");
	if(ans[rank])
		printf("%d\n", ans[rank]);
	else
		printf("invalid request!\n");
	return 0;
}
时间: 2024-08-01 16:34:56

HDU3078 Network [2016年6月计划 树上问题05]的相关文章

2016年9月计划

这个月的三大目标: 1. 培养健康的作息:10点半上床睡觉 2. 培养写博文的习惯:一个月至少要有两个系列的文章:每天看至少两篇技术文章: 3. 培养一段感情:勇于去尝试!有目的沟通,学会说话. 系列文章: 1. 推荐算法(相关知识.实践使用.反思总结) 2. java的内存查看(进程状态查询.内存泄漏问题.性能调优)

2016年1月计划

你现在唯一要做的就是--坚持. 对自己开战,这是一场持久战!

HDU3887 Counting Offspring [2017年6月计划 树上问题03]

Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2809    Accepted Submission(s): 981 Problem Description You are given a tree, it’s root is p, and the node is numbered from 1

ZOJ3195 Design the city [2017年6月计划 树上问题04]

Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason

洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]

[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who once roamedthe country lanes amidst sleepy hamlets and who now dashes down the motorways. But it is those strolls inthe days of

2016年4月8日作业

软考信息系统监理师:2016年4月8日作业 一.组织协调1.组织协调的基本原则是什么(记)? 答:1)公平.公正.独立原则:2)守法原则:3)诚信原则:4)科学的原则. 2.什么是科学的原则? 答:科学的原则,就是在监理实践中,要依据科学的方案(如监理规划),运用科学的手段(如测试设备或测试工具软件),采取科学的办法(如收集数据),并在项目结束后,进行科学的总结(如信息归纳整理). 3.组织协调的监理方法有哪些? 答:1)监理会议:2)监理报告:3)沟通: 3.会议成功的关键是什么?(记) 答:

2016半年总结与计划

转眼间16年竟然过去了一半,已经是六月份了.魔兽电影也出了,也嗨皮了. 年初给自己定的小计划也该回顾回顾了,作为一个码农技术上貌似也没什么多大的进步,虽然看了一些新知识,但是总感觉少了点什么, 没有那么深入的去理解.我觉得这种进步方式应该改改了.学习一个技术还是得一步一步的来,只知道使用,不知道远离进步也不大. 上半年计划执行的很糟糕,感情方面也遭遇很多的问题,从现在开始冷静下来好好按照自己计划的路线去学习吧. 下半年技术规划: 1.strom学习 2.elasticsearch学习 3.kaf

软考高项学员:2016年4月20日作业

软考高项学员:2016年4月20日作业 一.战略管理1.企业战略的特点有哪些?2.企业战略决策的特点有哪些?3.战略管理分为哪三步?(记)4.组织的使包括组织哲学和组织宗旨,请写下2者的定义.5.要确定一个组织的宗旨,首先做什么?(记)6.什么是长期目标,什么是短期目标?7.战略制订包括哪五个步骤?(记)8.战略分析中的外部分析有哪些?9.宏观趋势分析包括哪些?请列出名称,并简述之.10.行业分析包括哪些?11.五力模型认为,行业中的竞争包括哪五种基本的竞争力量?(记)(可以参看图20.1五力模

软考中高项学员:2016年3月28日作业

软考中高项学员:2016年3月28日作业 一.项目沟通管理1.项目沟通管理包括哪些过程?(记)2.阻碍有效沟通的因素有哪些?3.沟通计划编制的第一步是什么?目的是什么?4.沟通管理计划包括哪些内容(8条)5.干系人沟通计划包括哪些内容?(记)6.项目例会的主要议题有哪四条?7.项目内部启动会议.外部启动会议分别要解决什么问题?8.项目总结会议的目的有哪些?9.影响项目沟通的技术因素有哪些?9.常用的四种沟通方式是什么?各有何优缺点?10.信息分发的工具和技术是什么?11.经验教训总结过结果是什么