HDU 3078 Network LCA水题

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
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!
 
 
 
 
给出一棵树,和树上各点的权值
 
然后有q个询问,每个询问输入k a b
若k==0  则要求把a点的权值改为b
若k>0   则要求输出a,b路径上的点中,权值第k大的点。
 
若没有的话,输出invalid request!
 
思路:直接把a,b路径上的点的权值存进一个数组里面,然后对数组大到小排序,输出第k个。
这么暴力竟然可以过,还是140ms。
 
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 
  5 using namespace std;
  6 
  7 const int maxn=80000+5;
  8 
  9 struct Edge
 10 {
 11     int to,next;
 12 }edge[maxn<<1];
 13 
 14 int head[maxn];
 15 int tot;
 16 int w[maxn];
 17 int dep[maxn];
 18 int ans[1000];
 19 int p[maxn][25];
 20 
 21 void init()
 22 {
 23     memset(dep,0,sizeof(dep));
 24     dep[1]=1;
 25     memset(p,-1,sizeof(p));
 26     memset(w,-1,sizeof(w));
 27     memset(head,-1,sizeof(head));
 28     tot=1;
 29 }
 30 
 31 void addedge(int u,int v)
 32 {
 33     edge[tot].to=v;
 34     edge[tot].next=head[u];
 35     head[u]=tot++;
 36 }
 37 
 38 void dfs(int u)
 39 {
 40     for(int i=head[u];~i;i=edge[i].next)
 41     {
 42         int v=edge[i].to;
 43         if(!dep[v])
 44         {
 45             dep[v]=dep[u]+1;
 46             p[v][0]=u;
 47             dfs(v);
 48         }
 49     }
 50 }
 51 
 52 void init_lca(int n)
 53 {
 54     for(int j=1;(1<<j)<=n;j++)
 55     {
 56         for(int i=1;i<=n;i++)
 57         {
 58             if(p[i][j-1]!=-1)
 59                 p[i][j]=p[p[i][j-1]][j-1];
 60         }
 61     }
 62 }
 63 
 64 bool cmp(int a,int b)
 65 {
 66     return a>b;
 67 }
 68 
 69 int solve(int n,int a,int b,int k)
 70 {
 71     if(dep[a]<dep[b])
 72         swap(a,b);
 73 
 74     int init_a=a;
 75     int init_b=b;
 76 
 77     int cnt;
 78 
 79     for(cnt=0;(1<<cnt)<=dep[a];cnt++)
 80         ;
 81     cnt--;
 82 
 83     for(int j=cnt;j>=0;j--)
 84     {
 85         if(dep[a]-(1<<j)>=dep[b])
 86             a=p[a][j];
 87     }
 88 
 89     int lca;
 90 
 91     if(a==b)
 92     {
 93         lca=b;
 94         if(dep[init_a]-dep[lca]+1<k)
 95             return -1;
 96     }
 97 
 98     else
 99     {
100         for(int j=cnt;j>=0;j--)
101         {
102             if(p[a][j]!=-1&&p[a][j]!=p[b][j])
103             {
104                 a=p[a][j];
105                 b=p[b][j];
106             }
107         }
108         lca=p[a][0];
109         if(dep[init_a]+dep[init_b]-2*dep[lca]+1<k)
110             return -1;
111     }
112 
113     tot=1;
114 
115     for(int i=dep[init_a];i>=dep[lca];i--)
116     {
117         ans[tot++]=w[init_a];
118         init_a=p[init_a][0];
119     }
120 
121     for(int i=dep[init_b];i>dep[lca];i--)
122     {
123         ans[tot++]=w[init_b];
124         init_b=p[init_b][0];
125     }
126 
127     sort(ans+1,ans+tot,cmp);
128 
129     return ans[k];
130 
131 
132 }
133 
134 int main()
135 {
136     int n,Q;
137     scanf("%d%d",&n,&Q);
138 
139     init();
140 
141     for(int i=1;i<=n;i++)
142         scanf("%d",&w[i]);
143 
144     for(int i=1;i<n;i++)
145     {
146         int a,b;
147         scanf("%d%d",&a,&b);
148         addedge(a,b);
149         addedge(b,a);
150     }
151 
152     dfs(1);
153 
154     init_lca(n);
155 
156     for(int i=0;i<Q;i++)
157     {
158         int k,a,b;
159         scanf("%d%d%d",&k,&a,&b);
160         if(k==0)
161         {
162             w[a]=b;
163         }
164         else
165         {
166             int ans=solve(n,a,b,k);
167             if(ans==-1)
168                 printf("invalid request!\n");
169             else
170                 printf("%d\n",ans);
171         }
172     }
173     return 0;
174 }
140ms

 

 
 

 

posted on 2015-05-13 18:24  _fukua  阅读(257)  评论(0编辑  收藏  举报