Cattle off more school fourth radius A meeting of tree

Meaning of the questions:

There is a tree, the tree has a lot of people, they want to party, find a point of making everyone a minimum to a maximum distance of this point.

answer:

First, a point of the root was, seeking a spanning tree, to delete all subtrees no ensure that all of the suspension point (a point side connection only) was all nodes determined to ensure that the diameter of the rear someone ends of the two nodes. Why not be someone node is the root of it? Because if you get a nobody when the root node, and this also happens to be the root of a suspension point, then you can imagine, this could be the end point of suspension diameter.

Second, find the diameter of the tree, the tree is the longest distance two points, the two endpoints of the diameter of the suspension points are inevitable.

Diameter / 2 is the radius of the rounded up, i.e. the required distance from the subject.

Seek diameter can run twice dfs, starting with the first pass at any point, to find the maximum points from this point, the second time starting from this point, and this point is to find the point of maximum distance.

I think of when competition is the center of the tree, wa, and imagine a tree half is a long chain, the lower half is packed in a long chain of his son's end. Then I want to find the diameter of the topology, T a.

But found several rounds of topological sort is actually the radius of the tree, this conclusion does not know what is the use.

#include <the iostream> 
#include <Vector>
 #define INF 0x3f3f3f3f 
#include <Queue> 
#include <CString> 
#include < SET >
 the using  namespace STD; 
Vector < int > LNK [ 100005 ]; 
Vector < int > sclnk [ 100005 ] ; // spanning tree 
int n-; 
 
int SCN; // spanning tree nodes 
BOOL REN [ 100005 ];
 BOOL DFS ( int FA, int U) {
    bool ok=ren[u];
    for(int i=0;i<lnk[u].size();i++){
        int v=lnk[u][i];
        if(v==fa)continue;
        bool tr=dfs(u,v);
        if(tr){
            sclnk[u].push_back(v);
            sclnk[v].push_back(u);
            scn++;
        }
        ok=ok||tr;
    }
    return ok;
}
int kg;
void prt(int fa,int u){
    //把生成树打印出来
    for(int i=1;i<=kg;i++)printf(" ");
    printf("%d\n",u);
    kg++;
    for(int i=0;i<sclnk[u].size();i++){
        int v=sclnk[u][i];
        if(v==fa)continue;
        prt(u,v);
    }
    kg--;
}
 
bool vis[100005];
int size[100005];
 
int dis[100005];
struct P{
    int n,dis;
    P(){}
    P(int n1,int dis1){
        n=n1;dis=dis1;
    }
    friend bool operator >(const P &a,const P &b){
        return a.dis>b.dis;
    }
    friend bool operator <(const P &a,const P &b){
        return a.dis<b.dis;
    }
};
 
 
void DDFS ( int ST) { 
    VIS [ST] = . 1 ;
     for ( int I = 0 ; I <sclnk [ST] .size (); I ++ ) {
         int to = sclnk [ST] [I];
         IF ! ( VIS [to]) { 
            DIS [to] = DIS [ST] + . 1 ; 
            DDFS (to); 
        } 
    } 
} 
 
int main () {
     // put was a point when the roots pulled out
     // delete no sub tree
     // find the diameter of the tree 
    int K; 
    Scanf ( " % D% D ",&n,&k);
    for(int i=1;i<n;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        lnk[a].push_back(b);
        lnk[b].push_back(a);
    }
    int r1;
    for(int i=1;i<=k;i++){
        int q;
        scanf("%d",&q);
        r1=q;
        ren[q]=1;
    }
    dfs(-1,r1);
//  prt(r1);
    //求生成树
      
    memset(dis,INF,sizeof dis);
    dis[r1] = 0;
    ddfs(r1);
    //求树的直径
    int maxi=r1;
    for(int i=1;i<=n;i++){
        if(dis[i]!=INF && dis[i]>dis[maxi])maxi=i;
    }
     
    memset(dis,INF,sizeof dis);
    memset(vis,0,sizeof vis);
    dis[maxi] = 0;
    ddfs(maxi);
     
     
    int maxx=0;
    for(int i=1;i<=n;i++){
//      printf("%d ",dis[i]);
        if(dis[i]!=INF)maxx=max(maxx,dis[i]);
    }
    printf("%d\n",(maxx+1)/2);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11257329.html