标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 847 Accepted Submission(s): 347
/* 5 5 5 1 2 3 4 3 1 2 1 4 3 5 3 2 4 5 */ #include<iostream> #include<cstdio> #include<algorithm> #include <string.h> #include <math.h> #define N 80005 using namespace std; struct Edge{ int u,v,next; }edge[2*N]; int head[N]; int deep[2*N]; int vis[N]; int first[N]; int ver[2*N]; int father[N]; int dp[2*N][25]; int value[N]; int path[N]; int tot; void add_edge(int u,int v,int &k){ ///Á´Ê½Ç°ÏòÐÇ edge[k].u = u,edge[k].v = v; edge[k].next = head[u];head[u] = k++; } void dfs(int u,int dep,int pre){ vis[u]=true,ver[++tot]=u,first[u]=tot,deep[tot]=dep,father[u]=pre; for(int k=head[u];k!=-1;k=edge[k].next){ if(!vis[edge[k].v]){ dfs(edge[k].v,dep+1,u); ver[++tot] = u,deep[tot]=dep; } } } int MIN(int i,int j){ if(deep[i]<deep[j]) return i; return j; } void init_RMQ(int n){ for(int i=1;i<=n;i++) dp[i][0]=i; for(int j=1;(1<<j)<=n;j++){ for(int i=1;i+(1<<j)-1<=n;i++){ dp[i][j] = MIN(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } } } int RMQ(int L,int R){ int k = (int)(log(R-L+1.0)/log(2.0)); int a = dp[L][k]; int b = dp[R-(1<<k)+1][k]; return MIN(a,b); } int LCA(int a,int b){ int x = first[a],y = first[b]; //printf("%d %d\n",deep[x],deep[y]); if(x>y) swap(x,y); //printf("RMQ(x,y): %d\n",RMQ(x,y)); return ver[RMQ(x,y)]; } int cmp(int a,int b){ return a>b; } void solve(int k,int u,int v){ int lca = LCA(u,v); tot=0; while(u!=lca){ path[tot++] = value[u]; u = father[u]; } while(v!=lca){ path[tot++] = value[v]; v = father[v]; } //printf("lca = %d\n",lca); path[tot++] = value[lca]; //printf("path[tot-1] = %d\n",path[tot-1]); if(k>tot){ printf("invalid request!\n"); return; } sort(path,path+tot,cmp); //for(int i=0;i<tot;i++) printf("%d ",path[i]); printf("%d\n",path[k-1]); } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF){ memset(head,-1,sizeof(head)); memset(father,-1,sizeof(father)); for(int i=1;i<=n;i++){ scanf("%d",&value[i]); } tot = 0; for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&u,&v); add_edge(u,v,tot); add_edge(v,u,tot); } tot = 0; dfs(1,1,-1); init_RMQ(2*n-1); while(m--){ int k,a,b; scanf("%d%d%d",&k,&a,&b); if(k==0){ value[a]=b; }else{ solve(k,a,b); } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5357032.html