HDU3078 Network [2016年6月計劃 樹上問題05]

來源:互聯網
上載者:User

標籤:style   return   etc   pac   udt   hot   case   other   ignore   

Network

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


Problem DescriptionThe 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. 

 

InputThere 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. 

 

OutputFor 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 Input5 55 1 2 3 43 12 14 35 32 4 50 1 22 2 32 1 43 3 5 

 

Sample Output322invalid request! 

 

Source 2009 Multi-University Training Contest 17 - Host by NUDT  

 

Recommendlcy   |   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]);elseprintf("invalid request!\n"); if(ans[rank])printf("%d\n", ans[rank]);elseprintf("invalid request!\n"); return 0;}

 

HDU3078 Network [2016年6月計劃 樹上問題05]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.