• 题意
    • 一棵树上的n个点中有m个点中各有一个人,每个人在一个节点上,然后每小时朝有距离大于1的点的方向走到一个邻居节点上(多个的话等概率),问最少多长时间他们的距离都不超过1
    • n,m<1e3
  • 题解
    • 看起来求概率,实际上稍微分析后发现,不论怎么走,树上最远的电对之间的距离始终每次减2
    • 所以只要将有人的点看成树上真正的点,然后按照树的直径,从一个有人的点开始两次dfs/树形dp即可
  • 代码
    • 注意是双向边,所以图的规模=n*2
    • #include<algorithm>
      #include <iostream>
      #include  <fstream>
      #include  <cstdlib>
      #include  <cstring>
      #include  <cassert>
      #include   <cstdio>
      #include   <vector>
      #include   <string>
      #include    <cmath>
      #include    <queue>
      #include    <stack>
      #include      <set>
      #include      <map>
      using namespace std;
      #define P(a,b,c) make_pair(a,make_pair(b,c))
      #define rep(i,a,n) for (int i=a;i<=n;i++)
      #define per(i,a,n) for (int i=n;i>=a;i--)
      #define CLR(vis) memset(vis,0,sizeof(vis))
      #define MST(vis,pos) memset(vis,pos,sizeof(vis))
      #define pb push_back
      //#define mp make_pair
      #define all(x) (x).begin(),(x).end()
      #define fi first
      #define se second
      #define SZ(x) ((int)(x).size())
      typedef pair<int,pair<int,int> >pii;
      typedef long long ll;
      typedef unsigned long long ull;
      const ll mod = 1000000007;
      const ll INF = 1e18;
      ll gcd(ll a, ll b) {
      	return b ? gcd(b, a%b) : a;
      }
      template<class T>inline void gmax(T &A, T B) {
      	(A<B)&&(A=B);
      	//if(B>A)A=B;
      }
      template<class T>inline void gmin(T &A, T B) {
      	(A>B)&&(A=B);
      	//if(B<A)A=B;
      }
      template <class T>
      inline bool scan(T &ret) {
      	char c;
      	int sgn;
      	if(c=getchar(),c==EOF) return 0;
      	//EOF
      	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
      	sgn=(c=='-')?-1:1;
      	ret=(c=='-')?0:(c-'0');
      	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
      	ret*=sgn;
      	return 1;
      }
      inline void outt(int x) {
      	if(x>9) outt(x/10);
      	putchar(x%10+'0');
      }
      
      const int maxn = 1e3+10;
      int tot;
      int head[maxn], nxt[maxn*2], ver[maxn*2];
      void add(int x,int y) {
      	ver[++tot] = y;
      	nxt[tot] = head[x];
      	head[x] = tot;
      }
      
      int v[maxn];
      int root,ans;
      
      void init(int n) {
      	rep(i,1,n)v[i]=0,head[i]=0;
      	tot = 0;
      	ans = 0;
      }
      
      int vis[maxn];
      void dfs(int x,int step) {
      	vis[x] = 1;
      	if(v[x]) {
      		if(step>ans) {
      			ans = step;
      			root = x;
      		}
      	}
      	for(int i = head[x]; i; i=nxt[i]) {
      		int y = ver[i];
      		if(!vis[y]) {
      			dfs(y,step+1);
      		}
      	}
      }
      
      int main() {
      	int t;
      	scanf("%d", &t);
      	while(t--) {
      		int n,m;
      		scanf("%d%d", &n,&m);
      		init(n);
      		rep(i,1,m) {
      			int tmp;
      			scanf("%d", &tmp);
      			v[tmp] = 1;
      			root = tmp;
      		}
      		rep(i,1,n-1) {
      			int x,y;
      			scanf("%d%d", &x, &y);
      			add(x,y);
      			add(y,x);
      		}
      		rep(i,1,n)vis[i] = 0;
      		dfs(root,0);
      		rep(i,1,n)vis[i] = 0;
      		dfs(root,0);
      		printf("%d", ans/2);
      		puts(".00");
      	}
      	return 0;
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注