HDU 3078 Network
2018-06-17 21:37:32来源:未知 阅读 ()
Network
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1495 Accepted Submission(s):
670
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.
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.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAXN=2*1e5+10; 6 inline int read() 7 { 8 char c=getchar();int x=0,f=1; 9 while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();} 10 while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();return x*f; 11 } 12 int n,m,S=1; 13 int f[MAXN][21],deep[MAXN],g[MAXN]; 14 struct node 15 { 16 int u,v,w,nxt; 17 }edge[MAXN]; 18 int head[MAXN]; 19 int num=1; 20 int a[MAXN]; 21 inline void add_edge(int x,int y,int z) 22 { 23 edge[num].u=x; 24 edge[num].v=y; 25 edge[num].w=z; 26 edge[num].nxt=head[x]; 27 head[x]=num++; 28 } 29 void dfs(int now) 30 { 31 for(int i=head[now];i!=-1;i=edge[i].nxt) 32 if(deep[edge[i].v]==0) 33 { 34 deep[edge[i].v]=deep[now]+1; 35 f[edge[i].v][0]=now; 36 g[edge[i].v]=g[now]+edge[i].w; 37 dfs(edge[i].v); 38 } 39 40 } 41 inline void pre() 42 { 43 for(int i=1;i<=19;i++) 44 for(int j=1;j<=n;j++) 45 f[j][i]=f[f[j][i-1]][i-1]; 46 } 47 inline int LCA(int x,int y) 48 { 49 if(deep[x]<deep[y]) swap(x,y); 50 for(int i=19;i>=0;i--) 51 if(deep[f[x][i]]>=deep[y]) 52 x=f[x][i]; 53 if(x==y) return x; 54 55 for(int i=19;i>=0;i--) 56 if(f[x][i]!=f[y][i]) 57 x=f[x][i],y=f[y][i]; 58 return f[x][0]; 59 } 60 int tot=0; 61 int ans[MAXN]; 62 int comp(const int &a,const int &b) 63 { 64 return a>b; 65 } 66 int work(int k,int x,int y) 67 { 68 tot=0; 69 int lca=LCA(x,y); 70 while(x!=lca) ans[++tot]=a[x],x=f[x][0];ans[++tot]=a[x]; 71 while(y!=lca) ans[++tot]=a[y],y=f[y][0]; 72 sort(ans+1,ans+tot+1,comp); 73 if(k>tot) printf("invalid request!\n"); 74 else printf("%d\n",ans[k]); 75 } 76 int main() 77 { 78 int T=1; 79 while(T--) 80 { 81 n=read();m=read(); 82 for(int i=1;i<=n;i++) a[i]=read(); 83 memset(head,-1,sizeof(head));num=1; 84 memset(f,0,sizeof(f)); 85 memset(deep,0,sizeof(deep)); 86 for(int i=1;i<=n-1;i++) 87 { 88 int x=read(),y=read(); 89 add_edge(x,y,0); 90 add_edge(y,x,0); 91 } 92 deep[S]=1; 93 dfs(S);pre(); 94 while(m--) 95 { 96 97 int x=read(),y=read(),z=read(); 98 if(x==0) a[y]=z; 99 else work(x,y,z); 100 } 101 } 102 103 return 0; 104 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash