1021. Deepest Root (25)

来源:互联网 发布:中小学生交通事故数据 编辑:程序博客网 时间:2024/06/05 16:48

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 (<=10000) 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

算法分析:采用领接矩阵存储图,用两次深度优先搜索遍历图,第一次遍历是判断给定的图是否是连通图,如果不是则计算连通图的个数然后输出‘错误’,随便找出最深的顶点;第二次遍历是以第一次遍历中找到的顶点其中任意一个为出发点,再找出最深的顶点。结合前两次遍历中找到的最深的顶点,按从小到大输出。

#include <stdio.h>#include <stdlib.h>int g[10001][10001] = {{0}}; //图//depth表示每个顶点的深度,第一个顶点深度为1;result表示符合要求顶点的集合。int visited[10001] = {0}, depth[10001] = {0}, result[10001] = {0};int n;void dfs(int v, int level) //深度优先遍历,并更新每个顶点的深度{    int i;    visited[v] = 1;    depth[v] = level;    for(i = 1; i <= n; i++)    {        if(!visited[i] && g[v][i] == 1)        {            dfs(i, level + 1);        }    }}void mark() //找出最深的顶点,并初始化{    int maxDepth = -1, i;    for(i = 1; i <= n; i++) //找到最深顶点的深度        if(depth[i] > maxDepth)            maxDepth = depth[i];    for(i = 1; i <= n; i++) //找到最深的顶点,并记录该顶点    {        if(depth[i] == maxDepth)            result[i] = 1;    }    for(i = 1; i <= n; i++) //初始化    {        depth[i] = 0;        visited[i] = 0;    }}int main(){    int i, count = 0;    scanf("%d", &n);    for(i = 0; i < n - 1; i++)    {        int v, w;        scanf("%d %d", &v, &w);        g[v][w] = g[w][v] = 1;    }    for(i = 1; i <= n; i++) //第一次遍历    {        if(!visited[i])        {            dfs(i, 1);            count++;        }    }    if(count > 1) //不是连通图        printf("Error: %d components", count);    else //是连通图    {        mark();         for(i = 1; i <= n; i++) 第二次遍历        {            if(result[i])            {                dfs(i, 1);                break;            }        }        mark();        for(i = 1; i <= n; i++)            if(result[i])                printf("%d\n", i);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 租户不变更地址怎么办 营业执照忘审了怎么办 工商营业执照年检过期怎么办 个体营业执照没有年报怎么办? 个体工商户一年没有申报怎么办 个体工商户逾期未申报怎么办 个体户没报税过怎么办 农业银行证书过期了怎么办 ca证书丢了怎么办 ca证书被锁怎么办 上个月忘记清卡怎么办 财务人员进入税务黑名单怎么办 社保本丢了怎么办 贷款车辆登记证书怎么办 发票薄丢了怎么办? 汽车发票丢了怎么办 税票弄丢了怎么办 交强险正本丢了怎么办 个体营业执照正本丢失怎么办 简易注销后税务怎么办 拒绝了日历邀请怎么办 老人走丢了怎么办 老人走丢找不到怎么办 没人给介绍对象怎么办 bate365账号被锁怎么办 qq号疑似被盗怎么办 不知道音乐名字怎么办 忘记支付宝登录怎么办 微信被老婆拉黑怎么办 微信群昵称改不了怎么办 微信号设置不了怎么办 修改微信号点不开怎么办 多屏设置失败怎么办 icould密码忘了怎么办 微信号换不了怎么办 微信号改不了怎么办? 无法设置微信号怎么办 公司改名后商标怎么办 公司名称变更后发票怎么办 被起诉公司企业变更怎么办 企业公章坏了怎么办