最短meeting路线(树的直径)--牛客第四场(meeting)

题意:

给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少。

思路:

其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 void swapp(int &a,int &b);
 29 double fabss(double a);
 30 int maxx(int a,int b);
 31 int minn(int a,int b);
 32 int Del_bit_1(int n);
 33 int lowbit(int n);
 34 int abss(int a);
 35 //const long long INF=(1LL<<60);
 36 const double E=2.718281828;
 37 const double PI=acos(-1.0);
 38 const int inf=(1<<29);
 39 const double ESP=1e-9;
 40 const int mod=(int)1e9+7;
 41 const int N=(int)1e5+10;
 42  
 43 vector<vector<int> >v(N);
 44 bool is[N],vis[N];
 45 int pre1,pre2;
 46 int max_=-1;
 47  
 48 void dfs1(int u,int depth)
 49 {
 50     if(is[u])
 51     {
 52         if(max_<depth)
 53             pre2=u,max_=depth;
 54     }
 55     vis[u]=1;
 56     int sz=v[u].size();
 57     fo(i,0,sz-1)
 58     {
 59         int to=v[u][i];
 60         if(!vis[to])
 61             dfs1(to,depth+1);
 62     }
 63 }
 64 void dfs2(int u,int depth)
 65 {
 66     vis[u]=1;
 67     if(is[u])
 68         max_=maxx(max_,depth);
 69     int sz=v[u].size();
 70     fo(i,0,sz-1)
 71     {
 72         int to=v[u][i];
 73         if(!vis[to])
 74             dfs1(to,depth+1);
 75     }
 76 }
 77  
 78 int main()
 79 {
 80     int n,k;
 81     sc("%d%d",&n,&k);
 82     fo(i,1,n-1)
 83     {
 84         int a,b;
 85         sc("%d%d",&a,&b);
 86         v[a].push_back(b);
 87         v[b].push_back(a);
 88     }
 89     fo(i,1,k)
 90     {
 91         int a;
 92         sc("%d",&a);
 93         is[a]=1;
 94         pre1=a;
 95     }
 96     dfs1(pre1,1);
 97     mem(vis,0);
 98     max_=-1;
 99     dfs2(pre2,0);
100     pr("%d\n",(max_+1)/2);
101     return 0;
102 }
103  
104 /**************************************************************************************/
105  
106 int maxx(int a,int b)
107 {
108     return a>b?a:b;
109 }
110  
111 void swapp(int &a,int &b)
112 {
113     a^=b^=a^=b;
114 }
115  
116 int lowbit(int n)
117 {
118     return n&(-n);
119 }
120  
121 int Del_bit_1(int n)
122 {
123     return n&(n-1);
124 }
125  
126 int abss(int a)
127 {
128     return a>0?a:-a;
129 }
130  
131 double fabss(double a)
132 {
133     return a>0?a:-a;
134 }
135  
136 int minn(int a,int b)
137 {
138     return a<b?a:b;
139 }

 

posted @ 2019-08-30 21:10  ZMWLxh  阅读(209)  评论(0编辑  收藏  举报