之前不知道怎么判断是不是树,参考了

http://blog.csdn.net/eli850934234/article/details/8926263

但是最后有一个测试点有超时,在bfs里我用了数组拷贝,所以简化成直接访问超时就没有了。

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int bing[10005];
int n;
int bfs(int node);
vector<int> nodeList[10005];
vector<int> maxDis;
int vis[10005];
int unionFind(int a){
if(bing[a]==0) return a;
else return unionFind(bing[a]);
}
void unionUnion(int a,int b){
int ia=unionFind(a);
int ib=unionFind(b);
if(ia!=ib) bing[ia]=ib;
}
int bfs(int node){
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(node);
int co=1;
int co2=0;
int depth=0;
while(!q.empty()){
int fro=q.front();
vis[fro]=1;
q.pop();
for(int k=0;k<nodeList[fro].size();k++){
if(vis[nodeList[fro][k]]==0){
q.push(nodeList[fro][k]);
co2++;
}
}
co--;
if(co==0) {
co=co2;
co2=0;
depth++;
}
}
return depth;
} int main(){
cin>>n;
int i,j;
for(int m=0;m<n-1;m++){
cin>>i>>j;
unionUnion(i,j);
nodeList[i].push_back(j);
nodeList[j].push_back(i);
}
int count=0;
for(int m=1;m<=n;m++){
if(bing[m]==0) count++;
}
if(count!=1){
printf("Error: %d components",count);
return 0;
}
int max=0;
for(int m=1;m<=n;m++){
int dis=bfs(m);
if(max<dis){
maxDis.clear();
max=dis;
maxDis.push_back(m);
}else if(max==dis){
maxDis.push_back(m);
}
}
for(int m=0;m<(maxDis.size());m++){
cout<<maxDis[m]<<endl;
}
return 0;
}

PAT1021. Deepest Root (25)的更多相关文章

  1. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  2. PAT1021:Deepest Root

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  3. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  4. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  5. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  6. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. 1021 Deepest Root (25)(25 point(s))

    problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...

  8. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  9. PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]

    题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...

随机推荐

  1. hdu4266(三维凸包模板题)

    /*给出三维空间中的n个顶点,求解由这n个顶点构成的凸包表面的多边形个数. 增量法求解:首先任选4个点形成的一个四面体,然后每次新加一个点,分两种情况: 1> 在凸包内,则可以跳过 2> ...

  2. Android输入法的显示与隐藏

    显示输入法: public void ShowSoftInput(View v) { // v 接受输入的控件 mInputMethodManager = (InputMethodManager) ( ...

  3. Java的==与equals之辨

    =======================原理速记=================================== equals重载后比较内容,==比较指针.否则equls等同于== (Ja ...

  4. Python3.6全栈开发实例[006]

    6.检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者. dic = {"k1": "v1v1", " ...

  5. Inception 2.0

    文章<Rethinking the Inception Architecture for Computer Vision> 介绍 VGG与GoogLeNet相比更朴素,但计算量大.Goog ...

  6. Mybatis使用pageHelper步骤

    1.在pom.xml中添加如下依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artif ...

  7. 剑指offer 面试7题

    面试7题: 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{ ...

  8. 剑指offer 面试36题

    面试36题: 题:二叉搜索树与双向链表 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 解题思路一:由于输入的一个二叉搜索树, ...

  9. 转:ADO,OLEDB,ODBC,DAO的区别

    ODBC(Open Database Connectivity,开放数据库互连) 1992年,微软公司开放服务结构(WOSA,Windows Open Services Architecture)中有 ...

  10. 使用git从本地上传至git码云远程仓库

    从 http://git-scm.com/download  下载window版的客户端.下载好,一步一步安装即可. 使用前的基本设置 git  config --global user.name & ...