[SDOI2013] diameter explanations

Face questions

This question is particularly apparent diameter must find the path, so dfs twice is a good choice;

The first question to ask is a mentally handicapped, right?

The main difficulty all share in the second question;

Is not difficult, first find any diameter;

For any point in diameter:

Provided nxt [i] represents a position of the point, pre [i] represents a position on this point;

ls [i] point to the left represents the distance of the spot diameter, rs [i] indicates the distance from the point to the right end of a diameter;

Let us dfs again, to find the most distant point at each time point does not pass on any diameter can reach, this value is recorded as dismax [i];

Then we began to advance from the left point to the right diameter, until you find a point of rs [i] = dismax [i] (which means that there is a diameter such that the lower edge of one side is not a necessary);

We note that, from right to left also need to do this once. After all, we can not guarantee that the left endpoint is a necessary edge;

A can then be lost;

 

#include <bits/stdc++.h>
#define int long long
#define inc(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
const int MXR=2e5+10;
int head[MXR],cnt;
class littlestar{
	public:
	int to,nxt,w;
	void add(int u,int v,int gg){
		to=v; nxt=head[u]; 
		w=gg;head[u]=cnt;
	}
}star[MXR<<1];
int n,m;
template<class nT>
void read(nT& x)
{
	char c;while(c=getchar(),!isdigit(c));
	x=c^48;while(c=getchar(),isdigit(c)) x=x*10+c-48;
}
int dis[MXR],root1,root2,f[MXR],g[MXR];
void dfs(int u,int fa)
{
	f [u] = four;
	for(int i=head[u];i;i=star[i].nxt){
		int v=star[i].to;
		if(v==fa) continue;
		dis[v]=dis[u]+star[i].w;
		dfs(v,u);
		g[v]=star[i].w;
	}
}
int judge[MXR],ls[MXR],rs[MXR],nxt[MXR],pre[MXR];
int dismax[MXR];
void dfs2(int u,int fa)
{
	for(int i=head[u];i;i=star[i].nxt){
		int v=star[i].to;
		if(v==fa) continue;
		dfs2 (v, u);
		if(judge[v]==0){
			dismax[u]=max(dismax[u],star[i].w+dismax[v]);
		}
	}
}
signed main()
{
	read(n);
	inc(i,1,n-1){
		int a,b,c;
		read(a);read(b);read(c);
		star[++cnt].add(a,b,c);
		star[++cnt].add(b,a,c);
	}
	root1=1;
	dfs(root1,0);
	int maxn=-INT_MAX;
	inc(i,1,n) if(dis[i]>maxn) maxn=dis[i],root2=i;
	memset(dis,0,sizeof(dis)); maxn=-INT_MAX; memset(f,0,sizeof(f));
	dfs(root2,0);
	inc(i,1,n) if(dis[i]>maxn) maxn=dis[i],root1=i;
	int tmpx=root1;
	while(tmpx!=root2){
		nxt[tmpx]=f[tmpx];
		pre[f[tmpx]]=tmpx;		
		ls [f [tmpx]] = ls [tmpx] + g [tmpx];
		rs[tmpx]=maxn-ls[tmpx];
		judge[tmpx]=1;
		tmpx=f[tmpx];
	}
	judge[root2]=1;
	dfs2(root2,0);
	int goal=0;
	tmpx=root1;
	while(tmpx!=0){
		if(rs[tmpx]==dismax[tmpx]){
			break;
		}
		tmpx=nxt[tmpx];
	}
	if(tmpx==0) tmpx=pre[tmpx];
	goal=tmpx;
	while(tmpx!=0){
		if(ls[tmpx]==dismax[tmpx]){
			break;
		}
		tmpx=pre[tmpx];
	}
	if(tmpx==0) tmpx=nxt[tmpx];
	int years = 0;
	while(tmpx!=goal){
		++ years;
		tmpx=nxt[tmpx];
	}
	cout<<maxn<<endl;
	cout << years;
	
}
/*
6
3  1 1000
1  4 10
4  2 100
4  5 50
4  6 100
*/

 

 

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11799096.html
Recommended