Lightoj 1101 a secret mission

Source: Internet
Author: User

Http://www.lightoj.com/volume_showproblem.php? Problem = 1101.

For an undirected graph, ask the minimum risk of the path between two points. The danger level of a path is defined as the maximum edge weight of the path.

Step 1:

Because the edge weight is small, it is 1 ~ 1000. The first thought was to add all the sides with the weight of 1 to check which two points have been connected. The answer to those questions is 1; add the edge with the weight of 2 ...... Continue until all the answers are answered. This complexity is 1000 * q = 5*10 ^ 7. After Various optimizations are added, it still needs to time out.

It took a lot of time on this idea, but it is not meaningless. Because when edge is added, it is found that if the two points of an edge have been merged, there is no need to add this edge, so you can think of the Minimum Spanning Tree. So we can convert the source image into a tree. There is only one path between the two nodes, and it feels a little simpler. The problem also turns into the greatest weight on this path.

Step 2:

When we see the tree, we naturally think of LCA, but this is not like sum [u] + sum [v]-2 * sum [LCA] In the path summation mode. This maximum value cannot be used for subtraction, so how can we quickly find the results after knowing the two nodes and Their LCA? (After reading other people's code, I found a very simple and useful method. See the last red letter.) It seems that I can only be violent, but I thought that if a certain inquiry has been solved, in another query, the two-point LCA nodes are the parent nodes of the LCA node in this query. I feel that I can solve the problem with a slight modification. So I thought of the first solution to the LCA in a deeper level of inquiry, such as the X layer; then add the side connecting the X-1 and the X layer, you can solve the problem with the LCA at the X-1 layer.

The specific point of thinking is: sort the inquiry by the deep to the shallow order by the LCA, and sort all the nodes by the deep to the shallow order. Add and combine the edges of the node at the deepest layer (maintain the maximum value from this node to the path of the ancestor node at the same time ), in this case, if the requested LCA is on this layer, then the two points will be resolved.

Each side is scanned, and each query is scanned. The complexity is roughly e + Q, hundreds of thousands. On the contrary, the previous sorting takes up the main time.

PS: in the middle, the node starts from 0, and the result is re several times. If it is a stack overflow, the deep search of the tree is written as non-recursive. The array of rmq to be added to each node is only in two cases: 1. Search for the first time; 2. Search for a subnode. The first case is good, and the second case should be slightly modified. The specific method is as follows:

Two records should be added to the stack element: 1. the label of the parent node, and 2. Whether the node has been accessed. Set the access tag to 1 when a node is used in the stack for the first time, but do not take it out and add its label to the array. When it is used for the second time, that is, the access Mark has already been set to 1, which means that all of the subsequent searches are complete, so do not scan the child node any more, take it out and put its parent node label into an array.

The simplest method after knowing the node and the LCA is to perform brute force lookup. In fact, we can simulate the regular rmq solution sparse table to optimize ...... The first time I saw this kind of usage, I felt that my thoughts were singing my melancholy with a miserable horn in the barren land after dusk. In a regular sparse table, num [I] [J] indicates the interval from I to 2 ^ J, which is copied to the tree structure: P [I] [J] indicates the node from node I to step 2 ^ J (the other end of the range can be found based on IJ in rmq, but it is hard to find it here, so you have to save it), and create an array to save the information of this interval.

Therefore, P [I] [0] is the parent node of node I. Others: P [I] [J] = P [p [I] [J-1] [J-1]; (from point I to 2 ^ (J-1) to point I ', then take 2 ^ (J-1) step from I ).

Then, you can make full use of it. In fact, we don't need to find the LCA, which is much more convenient.

Summary: although it is a tree structure, in fact, every node goes up is a one-dimensional array, unexpectedly did not think ......

Code:

#include <set>#include <map>#include <list>#include <cmath>#include <ctime>#include <deque>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cassert>#include <cstdlib>#include <cstring>#include <sstream>#include <iostream>#include <algorithm>#define ll __int64//#define ll long long#define PI acos(-1.0)#define PRN 105using namespace std;int f_min(int x,int y){return x<y?x:y;}int f_max(int x,int y){return x>y?x:y;}int f_abs(int x){return x<0?-x:x;}int prime[PRN],Pn;bool unprime[PRN];void get_prime(){int i,j,lim=sqrt(PRN);memset(unprime,0,sizeof(unprime));Pn=0;for(i=2;i<PRN;i++){if(unprime[i])continue;prime[Pn++]=i;if(i>lim)continue;for(j=i*i;j<PRN;j+=i)unprime[j]=1;}}struct Edge{int u,v,w,next;bool mark;bool friend operator<(Edge a,Edge b){return a.w<b.w;}};struct Node{int dep,fa,prev;};Node node[50005];Edge edge[210000],inie[100000];int head[50005],en;void insert(int u,int v,int w){edge[en].u=u;edge[en].v=v;edge[en].w=w;edge[en].mark=0;edge[en].next=head[u];head[u]=en++;}int N,M,Q;void get_data(){scanf("%d%d",&N,&M);int i,u,v,w;for(i=0;i<M;i++){scanf("%d%d%d",&u,&v,&w);u--;v--;inie[i].u=u;inie[i].v=v;inie[i].w=w;}}int fa[50005];int findf(int x){if(fa[x]!=x)fa[x]=findf(fa[x]);return fa[x];}void get_edge(){memset(head,-1,sizeof(head));en=0;sort(inie,inie+M);int i,u,v;for(i=0;i<N;i++)fa[i]=i;for(i=0;i<M;i++){u=findf(inie[i].u);v=findf(inie[i].v);if(u==v)continue;fa[u]=v;insert(u,v,inie[i].w);insert(v,u,inie[i].w);}}int maxd;void dfs(int u,int dep){//printf("%d\n",u);node[u].dep=dep;maxd=f_max(maxd,dep);int i,v;for(i=head[u];i!=-1;i=edge[i].next){if(edge[i].mark)continue;edge[i^1].mark=1;v=edge[i].v;node[v].fa=u;node[v].prev=edge[i].w;dfs(v,dep+1);}}void get_tree(){node[0].fa=-1;node[0].prev=0;maxd=0;dfs(0,0);}int P[50005][20],vmax[50005][20];void pre_process(){memset(P,-1,sizeof(P));memset(vmax,0,sizeof(vmax));int i,j,l;for(i=0;i<N;i++){P[i][0]=node[i].fa;vmax[i][0]=node[i].prev;}for(j=1,l=2;l<=maxd;j++,l<<=1){for(i=0;i<N;i++){if(l<=node[i].dep){P[i][j]=P[P[i][j-1]][j-1];vmax[i][j]=f_max(vmax[i][j-1],vmax[P[i][j-1]][j-1]);}}}}void ans(int u,int v){int res=0,i;if(node[u].dep<node[v].dep)swap(u,v);i=20;while(node[u].dep>node[v].dep){while(node[u].dep-(1<<i)<node[v].dep)i--;res=f_max(res,vmax[u][i]);u=P[u][i];}if(u==v){printf("%d\n",res);return;}i=20;while(P[u][0]!=P[v][0]){while(node[u].dep<(1<<i)||P[u][i]==P[v][i])i--;res=f_max(res,f_max(vmax[u][i],vmax[v][i]));u=P[u][i];v=P[v][i];}res=f_max(res,f_max(vmax[u][0],vmax[v][0]));printf("%d\n",res);}void run(){get_edge();get_tree();pre_process();scanf("%d",&Q);int i,u,v;for(i=0;i<Q;i++){scanf("%d%d",&u,&v);u--;v--;ans(u,v);}}int main(){int i,t;scanf("%d",&t);for(i=1;i<=t;i++){get_data();printf("Case %d:\n",i);run();}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.