HDU 3078 - Network (LCA)【tarjan离线/DFS倍增】
2018-03-19 22:11
489 查看
Network
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1628 Accepted Submission(s): 726
[align=left]Problem Description[/align]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.
[align=left]Input[/align]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.
[align=left]Output[/align]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.
[align=left]Sample Input[/align]5 55 1 2 3 43 12 14 35 32 4 50 1 22 2 32 1 43 3 5
[align=left]Sample Output[/align]322invalid request!
[align=left]Source[/align]2009 Multi-University Training Contest 17 - Host by NUDT
找出a,b到他们lca的路径上的第k大点值。
POINT:
找出lca,模拟把每个点找到。这样理论上不能ac。因为数据弱,水过去了。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 80000+111;
const int inf = 0x3f3f3f3f;
int val[maxn];
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt=0;
int qhead[maxn],qto[maxn],qflag[maxn],qnxt[maxn],qcnt=0;
int pre[maxn],fa[maxn],vis[maxn];
void add(int u,int v)
{
to[cnt]=v;
nxt[cnt]=head[u];
head[u]=cnt++;
}
void add1(int u,int v,int flag)
{
qto[qcnt]=v;
qflag[qcnt]=flag;
qnxt[qcnt]=qhead[u];
qhead[u]=qcnt++;
}
struct node
{
int k,a,b;
int ans;
}qu[maxn];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
fa[yy]=xx;
}
void tarjan(int u)
{
vis[u]=1;
for(int i=head[u];~i;i=nxt[i]){
int v=to[i];
if(vis[v]) continue;
tarjan(v);
merge(u,v);
pre[v]=u;
}
for(int i=qhead[u];~i;i=qnxt[i]){
int v=qto[i];
if(vis[v]){
qu[qflag[i]].ans=find(v);
}
}
}
bool cmd(int a,int b)
{
return a>b;
}
int main()
{
int n,q;
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&val[i]);
qhead[i]=head[i]=-1;
fa[i]=i;
}
for(int i=1;i<n;i++){
int u,v;scanf("%d %d",&u,&v);
add(u,v);add(v,u);
}
for(int i=1;i<=q;i++){
scanf("%d %d %d",&qu[i].k,&qu[i].a,&qu[i].b);
if(qu[i].k>=1){
add1(qu[i].a,qu[i].b,i);
add1(qu[i].b,qu[i].a,i);
}
}
tarjan(1);
for(int i=1;i<=q;i++){
vector<int>v;
int k=qu[i].k;
int a=qu[i].a;
int b=qu[i].b;
if(k==0){
val[a]=b;
}else{
v.clear();
int now = a;
while(now!=qu[i].ans){
v.push_back(val[now]);
now=pre[now];
}
now=b;
while(now!=qu[i].ans){
v.push_back(val[now]);
now=pre[now];
}
v.push_back(val[qu[i].ans]);
if(v.size()<k){
printf("invalid request!\n");
}else{
sort(v.begin(),v.end(),cmd);
printf("%d\n",v[k-1]);
}
}
}
// printf("\n");
return 0;
}
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 80000+5;
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt=0;
int fa[maxn][17],d[maxn];
int val[maxn],vis[maxn];
int n,Q;
void add(int x,int y)
{
to[cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt++;
}
void dfs(int u)
{
vis[u]=1;
for(int i=head[u];~i;i=nxt[i]){
int v=to[i];
if(vis[v]) continue;
d[v]=d[u]+1;
dfs(v);
fa[v][0]=u;
}
}
void init()
{
for(int i=1;(1<<i)<=n;i++)
for(int j=1;j<=n;j++){
fa[j][i]=fa[fa[j][i-1]][i-1];
}
}
int query(int a,int b)
{
if(d[a]<d[b]) swap(a, b);
int dis=d[a]-d[b];
int c=0;
while(dis){
if(dis&1)
a=fa[a][c];
dis/=2;
c++;
}
if(a==b) return a;
else{
for(int i=(int)log2(n);i>=0;i--){
if(fa[a][i]!=fa[b][i])
a=fa[a][i],b=fa[b][i];
}
a=fa[a][0];
return a;
}
}
bool cmd(int a,int b)
{
return a>b;
}
int main()
{
scanf("%d %d",&n,&Q);
memset(head,-1,sizeof head);
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1;i<n;i++){
int u,v;scanf("%d %d",&u,&v);
add(u,v);add(v,u);
}
d[0]=0;
dfs(1);
init();
while(Q--){
vector<int>v;
int k,a,b;
scanf("%d %d %d",&k,&a,&b);
if(k==0){
val[a]=b;
}else{
v.clear();
int now = a;
int ans=query(a, b);
// printf("%d\n",ans);
// int ci=0;//祖先找的不对!!!或者pre数组
while(now!=ans){
v.push_back(val[now]);
now=fa[now][0];
}
now=b;
while(now!=ans){
v.push_back(val[now]);
now=fa[now][0];
}
v.push_back(val[ans]);
if(v.size()<k){
printf("invalid request!\n");
}else{
sort(v.begin(),v.end(),cmd);
printf("%d\n",v[k-1]);
}
}
}
return 0;
}
相关文章推荐
- hdu 3078 Network(lca 倍增)
- HDU 6065 RXD, tree and sequence(在线倍增LCA+CDQ分治+离线tarjan-LCA+dp)
- HDU 2586How far away ?(离线Tarjan求LCA
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- HDU 3078 - Network(LCA'ST算法)
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- 最近公共祖先LCA(Tarjan与DFS--ST倍增)
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- HDU 3078 Network LCA + 排序
- Network (hdu 3078 在线LCA)
- 最近公共祖先(LCA)算法实现过程 【Tarjan离线+倍增在线+RMQ】
- HDU - 3078 - Network(LCA)
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- hdu 2586 How far away ?(离线tarjan求LCA)
- HDU 3078 Network 在线LCA
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- hdu 2874 Tarjan离线 LCA
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- hdu 2586 lca离线tarjan