PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes' numbers.
Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components where K is the number of connected components in the graph.
Sample Input 1:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components
交了一发暴力,居然过了,关键就是这个求树的深度,这题的主要坑点在于,如果不是一颗树要输出那个信息,如果成环了但是只有一个连通块,要输出
Error: 1 components
所以注意判断逻辑
int dfs(int x)
{
    //de(x);
    vis[x]=1;
    int ma=0;
    for(int i=0;i<(int)G[x].size();i++){
        int v=G[x][i];
       if(vis[v])continue;
        ma=max(ma,dfs(G[x][i]));
    }
    return ma+1;
}
#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std;
const int maxn=1e4+5;
int father[maxn];   //  储存i的father父节点
void makeSet(int n) {
    for (int i = 1; i <=n; i++)
        father[i] = i;
}
int findRoot(int x) {   //  迭代找根节点
    int root = x; // 根节点
    while (root != father[root]) { // 寻找根节点
        root = father[root];
    }
    while (x != root) {
        int tmp = father[x];
        father[x] = root; // 根节点赋值
        x = tmp;
    }
    return root;
}
void Union(int x, int y) {  //  将x所在的集合和y所在的集合整合起来形成一个集合。
    int a, b;
    a = findRoot(x);
    b = findRoot(y);
    father[a] = b;  // y连在x的根节点上   或father[b] = a为x连在y的根节点上;
}
vector<int>G[maxn];
/*
5
1 2
1 3
1 4
2 5
*/
int maxx;
int depth[maxn];
int vis[maxn];
int dfs(int x)
{
    //de(x);
    vis[x]=1;
    int ma=0;
    for(int i=0;i<(int)G[x].size();i++){
        int v=G[x][i];
       if(vis[v])continue;
        ma=max(ma,dfs(G[x][i]));
    }
    return ma+1;
}
int main()
{
    int n;
    cin>>n;
    makeSet(n);
    int m=n-1;
    int a,b;
    int tree_flag=true;
    while(m--)
    {
        scanf("%d%d",&a,&b);
        if(findRoot(a)!=findRoot(b)){
            Union(a,b);
        G[a].push_back(b);
        G[b].push_back(a);
        }
        else tree_flag=false;
    }
    //int components=0;
    set<int>s;
    for(int i=1;i<=n;i++)
    {
        //de(i);
        //de(findRoot(i));
        s.insert(findRoot(i));
    }
    //de(s.size());
    if(tree_flag==false||s.size()!=1)
    {
        printf("Error: %d components\n",(int)s.size());
        return 0;
    }
    maxx=0;
    each(i,1,n)
    {
        memset(vis,0,sizeof(vis));
        //de(i);
        depth[i]=dfs(i);
    }
    each(i,1,n)
    {
        maxx=max(maxx,depth[i]);
    }
    each(i,1,n)
    {
        if(depth[i]==maxx)
        {
            cout<<i<<endl;
        }
    }
    return 0;
}
												
											PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度的更多相关文章
- 1021. Deepest Root (25)——DFS+并查集
		
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
 - PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
		
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
 - 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 ...
 - [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 ...
 - 1021 Deepest Root (25 分)
		
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
 - 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
		
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
 - PAT 1021 Deepest Root[并查集、dfs][难]
		
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
 - PAT (Advanced Level) 1021. Deepest Root (25)
		
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...
 - 1021. Deepest Root (25)
		
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
 
随机推荐
- C语言中size_t类型详细说明【转载】
			
来看看网上的一些说法: C语言 size_t到底是个什么东东? 大神求解 . 简单理解为 unsigned int就可以了 . 这是在不同的机器里面的的头文件定义的相应宏定义,实际上是unsigned ...
 - locust性能测试脚本模板
			
locust性能测试脚本模板 #!/usr/bin/env python # -*- coding: utf-8 -*- import time from locust import HttpLocu ...
 - 约翰·麦斯威尔 | John C. Maxwell | A leader is one who knows the way, goes the way, and shows the way.
			
约翰·麦斯威尔_百度百科https://baike.baidu.com/item/%E7%BA%A6%E7%BF%B0%C2%B7%E9%BA%A6%E6%96%AF%E5%A8%81%E5%B0%9 ...
 - typeScript模块<四>
			
/* 功能:定义一个操作数据库的库 支持 Mysql Mssql MongoDb 要求1:Mysql MsSql MongoDb功能一样 都有 add update delete get方法 注意:约 ...
 - Linux 在 TOP 命令中切换内存的显示单位
			
顶部的内存信息可以在top运行时按E切换,每次切换转换率为1000,只是没有单位,切换的单位为 k,m,g,t,p: 1. 2. 3., 4. 底下的进程信息按e切换,每次切换转换率为1000,切换的 ...
 - Oracle insert大量数据经验之谈
			
在很多时候,我们会需要对一个表进行插入大量的数据,并且希望在尽可能短的时间内完成该工作,这里,和大家分享下我平时在做大量数据insert的一些经验. 前提:在做insert数据之前,如果是非生产环境, ...
 - Python - Django - Cookie 简单用法
			
home.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
 - Python - Django - ORM 双下划线
			
id 字段: id__lt:id 小于,id__gt:id 大于 import os if __name__ == '__main__': # 加载 Django 项目的配置信息 os.environ ...
 - 关于PF_INET和AF_INET的区别
			
在写网络程序的时候,建立TCP socket: sock = socket(PF_INET, SOCK_STREAM, 0);然后在绑定本地地址或连接远程地址时需要初始化sockaddr_in结构 ...
 - Underscore.js 的模板功能
			
Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能. 无论你写一段小的js代码,还是写一 ...