Network (hdu 3078 在线LCA)
2015-09-18 21:34
344 查看
Network
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 683 Accepted Submission(s): 274
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 3073 3074 3075
题意:n个点n-1条边,每个点有一个权值value[i],q次询问(k,u,v),若k==0,令value[u]=v;若k>0,输出u到v路径上第k大的权值。
思路:在线LCA算法,LCA转RMQ,添加一个pre数组记录路径。
代码:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define mod 1000000009 const int maxn = 1005; const int MAXN = 80010; const int MAXM = 200010; int n,q; int rmq[2*MAXN]; int pre[MAXN]; struct ST{ int mm[2*MAXN]; int dp[2*MAXN][20]; void init(int n) { mm[0]=-1; for (int i=1;i<=n;i++){ mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1]; dp[i][0]=i; } for (int j=1;j<=mm ;j++) for (int i=1;i+(1<<j)-1<=n;i++) dp[i][j]=rmq[dp[i][j-1]]<rmq[dp[i+(1<<(j-1))][j-1]]?dp[i][j-1]:dp[i+(1<<(j-1))][j-1]; } int query(int a,int b) { if (a>b) swap(a,b); int k=mm[b-a+1]; return rmq[dp[a][k]]<=rmq[dp[b-(1<<k)+1][k]]?dp[a][k]:dp[b-(1<<k)+1][k]; } }; struct Edge{ int to,next; }edge[MAXN*2]; int tot,head[MAXN]; int F[MAXN*2]; int P[MAXN]; int cnt; ST st; void init() { tot=0; memset(head,-1,sizeof(head)); memset(pre,-1,sizeof(pre)); } void addedge(int u,int v) { edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++; } void dfs(int u,int father,int dep) { F[++cnt]=u; rmq[cnt]=dep; P[u]=cnt; for (int i=head[u];~i;i=edge[i].next) { int v=edge[i].to; if (v==father) continue; pre[v]=u; dfs(v,u,dep+1); F[++cnt]=u; rmq[cnt]=dep; } } void LCA_init(int root,int node_num) { cnt=0; dfs(root,root,0); st.init(2*node_num-1); } int query(int u,int v) { return F[st.query(P[u],P[v])]; } bool flag[MAXN]; int value[MAXN]; int ans[MAXN]; void solve(int u,int v,int p) { int f=query(u,v); int cc=0; while (u!=f) { ans[cc++]=value[u]; u=pre[u]; } while (v!=f) { ans[cc++]=value[v]; v=pre[v]; } ans[cc++]=value[f]; if (cc<p) printf("invalid request!\n"); else { sort(ans,ans+cc); printf("%d\n",ans[cc-p]); } } int main() { #ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin); #endif int i,j,u,v,k; while (~scanf("%d%d",&n,&q)) { for (i=1;i<=n;i++) scanf("%d",&value[i]); init(); memset(flag,false,sizeof(flag)); for (i=1;i<n;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); flag[v]=true; } int root; for (i=1;i<=n;i++) { if (!flag[i]) { root=i; break; } } LCA_init(root,n); while (q--) { scanf("%d%d%d",&k,&u,&v); if (k==0) value[u]=v; else solve(u,v,k); } } return 0; }
相关文章推荐
- hdu2159 FATE(二维背包)
- 技术人生:故事之五 ODBC和C/S为何物
- iOS 核心动画 -基础动画
- HDU 4022 Bombing STL 模拟题
- 安装VS2013后与数据库的连接问题
- 送给半路出家的Pythoner
- 继续Set,Map起来
- Android设置透明、半透明等效果
- struts学习小结
- UVAoJ --401
- 自定义View之球体波浪形充满
- iOS 核心动画-锚点
- C#多线程学习笔记
- POJ 3261 出现至少K次的可重叠最长子串
- 为 Activity 设置切换动画
- error while loading shared libraries错误的解决
- 欢迎使用CSDN-markdown编辑器
- Android之Color颜色值和RGB颜色对照表
- Android绘图:360加速球
- python网络爬虫