博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GYM 101522B. Bacteria Experiment
阅读量:7254 次
发布时间:2019-06-29

本文共 2746 字,大约阅读时间需要 9 分钟。

B. Bacteria Experiment

time limit per test

memory limit per test

input

output

A year after his bacteria experiment, Jason decided to perform another experiment on a new bacteria specie which evolves in a special way. The initial form of the bacteria can be considered as an undirected tree with N nodes in terms of graph theory. Every hour, an edge (x, y) is built if there exists a node z such that, in the previous hour, there exists edge (x, z) and edge (y, z), but not edge (x, y). The bacteria keep evolving until no more edges can be formed.

The following graph shows a type of bacteria which requires 2 hours to fully evolve:

img

As it may take months if not years for the bacteria to evolve to its ultimate form, it is impossible for Jason to stay at the laboratory to observe the change of the bacteria throughout the entire process. Therefore, he wants you to calculate the time required for the bacteria to fully evolve, so that he can just get back to the laboratory on time.

Input

The first line contains an integer N. (2 ≤ N ≤ 5 × 105)

The next N - 1 line each contains two integers u and v, which means there exists an edge between node u and v. (1 ≤ u, v ≤ N)

The given graph is guaranteed to be a valid tree.

Output

Output an integer, the time (in hours) required for the bacteria to fully evolve.

Example

input

61 55 35 66 26 4

output

2

题意

​ 给出一张图,每过一个小时相同父节点的子节点可以互相连接,问最多几个小时可以变成完成图。

解题思路

​ 找出这个图的最长的路。由于节点之间可以互相传递,例如1-2-3-4-5,第一个小时1和3连、2和4连、3和5连,第二个小时就可以1和5连,只要头和尾连接成功就结束。这样看下来,时间就是这条最长路的长度离的最近的2^n,而且向上取,n即为时间。

​ 关于求这个图的最长路,先以任意点开始bfs,最后到达的点一定是最长路的一个端点,然后再以这个点开始进行bfs,可以得到最长路的长度。

代码

#include
using namespace std;const int maxn = 5e5 + 5;#define inf 0x3f3f3f3fstruct node{ int x,step; node() {} node(int a,int b):x(a),step(b) {}} tmp;struct edge{ int to,next;} edges[maxn*2];int head[maxn],cnt,n;int vis[maxn];void init(){ cnt=0; memset(head,-1,sizeof(head));}void addedges(int u,int v){ edges[cnt].to=v; edges[cnt].next=head[u]; head[u]=cnt++;}void bfs(int r){ memset(vis,0,sizeof(vis)); queue
q; q.push(node(r,0)); vis[r]=1; while(!q.empty()) { node x=q.front(); q.pop(); tmp=x; for(int i=head[x.x]; ~i; i=edges[i].next) { if(vis[edges[i].to]) continue; vis[edges[i].to]=1; q.push(node(edges[i].to,x.step+1)); } }}int main(){ //freopen("in.txt","r",stdin); scanf("%d",&n); if(n==2) { printf("0\n"); return 0; } init(); int a,b; for(int i=1; i
arr[i-1]&&tmp.step<=arr[i]) { ans=i; break; } printf("%d\n",ans); return 0;}

转载于:https://www.cnblogs.com/RefrainLi/p/8861409.html

你可能感兴趣的文章
Flex获取屏幕的高度和宽度,浏览器窗口大小
查看>>
CodeForces 937D Sleepy Game
查看>>
杭电多校第二场 1005 hack it
查看>>
python MultiProcessing标准库使用Queue通信的注意要点
查看>>
JAVA入门之基础语言
查看>>
NSLayoutAttribute
查看>>
八种排序整理(二)----希尔排序
查看>>
SpringBoot图片上传(三)——调用文件上传项目的方法(同时启动两个项目)
查看>>
Windows下配置eclipse写WordCount
查看>>
【转载】 HDU 动态规划46题【只提供思路与状态转移方程】
查看>>
世界500强企业生存法则
查看>>
2015.4.8-C#入门基础(二)
查看>>
BZOJ-1303: [CQOI2009]中位数图 (思想)
查看>>
浅谈HTTP中Get与Post的区别[转载]
查看>>
useBean
查看>>
约瑟夫环C#解决方法
查看>>
ZeroMQ安装说明
查看>>
Ring3 Hook API
查看>>
size of pointer is not size of int
查看>>
插入排序
查看>>