HDU 3078 Network tarjan算法

来源:互联网 发布:域名和网址和ip的联系 编辑:程序博客网 时间:2024/06/08 14:24
/*NetworkTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1304    Accepted Submission(s): 581Problem 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!*//*PS:暴力tarjan*/#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int Tmaxn = 80000+10;const int Qmaxn = 30000+10;int head_tree[Tmaxn],head_query2[Tmaxn],head_query[Tmaxn],fat[Tmaxn],vist[Tmaxn];int val[Tmaxn],find_lca[Tmaxn],N,M,cnt_tree,cnt_query2,cnt_query;int mmp[Tmaxn];struct tree{int to,next;}Tree[Tmaxn<<1];struct query{int u,v,id;int op;}Query[Qmaxn<<1];struct query2{int to,next,id;}Query2[Qmaxn<<1];void Init(){cnt_tree = cnt_query2 = 0;for(int i = 1; i <= N; i++){vist[i] = 0;head_tree[i] = head_query2[i] = -1;fat[i] = i;}}bool cmp(int a,int b){return a>b;}void ADD_tree(int u,int v){Tree[cnt_tree].to = v;Tree[cnt_tree].next = head_tree[u];head_tree[u] = cnt_tree++;}void ADD_query2(int u,int v,int id){Query2[cnt_query2].to = v;Query2[cnt_query2].next = head_query2[u];Query2[cnt_query2].id = id;head_query2[u] = cnt_query2++;}void ADD_query(int op,int u,int v,int id){Query[id].u = u;Query[id].v = v;Query[id].id = id;Query[id].op = op;}int Find(int x){if(x != fat[x])x = Find(fat[x]);return x;}void Tarjan(int u,int father){for(int i = head_tree[u]; ~i; i = Tree[i].next){int v = Tree[i].to;if(!vist[v] && v!=father){Tarjan(v,u);fat[v] = u;}}vist[u] = 1;for(int i = head_query2[u]; ~i; i = Query2[i].next){int v = Query2[i].to;if(vist[v])find_lca[Query2[i].id] = Find(v);}}int main(){int u,v,op;scanf("%d %d",&N,&M);Init();for(int i = 1; i <= N; i++)scanf("%d",&val[i]);for(int i = 1; i < N; i++){scanf("%d %d",&u,&v);ADD_tree(u,v);ADD_tree(v,u);}for(int i = 1; i <= M; i++){scanf("%d %d %d",&op,&u,&v);ADD_query(op,u,v,i);if(op){ADD_query2(u,v,i);ADD_query2(v,u,i);}}Tarjan(1,-1);for(int i = 1; i <= M; i++){if(Query[i].op){u = Query[i].u;v = Query[i].v;int cnt = 0;while(u != find_lca[Query[i].id]){mmp[cnt++] = val[u];u = fat[u];}while(v != find_lca[Query[i].id]){mmp[cnt++] = val[v];v = fat[v];}mmp[cnt++] = val[find_lca[Query[i].id]];if(cnt < Query[i].op)puts("invalid request!");else{sort(mmp,mmp+cnt,cmp);printf("%d\n",mmp[Query[i].op-1]);}}elseval[Query[i].u] = Query[i].v;}return 0;}

原创粉丝点击