[3884] Luo Gu binary tree problem

Title Description

Between a binary tree shown below the depth, width and distances of the node:

Depth: 4 Width: 4 (the same level up to the number of nodes)

Distance between nodes: ⑧ → ⑥ is 8 (3 × 2 + 2 = 8)

⑥ → ⑦ of 3 (1 × 2 + 1 = 3)

Note: The definition of the distance between the node: a node × number of edges at the root of the direction (uplink) 2,

When the leaf nodes from the root to the direction (downlink direction) and the number of edges.

Input and output formats

Input formats:

 

A first behavior file input integer n (1≤n≤100), represents the number of binary tree nodes. The next row n-1, x represents from node to node y (root convention is 1), the last line of two integers u, v, u represents the seek distance from the node to the node v.

 

Output formats:

 

Three numbers, each number representing one line sequentially represents a given binary tree of depth, width and distance between the node u to node v.

 

Sample input and output

Input Sample # 1:  Copy
10                                
1 2                            
1 3                            
2 4
2 5
3 6
3 7
5 8
5 9
6 10
8 6
Output Sample # 1:  Copy
4
4
8

 

 

Problem solution: LCA trained, trained, trained,

#include<bits/stdc++.h>
using namespace std;
int n;
struct edge{
    int to,next,val;
}G[100010];
int tot;
int maxn=0;
int depth[100010];
int st[100010][25];
int dist[100010];
bool used[100010];
int head[100010];
inline void addedge(int a,int b)
{
    G[tot].to=b;
    G[tot].next=head[a];
    G[tot].val=1;
    head[a]=tot++;
} 
void clear()
  {
       memset(used,false,sizeof(used));
       memset(head,-1,sizeof(head));
       memset(depth,0,sizeof(depth));
  }
  inline void tree(int s)
  {
      used[s]=1;
      for(int i=head[s];~i;i=G[i].next)
{
     int tt=G[i].to;
     if(used[tt])
     continue;
     dist[tt]=dist[s]+G[i].val;
      depth[tt]=depth[s]+1;
      maxn=max(depth[tt],maxn);
      st[tt][0]=s;
      for(int j=1;j<=20;j++)
      st[tt][j]=st[st[tt][j-1]][j-1];
      tree(tt);
}
   } 
int lca(int a,int b)
   {
        if(depth[a]>depth[b])
        swap(a,b);
        int d=depth[b]-depth[a];
         for(int i=0;(1<<i)<=d;i++)
          {
              if((1<<i)&d)
              b=st[b][i];
          }
          if(a==b)
          return a;
          for(int i=20;i>=0;i--)
           {
               if(st[a][i]!=st[b][i])
               {
                   a=st[a][i];
                   b=st[b][i];
               }
           }
           return st[a][0];
    } 
    int p,q;
int main()
{
    clear();
    scanf("%d",&n);
     for(int i=1;i<n;i++)
     {
          int a,b;
          scanf("%d %d",&a,&b);
           addedge(a,b);
      addedge(b,a);
      }
      scanf("%d %d",&p,&q);
      depth[1]=1;
      dist[1]=0;
      tree(1);
    printf("%d\n",maxn);
    int wide=0;
     for(int i=1;i<=maxn;i++)
     {
         int ans=0;
         int t=i;
          for(int j=1;j<=n;j++)
          {
              if(depth[j]==t)
               ans++;
          }
          wide=max(ans,wide);
     }
     // printf("%d %d\n",dist[p],dist[q]);
    // printf("%d %d %d",dist[p],dist[q],dist[lca(p,q)]);
      printf("%d\n%d",wide,2*(dist[p]-dist[lca(p,q)])+dist[q]-dist[lca(p,q)]);
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11229233.html