树上最长距离 hdu5527、cf592D

HDU5527题意:

给一棵树,问从每个点出发可以到达的最远的距离~~~~~~~~~


首先先弄清楚如何找出一棵树上最长的距离~~

1、从root遍历一下,得到各个点到根的距离

2、找出最大距离的那个点设为根,设为ROOT

3、再从新根开始遍历所有点,得到各个点到新根的距离

4、树上最长距离就是刚才遍历出来的最大距离~~

可以联系现实想一下~~~


那么对于HDU5527这道题

在这里,先以root为根跑一个DFS找出ROOT,新树的树根为ROOT

再对新树跑一次DFS

顺便对于root这个节点的子树节点,进行标记~~~

有两种情况  

一、如果这个节点属于新树里面的root节点的子树节点,那么最远距离即为到ROOT的距离

二、如果这个节点不属于新树里面的root节点的子树节点,那么最远距离为max(到ROOT的距离,点到root的距离+root到子树最远叶子的距离)

#include <algorithm>
#include <iostream>
#include<string.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define exp 1e-8
#define fi first
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
#define cnt_one(i) __builtin_popcount(i)
#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;
void bug(string m="here"){cout<<m<<endl;}
template<typename __ll> inline void READ(__ll &m){__ll x=0,f=1;char ch=getchar();while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}m=x*f;}
template<typename __ll>inline void read(__ll &m){READ(m);}
template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}
template < class T > inline  void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}
template < class T > inline  void outln(T a){out(a);puts("");}
template < class T > inline  void out(T a,T b){out(a);putchar(' ');out(b);}
template < class T > inline  void outln(T a,T b){out(a);putchar(' ');outln(b);}
template < class T > inline  void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}
template < class T > inline  void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }

const int cnt_edge=20100;  //修改啊
const int cnt_v=10100;
int head[cnt_v],cnt_e;
struct EDGE{int u,v,next,cost;}edge[cnt_edge];
void addedge(int u,int v,int cost=0)
{edge[cnt_e].u=u;edge[cnt_e].v=v;edge[cnt_e].cost=cost;edge[cnt_e].next=head[u];head[u]=cnt_e++;}
void init(){cnt_e=0;memset(head,-1,sizeof(head));}
#define erg(i,u) for(int i=head[u];i!=-1;i=edge[i].next)

int n;
int dist[2][cnt_v];
bool vis[cnt_v];
void dfs(int u,int fa,int idx,int DIST,int flag)
{
    vis[u]=flag;
    dist[idx][u]=DIST;
    if(u==1)flag=1;
    erg(i,u)
    {
        int v=edge[i].v;
        if(v==fa)continue;
        dfs(v,u,idx,DIST+edge[i].cost,flag);
    }
}
int main()
{
    while(cin>>n)
    {
        init();
        for1(i,n-1)
        {
            int a,b;
            read(a,b);
            addedge(a,i+1,b);
            addedge(i+1,a,b);
        }
        mm(dist,0);
        dfs(1,1,0,0,0);
        int w,maxn=-1;
        for1(i,n)if(dist[0][i]>maxn)
        {
            w=i;
            maxn=dist[0][i];
        }
        mm(vis,0);
        dfs(w,w,1,0,0);
        maxn=0;
        for1(i,n)if(vis[i])maxn=max(maxn,dist[1][i]-dist[1][1]);
        for1(i,n)
            if(vis[i])
                cout<<dist[1][i]<<endl;
            else
                cout<<max(dist[1][i],dist[0][i]+maxn)<<endl;
    }
}


CF592D:

给一棵树,有n个节点,给出特定的m个节点,一个人可以出现再任意的一个点,问遍历完这特定的m个点的最短路径~~


以某一个特定点为根进行遍历,可以找出最少必须要经过的所有点,数量设为sum,回到出发点的话,那么这个人一共走了(sum-1)*2的距离

但是这里并不需要走回到出发点,遍历完就走人了~~那么这个人一共走了(sum-1)*2-cost,其中cost是出发点到结束点的距离~~

那么这里就相当于求一个最大的cost~~~~~

也就是以必须经过的点重建一颗树,然后求树内最远的距离~~

#include <algorithm>
#include <iostream>
#include<string.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define exp 1e-8
#define fi first
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
#define cnt_one(i) __builtin_popcount(i)
#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;
void bug(string m="here"){cout<<m<<endl;}
template<typename __ll> inline void READ(__ll &m){__ll x=0,f=1;char ch=getchar();while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}m=x*f;}
template<typename __ll>inline void read(__ll &m){READ(m);}
template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}
template < class T > inline  void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}
template < class T > inline  void outln(T a){out(a);puts("");}
template < class T > inline  void out(T a,T b){out(a);putchar(' ');out(b);}
template < class T > inline  void outln(T a,T b){out(a);putchar(' ');outln(b);}
template < class T > inline  void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}
template < class T > inline  void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }

const int cnt_edge=123496*2;  //修改啊
const int cnt_v=123496;
int head[cnt_v],cnt_e;
struct EDGE{int u,v,next,cost;}edge[cnt_edge];
void addedge(int u,int v,int cost=0)
{edge[cnt_e].u=u;edge[cnt_e].v=v;edge[cnt_e].cost=cost;edge[cnt_e].next=head[u];head[u]=cnt_e++;}
void init(){cnt_e=0;memset(head,-1,sizeof(head));}
#define erg(i,u) for(int i=head[u];i!=-1;i=edge[i].next)

int n,m,a,b;
bool h[cnt_v];
bool vis[cnt_v];
int dist[cnt_v];
bool dfs(int u,int fa,int DIST)
{
    dist[u]=DIST;
    bool flag=h[u];
    erg(i,u)
    {
        int v=edge[i].v;
        if(v==fa)continue;
        if(dfs(v,u,DIST+1))flag=1;
    }
    return vis[u]=flag;
}
int main()
{
    init();
    read(n,m);
    for1(i,n-1)
    {
        read(a,b);
        addedge(a,b);
        addedge(b,a);
    }
    for1(i,m)
    {
        read(a);
        h[a]=1;
    }
    dfs(a,a,0);//必须以a为点进行遍历,不能随便以某个点进行遍历,否则会额外增加必须要走的点的数量
    for1(i,n)if(vis[i]&&dist[i]>dist[a])a=i;
    memset(vis,0,sizeof vis);
    dfs(a,a,0);
    int sum=0,maxn=0;
    for1(i,n)if(vis[i])
    {
        sum++;
        maxn=max(maxn,dist[i]);
    }
    for1(i,n)if(vis[i]&&dist[i]==maxn)a=min(a,i);
    cout<<a<<"\n"<<(sum-1)*2-maxn<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值