bzoj3242: [Noi2013] fast food restaurant

Source: Internet
Author: User

--From a lost dream of salted fish miaom

Considering the general routine of jellyfish base ring tree, in the case of determining the location of a fast food restaurant, there must be an edge on the ring in the optimal solution. Thinking about enumerating the edges, we need to find the diameter of the remainder in the quickest time. The current answer is diameter/2, proving very simple, the farthest point from the fast food restaurant must be the diameter endpoint. This thing can be maintained by a double-pointer monotone queue to achieve O (n) complexity. Then I lost my dream, straight line of tree water past.

The line tree approach is as follows: first make the ring, repeat it into the sequence problem, ask for the diameter of an interval. There is a case that the diameter is not on the ring and can be pretreated. Another situation is related to the deepest point of the tree on the ring, using the segment tree to maintain the depth of the point ± in the loop of the position of the band merge, see Code ~

As for how to do the ring, not the focus, a wave of Dfs (in fact, topological sort can also).

#include <bits/stdc++.h> #define N 200005 #define LL long long using namespace std;
ll N,x,y,z,m;
ll Fst[n],to[n*2],nxt[n*2],len[n],l;
ll Vis[n],fa[n],flag,flag1,flag2,fl[n];
ll A[n];ll b[n],d[n],m1[n],mx[n],dep[n],vl[n],vr[n],ans,ly;
	void Link (ll x,ll y,ll z) {to[++l]=y;len[l]=z;nxt[l]=fst[x];fst[x]=l;
To[++l]=x;len[l]=z;nxt[l]=fst[y];fst[y]=l;
	} void Dfs (LL x) {//cout<<x<<endl;
	Vis[x]=1; for (ll I=fst[x];i;i=nxt[i]) if (To[i]!=fa[x]) {if (Vis[to[i])) {if (!flag) flag=x,flag1=to[i],flag2=l
			En[i];
				} else {dep[to[i]]=dep[x]+len[i];
				Fa[to[i]]=x;
				DFS (To[i]);
			Fl[x]+=fl[to[i]];
	}}} void Dfs (ll x) {mx[x]=0;
	m1[x]=0;
			for (ll I=fst[x];i;i=nxt[i]) if (to[i]!=fa[x]&&fl[to[i]]==0) {fa[to[i]]=x;
			Dfs (To[i]);
			Mx[x]=max (Mx[x],max (mx[to[i]],m1[x]+m1[to[i]]+len[i));
		M1[x]=max (M1[x],m1[to[i]]+len[i]); }} struct T {ll l,r,a;}
Nd[n*8],now; T operator+ (t a,t b) {return (t) {max (A.L,B.L), Max (A.R, B.R), Max (Max (A.A,B.A), A.L+B.R)};
		} void Build (ll k,ll l,ll R) {if (l==r) {nd[k]= (T) {vl[l],vr[l],0};
	Return
	} ll mid=l+r>>1;
	Build (K<<1,l,mid);
	Build (K<<1|1,mid+1,r);
nd[k]=nd[k<<1]+nd[k<<1|1];
		} void Qry (ll k,ll l,ll r,ll x,ll y) {if (x<=l&&r<=y) {if (now.a==-1) now=nd[k];
		else Now=now+nd[k];
	Return
	} ll mid=l+r>>1;
	if (x<=mid) qry (k<<1,l,mid,x,y);
if (y>mid) qry (k<<1|1,mid+1,r,x,y);
	} int main () {scanf ("%lld", &n);
		for (ll i=1;i<=n;i++) {scanf ("%lld%lld%lld", &x,&y,&z);
	Link (x, y, z);
	} dfs (1);
		
	for (; Flag!=flag1;flag=fa[flag]) Fl[a[++m]=flag]=1,b[m]=dep[flag]-dep[fa[flag]];
	Fl[a[++m]=flag1]=1;b[m]=flag2;
	
	for (ll i=1;i<=m;i++) Fa[a[i]]=0,dfs (A[i]), Ly=max (Ly,mx[a[i]]);
	
	for (ll i=1;i<=m;i++) a[i+m]=a[i],b[i+m]=b[i];
		for (ll i=1;i<=2*m;i++) {d[i]=d[i-1]+b[i-1]; 
		Vl[i]=m1[a[i]]-d[i];
	Vr[i]=m1[a[i]]+d[i];
	} build (1,1,2*m); ans=100000000000000L;
		for (ll i=1;i<=m;i++) {now= (T) {0,0,-1};
		Qry (1,1,2*m,i,i+m-1);
	Ans=min (Ans,max (now.a,ly));
	
} printf ("%lld.%d\n", ans/2,ans&1?5: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.