codeforce 120F 【树的直径】
来源:互联网 发布:python 元组 相等 编辑:程序博客网 时间:2024/05/24 06:43
One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders will hang, the more mum is going to like it and then she won't throw his favourite toys away. Help Petya carry out the plan.
A spider consists of k beads tied together by k - 1 threads. Each thread connects two different beads, at that any pair of beads that make up a spider is either directly connected by a thread, or is connected via some chain of threads and beads.
Petya may glue spiders together directly gluing their beads. The length of each thread equals 1. The sizes of the beads can be neglected. That's why we can consider that gluing spiders happens by identifying some of the beads (see the picture). Besides, the construction resulting from the gluing process should also represent a spider, that is, it should have the given features.
After Petya glues all spiders together, he measures the length of the resulting toy. The distance between a pair of beads is identified as the total length of the threads that connect these two beads. The length of the resulting construction is the largest distance between all pairs of beads. Petya wants to make the spider whose length is as much as possible.
The picture two shows two spiders from the second sample. We can glue to the bead number 2 of the first spider the bead number 1 of the second spider. The threads in the spiders that form the sequence of threads of maximum lengths are highlighted on the picture.
The first input file line contains one integer n (1 ≤ n ≤ 100) — the number of spiders. Next n lines contain the descriptions of each spider: integer ni (2 ≤ ni ≤ 100) — the number of beads, then ni - 1 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbered from 1 to ni.
Print a single number — the length of the required construction.
13 1 2 2 3
2
23 1 2 1 34 1 2 2 3 2 4
4
25 1 2 2 3 3 4 3 57 3 4 1 2 2 4 4 6 2 7 6 5
7
解题思路:
把每棵树的直径加起来就可以了,注意这题是文件输入输出。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<map>#include<string>#include<queue>#include<vector>#include<list>#include<bitset>//#pragma comment(linker,"/STACK:1024000000,1024000000")using namespace std;typedef long long ll;#define INF 0x3f3f3f3fvector<int> G[105];int n,m;int dis[105];int bfs(int u){ queue<int> que; que.push(u); dis[u]=0; int Max=0,Maxi=1; while(!que.empty()) { int f=que.front();que.pop(); if(dis[f]>Max) Maxi=f; for(int i=0;i<G[f].size();i++) { int to=G[f][i]; if(dis[to]==-1) que.push(to),dis[to]=dis[f]+1; } } return Maxi;}int main(){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); while(~scanf("%d",&n)) { int ans=0; for(int i=1;i<=n;i++) { for(int j=0;j<105;j++) G[j].clear(); scanf("%d",&m); for(int j=1;j<m;j++) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } memset(dis,-1,sizeof dis); int p=bfs(1); memset(dis,-1,sizeof dis); int q=bfs(p); ans+=dis[q]; } printf("%d\n",ans); } return 0;}
- codeforce 120F 【树的直径】
- Codeforce 337D(树的直径变形)
- bzoj3124 [Sdoi2013]直径 树的直径
- codeforce 899F
- 【树的直径】
- 树的直径
- 求树的直径
- 树的最长直径
- HDU4607 树的直径
- URAL1056(树的直径)
- 树的直径hdu3721
- 树的直径
- 树的直径
- 树的重心、直径
- 树的直径
- 树的直径 【总结】
- poj1985 树的直径
- 树的直径
- Java序列化与反序列化
- 读写操作
- Eclipse使用Git基本流程
- CSS3弹性伸缩布局(二)——flex布局
- 数据预处理之独热编码(One-Hot Encoding)
- codeforce 120F 【树的直径】
- OJ题:将一个数倒置输出
- 1717 小数化分数2
- 给LinearLayout设置动画(可以逐条出来动画效果)
- AndroidScannerDemo移植总结
- linux下获取程序当前目录绝对路径
- sigaction函数解析
- API 返回结果设计经验与总结
- ARM中断原理, 中断嵌套的误解