传送门.

题意:有一棵树

A 在点 1,B 在点 n
A的移动速度是每秒走过一条边,B的移动速度是每秒走过两条边(也可以只走一条)
前 t 秒 A 在不断的走向 B,B 不动,之后A还是逃,B开始追,问最迟什么时候追上

思路:

1、dfs找到t秒后A的位置node
2、dfs1求出B到追到每个点的时间
3、dfs2遍历A逃到每个点的时间,如果小于B追到的时间就继续逃,否则就更新ans

代码
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
// #include <tr1/unordered_map>
#define ll long long
#define T int t;scanf("%d", &t);while(t--)
using namespace std;
// using namespace std::tr1;
const int mod = 1e9 + 7;
const int N = 1e7 + 10;
int n, t;
int node;   //t秒后A的位置
int cnt;
int head[N];
int t1[N];      //B追到的时间
int ans = 0;
struct ed{int to, ne;
}e[N<<4];
void add(int u, int v){e[cnt].to = v;e[cnt].ne = head[u];head[u] = cnt++;
}int dfs(int u,int f,int step){ //找t秒后A的位置nodeif(u == n) return 1;    //如果这条路能到达B return 1for(int i = head[u]; i != -1; i = e[i].ne){int v = e[i].to;if(v==f) continue;int m = dfs(v,u,step+1);   if(step == t && m==1){  //如果第t秒所在位置是去往B的路上 更新值node = u;}if(m==1) return 1;   }return 0;
}
void dfs1(int u, int f,int step, int op){   //记录B追到每个点的时间t1[u] = step; for(int i = head[u]; i != -1; i = e[i].ne){int v = e[i].to;if(v==f) continue;if(op)                   //一秒可以跑两米dfs1(v,u,step+1,0);       else dfs1(v,u,step,1);}
}
void dfs2(int u,int f, int step){       //搜索答案ans = max(ans,t1[u]);            for(int i = head[u]; i != -1; i = e[i].ne){int v = e[i].to;if(v==f) continue;if(step+1<t1[v]) dfs2(v,u,step+1);      //如果逃到v的时间小于追到的时间,继续逃,否则更新anselse ans = max(ans,t1[v]);}
}
int main(){int cnt = 0;memset(head,-1,sizeof(head));scanf("%d %d", &n, &t);for(int i = 1; i < n; i++) {int u, v;scanf("%d %d", &u, &v);add(u,v);add(v,u);}dfs(1,-1,0);dfs1(n,-1,0,1);dfs2(node,-1,0);printf("%d\n", ans);return 0;
}
/*      测试样例
9 2
1 8
8 2
2 3
2 4
2 7
7 9
4 5
5 6
*/

2020牛客暑期多校训练营(第九场) The Flee Plan of Groundhog相关推荐

  1. 2020牛客暑期多校训练营(第九场)E题 Groundhog Chasing Death

    题意 计算 ∏ i = a b ∏ j = c d g c d ( x i , y j ) \prod_{i=a}^{b}\prod_{j=c}^{d}gcd(x^i,y^j) i=a∏b​j=c∏d ...

  2. E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解)

    E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解) 链接:https://ac.nowcoder.com/acm/contest ...

  3. 2020牛客暑期多校训练营(第六场)

    2020牛客暑期多校训练营(第六场) 额,睡了一下午,直接错过了比赛... 文章目录 A African Sort 题意: 题解: 代码: B Binary Vector C Combination ...

  4. 2020牛客暑期多校训练营(第四场)

    2020牛客暑期多校训练营(第四场) 这场属实有点难受 文章目录 A Ancient Distance B Basic Gcd Problem 题目 代码: C Count New String D ...

  5. 2020牛客暑期多校训练营(第一场)

    文章目录 A B-Suffix Array B Infinite Tree C Domino D Quadratic Form E Counting Spanning Trees F Infinite ...

  6. 2020牛客暑期多校训练营(第二场)

    2020牛客暑期多校训练营(第二场) 最烦英语题 文章目录 A All with Pairs B Boundary C Cover the Tree D Duration E Exclusive OR ...

  7. 2020牛客暑期多校训练营(第七场)J.Pointer Analysis

    2020牛客暑期多校训练营(第七场)J.Pointer Analysis 题目链接 题目描述 Pointer analysis, which aims to figure out which obje ...

  8. 2020牛客暑期多校训练营(第三场)A.Clam and Fish

    2020牛客暑期多校训练营(第三场)A.Clam and Fish 题目链接 题目描述 There is a fishing game as following: The game contains ...

  9. 2020牛客暑期多校训练营(第五场)——E Bogo Sort

    2020牛客暑期多校训练营(第五场)--E Bogo Sort 题目描述 Today Tonnnny the monkey learned a new algorithm called Bogo So ...

  10. 2020牛客暑期多校训练营(第一场)A B-Suffix Array(后缀数组,思维)

    链接:https://ac.nowcoder.com/acm/contest/5666/A 来源:牛客网 题目描述 The BBB-function B(t1t2-tk)=b1b2-bkB(t_1 t ...

最新文章

  1. Java Timer 定时器的使用
  2. **PHP foreach 如何判断为数组最后一个最高效?
  3. 获取AD用户和OU属性字段名称
  4. python数据结构与算法之排序
  5. InvisionApp学习
  6. java final 初始化_[转]java static final 初始化
  7. Python使用scrapy框架编写自动爬虫爬取京东商品信息并写入数据库
  8. 硬刚Hive | 4万字基础调优面试小总结
  9. 一种标准地图服务转矢量Shapefile方法
  10. C51最小单片机系统
  11. 75.(leaflet之家)leaflet柱状图
  12. FIL在十月份的ICO流通减产
  13. 创易手机--真正DIY手机
  14. P1039 侦探推理
  15. 磁珠法DNA pull down试剂盒、蛋白质-核酸相互作用
  16. 《现代命令行工具指南》9. 删除文件:让删除文件变得安全可控 - trash-cli
  17. Youtube的个人视频门户:Portal:Youtube director
  18. 设计师7个细微但是高效有用的习惯
  19. 转一篇很好的AD转换设计中的基本问题整理
  20. 第四届中国区块链开发大赛,权威投资机构将直接对接获奖团队

热门文章

  1. 北大邮箱收件服务器,邮箱手机客户端设置说明
  2. Centos7 SGE安装部署
  3. harmonyos在哪里使用,HarmonyOS 2.0 手机版使用初体验 ——手机开发者 (Beta版)
  4. Java之美[从菜鸟到高手演变]之Java学习方法
  5. Google Authenticator(谷歌身份验证器)C#版
  6. 【青松资讯】Anonymous匿名者黑客组织公布攻击新目标:100家中国政府网站
  7. 关于论坛数据库的设计
  8. linux定时任务生效_linux ( crontab 定时任务命令)
  9. 揭开 Flutter 跨平台开源框架的神秘面纱
  10. WPF Deactivated和Activated简单使用