[NOIP2015] [LCA] [a tree difference] transport plan

[Problem Description]
year 2044, humanity has entered the cosmic era.
L n-owned planet, there are n-1 bidirectional channel, each channel is established between the two planets, which the n-1
channel of communication with all countries of the planet L.
Little P, in charge of a logistics company, the company has a number of transportation planning, transportation plans for each of the form: there is a material
flow from the spacecraft needs ui number of aerospace planet along the fastest path to vi flight number to the planet. Obviously, the spacecraft passing a course
it takes time for the channel j, any ship passing of time it takes for tj, and any of the two spacecraft
will not have any interference.
In order to encourage scientific and technological innovation, the king agreed to a small country L P logistics companies involved in waterway construction L States that allow small
P to transform a channel into a wormhole, wormhole passing spacecraft is not time consuming.

Before the completion of the construction of small P wormhole logistics companies pre-wired by m transport plan. After the completion of the construction of the wormhole,
the m transportation plan will also begin, starting with all the spacecraft. When the m transportation plans are complete, the small P
logistics company phased work is complete.
If the small P is free to choose which of course will be transformed into a wormhole and try to find a small logistics company P stage of completion
minimum time needed to work is how much?
 
[Input format
into a file called transport.in.
The first line includes two positive integers n, m, L represents the planet and a small number of countries P's pre-contact transportation schedule
number, numbered from 1 to n planet.
Next, construction of the line n-1 channel is described, wherein the i-th row comprising three integers ai, bi and ti, represents the
article of construction between the two-way route i ai and bi two planets, passing it would take any of the spacecraft the time ti.
Next m lines describe the case of transportation planning, where the first line contains two positive integers j uj and vj, j th
transport plan from planet to vj uj No. No. planet.

[] Output format
output file name transport.out.
A total of one line, contains an integer representing the small P logistics company completed the minimum time required for stage work. 

Sample 1 [O]
transport.in
. 6. 3
1 2. 3
1. 6. 4
. 3. 7 1
. 4. 3. 6
. 3. 5. 5
. 3. 6
2. 5
. 4. 5

transport.out 
11

[O] Sample 1 illustrates
the transformation of article 1 of the fairway into a wormhole: the three programs are time-consuming: 11,12,11, when it takes a
room of 12.
Article 2 of the fairway transformed into a wormhole: the three programs are time-consuming: 7,15,11, when it takes a
room of 15.
Article 3 of the fairway transformed into a wormhole: the plan takes three are: 8, 11, so the time it takes
is 11.
Article 4 of the fairway transformed into a wormhole: the three programs are time-consuming: 11,15,5, when it takes a
room of 15.
Article 5 of the fairway transformed into a wormhole: the three programs are time-consuming: 11,10,6, when it takes a
room of 11.
Therefore, the transformation of Article 3 or Article 5 of the fairway into a wormhole can make time-consuming to complete the shortest stage of the work, it takes
time fee is 11.


 Please note that the constant influence on program efficiency factor brings.
 

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=300000+15;
 6 int n,m,p[N],q[N],cmp[N],ord[N],id;
 7 
 8 int num,last[N],nxt[2*N],ver[2*N],len[2*N];
 9 inline void add(int x,int y,int z) 
10  {nxt[++num]=last[x]; last[x]=num; ver[num]=y; len[num]=z;
11   nxt[++num]=last[y]; last[y]=num; ver[num]=x; len[num]=z;     
12  }
13  // 2^18
14 int f[N][20],dis[N],deep[N];
15 void dfs(int x)
16  {ord[++id]=x;
17   for(int j=1;j<=18&&(1<<j)<=deep[x];j++)     f[x][j]=f[f[x][j-1]][j-1]; 
18   
19   for(int i=last[x];i;i=nxt[i])
20   {int y=ver[i];
21    if(y==f[x][0]) continue;
22    f[y][0]=x;  deep[y]=deep[x]+1;  dis[y]=dis[x]+len[i];
23    dfs(y);
24   }      
25  } 
26 inline int lca(int x,int y)
27  {if(deep[x]<deep[y]) swap(x,y);
28      
29   for(int i=18;i>=0;i--)
30     if(deep[f[x][i]]>=deep[y] ) x=f[x][i]; 
31         
32   if(x==y) return x;
33 
34   for(int i=18;i>=0;i--)
35     if(f[x][i]!=f[y][i]) 
36          {x=f[x][i]; y=f[y][i]; }
37   return f[x][0];    
38  } 
39 
40 int v[N]; 
41 
42 inline bool judge(int t)
43  {int lim=0,cnt=m; memset(v,0,sizeof(v));
44   for(int k=1;k<=m;k++)
45   {int he=dis[p[k]]+dis[q[k]]-2*dis[cmp[k]];
46    if(he<=t)   {cnt--; continue;}
47 
48    lim=max(lim,he-t); 
49    v[p[k]]++; v[q[k]]++; v[cmp[k]]-=2; 
50   }
51   for(int i=n;i>0;i--) v[f[ord[i]][0]]+=v[ord[i]];
52   
53   for(int i=1;i<=n;i++) if(v[i]==cnt && dis[i]-dis[f[i][0]]>=lim)return 1;
54   return 0;
55  } 
56  
57 inline int red()
58  {int r=0; char ch=getchar();
59   while(!('0'<=ch && ch<='9')) ch=getchar();
60   while('0'<=ch && ch<='9')      {r=r*10+ch-'0';ch=getchar();}
61   return r;      
62  } 
63  
64 int main()
65  {int x,y,z; n=red(); m=red();
66   
67   for(int i=1;i<n;i++) {x=red(); y=red(); z=red(); add(x,y,z);}     
68      
69   deep[1]=1; dfs(1);   int l=0,r=0,ans;  
70      
71   for(int i=1;i<=m;i++) 
72    {p[i]=red(); q[i]=red(); cmp[i]=lca(p[i],q[i]); 
73     r=max(r,dis[p[i]]+dis[q[i]]-2*dis[cmp[i]]);
74    }     
75      
76   while(l<=r)
77   {int mid=(l+r)/2;
78    if(judge(mid)){ans=mid; r=mid-1; }
79    else           l=mid+1;   
80   }       
81   printf("%d",ans);
82 return 0;     
83  }

 

Guess you like

Origin www.cnblogs.com/YuXiaoze/p/11005874.html