Test Instructions
is to give you a ring-based tree, and then ask you to find a point (which can be on the edge), so that the point to the maximum point distance on all graphs is minimized
First there is a conclusion: this point must be the midpoint of the diameter in this "tree", the answer is half the length of this diameter
So we're actually looking for the longest chain.
The answer comes in two ways:
1. Do not go through any side of the ring, and then this sweep will be
2. To pass through the ring, then how does this count.
We know that for a scheme, he certainly will not go through the loop, that is, at least 1 edges will not be used, we will try to enumerate this edge, look at the end of the cut off the maximum value of the smallest is the answer.
Why is the maximum value minimal? For the maximum value, we are guaranteed that he is a longest chain, and then the minimum is because the path does not Shejinqiuyuan
Marking the point on the ring 1~k
Set Pre1[i] represents the maximum value of all points to 1 on the 1~i tree, Suf1[i] represents the maximum value of all points to K on the I~k tree
Set Pre2[i] represents the maximum value of the longest chain consisting of any two trees of 1~i, suf2[i] similar to that of i~k
This is and upside down separately sweep can be begged out, specific look at the code bar.
And then disconnecting the i~i+1 edge is
Max (pre1[i]+suf1[i+1]+ (side length between 1 and K), Pre2[i],suf2[i+1])
Then the final and non-loop scheme takes a maximum value
Here's a little bit of detail to note:
Which means that when you take ans, the order is required.
The following should have been written:
for (ll u=2;u<=cnt;u++)
{
ll tt=0;
Tt=max (Tt,pr2[u-1]);
Tt=max (Tt,suf2[u]);
Tt=max (tt,pr1[u-1]+suf1[u]+cval[cnt]);
Ans=min (ANS,TT);
}
for (int u=1;u<=n;u++) Ans=max (Ans,d[u]);
But at first I was lazy and wrote the following
Ans=max (ans,d[1]);
for (ll u=2;u<=cnt;u++)
{
ll tt=0;
Tt=max (Tt,pr2[u-1]);
Tt=max (Tt,suf2[u]);
Tt=max (tt,pr1[u-1]+suf1[u]+cval[cnt]);
Ans=min (ANS,TT);
Ans=max (Ans,d[u]);
}
I didn't tune it for a long time, and finally I found out there was a difference.
If you take the maximum value in it, maybe his minimum is gone and it will affect our answer.
It seems that I compare the dish, make the Min and Max have Exchange law.
CODE:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <
Stack> using namespace std;
typedef long Long LL;
Const LL n=100005;
LL N; struct QQ {LL x,y,c,last;} E[N*2];
LL Num,last[n];
void Init (LL x,ll y,ll c) {num++;
E[num].x=x;e[num].y=y;e[num].c=c;
E[NUM].LAST=LAST[X];
Last[x]=num; } bool vis[n];//is not in the stack inside bool in[n];//This point is not in the ring above LL cir[n],cnt;//ring ll cval[n],c1[n];//this point on the ring above with the father side of the weight stack<ll> STA
; BOOL Find_cir (LL x,ll fa,ll C)//Current node father-to-father-side value {if (vis[x]==true)//Ring {CIR[++CNT]=X;IN[X]=TRUE;CVAL[C
Nt]=c;
while (!sta.empty () &&sta.top ()!=x) {LL x=sta.top (); Sta.pop ();
Cir[++cnt]=x;
CVAL[CNT]=C1[X];
In[x]=true;
} return true;
} vis[x]=true;
Sta.push (x); c1[x]=c;
for (ll U=last[x];u!=-1;u=e[u].last) {ll y=e[u].y;
if (Y==FA) continue; if (Find_cir (y,x,e[u).c)) return true;
} sta.pop ();
return false; } ll dis[n],d[n];//the longest chain inside the tree inside the longest distance ll Dfs (ll X,ll FA) {ll re=0;//subtree in the maximum distance of two points for (LL u=last[x];u!=-1;u=e
[U].last] {LL y=e[u].y; if (y==fa| |
In[y]==true)//Our aim is to traverse this forest continue;
Re=max (Re,dfs (y,x));
LL tmp=dis[y]+e[u].c;
Re=max (re,dis[x]+tmp);//And before the chain and up Dis[x]=max (DIS[X],TMP);
} return re;
} LL Pr1[n],pr2[n];
LL Suf1[n],suf2[n];
int main () {Num=0;memset (last,-1,sizeof (last));
scanf ("%lld", &n);
for (ll u=1;u<=n;u++) {ll x,y,c;
scanf ("%lld%lld%lld", &x,&y,&c);
Init (X,Y,C); init (y,x,c);
} memset (In,false,sizeof (in));
memset (vis,false,sizeof (VIS)); Find_cir (1,0,0);//looking for ring cval[0]=cval[cnt];//because it is the ring for (LL u=1;u<=cnt;u++)//Now this tree is divided into a number of forest D[u]=dfs (CIR
[u],0];
LL T1=-cval[0],t2=-cval[0];
for (ll u=1;u<=cnt;u++) {ll nd=dis[cir[u]]; T1=T1+CVAL[U-1];T2=T2+CVAL[U-1];
Pr1[u]=max (PR1[U-1],ND+T1);
Pr2[u]=max (PR2[U-1],ND+T2);
T2=max (T2,ND);
} T1=-cval[0],t2=-cval[0];
for (ll u=cnt;u>=1;u--) {ll nd=dis[cir[u]];
T1=t1+cval[u];t2=t2+cval[u];
Suf1[u]=max (SUF1[U+1],ND+T1);
Suf2[u]=max (SUF2[U+1],ND+T2);
T2=max (T2,ND);
} LL ans=suf2[1];
for (ll u=2;u<=cnt;u++) {ll tt=0;
Tt=max (Tt,pr2[u-1]);
Tt=max (Tt,suf2[u]);
Tt=max (tt,pr1[u-1]+suf1[u]+cval[cnt]);
Ans=min (ANS,TT);
} for (int u=1;u<=n;u++) Ans=max (Ans,d[u]);
printf ("%.1lf\n", (double) ans/2.0);
return 0; }