博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sicily 1034 Forest
阅读量:6505 次
发布时间:2019-06-24

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

hot3.png

Description

In the field of computer science, forest is important and deeply researched , it is a model for many data structures . Now it’s your job here to calculate the depth and width of given forests.

     Precisely, a forest here is a directed graph with neither loop nor two edges pointing to the same node. Nodes with no edge pointing to are roots, we define that roots are at level 0 . If there’s an edge points from node A to node B , then node B is called a child of node A , and we define that B is at level (k+1) if and only if A is at level k .

      We define the depth of a forest is the maximum level number of all the nodes , the width of a forest is the maximum number of nodes at the same level.

Input

There’re several test cases. For each case, in the first line there are two integer numbers n and m (1≤n≤100, 0≤m≤100, m≤n*n) indicating the number of nodes and edges respectively , then m lines followed , for each line of these m lines there are two integer numbers a and b (1≤a,b≤n)indicating there’s an edge pointing from a to b. Nodes are represented by numbers between 1 and n .n=0 indicates end of input.

Output

For each case output one line of answer , if it’s not a forest , i.e. there’s at least one loop or two edges pointing to the same node, output “INVALID”(without quotation mark), otherwise output the depth and width of the forest, separated by a white space.

Sample Input

1 01 11 13 11 32 21 22 10 88

Sample Output

0 1INVALID1 2INVALID

分析:

本题要求森林的深度和宽度,建议用深搜,因为广搜的循环控制条件很难写,极易出错或者死循环。要注意图生成的森林中,可能会出现公共子节点以及环等不合法情况,要注意判断条件。DFS过程中遇到了已经访问过的节点说明有公共子节点的存在,DFS完成后仍有节点未被访问说明有环存在。另外,一次完整的DFS过程中求得的广度只是一棵树的广度,要对同一深度的节点全部统计才能得到森林的广度。

代码:

// Problem#: 1034// Submission#: 1795986// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include 
#include
using namespace std;#define N 101#define M 101bool flag;bool buffer[N][N];bool visit[N];bool isroot[N];int maxd,n,m,maxb;int b[N];void dfs( int p, int td ){ if( visit[p] ){ flag = false; return ; } visit[p] = true; maxd = td>maxd ? td : maxd; if(++b[td]>maxb) maxb = b[td]; for( int i=1 ; i<=n ; i++ ){ if( buffer[p][i] ){ if(!flag) return ; dfs(i,td+1); }else continue; }}int main(){ int x,y; while( cin>>n>>m && n ){ memset(buffer,0,sizeof(buffer)); memset(visit,0,sizeof(visit)); memset(isroot,true,sizeof(isroot)); flag = true; if(m>=n) flag = false; for( int i=0 ; i
> x >> y; buffer[x][y] = true; isroot[y] = false; } maxd = maxb = 0; memset(b,0,sizeof(b)); for( int i=1 ; i<=n ; i++ ) if( isroot[i] ) dfs(i,0); for( int i=1 ; i<=n ; i++ ){ if( !visit[i] ){ flag = false; break; } } if( flag ) cout << maxd << " " << maxb; else cout << "INVALID"; cout << endl; } return 0;}

转载于:https://my.oschina.net/u/235391/blog/96618

你可能感兴趣的文章
《Java EE 7精粹》—— 2.5 非阻塞I/O
查看>>
《Python数据科学实践指南》一2.2 字符串
查看>>
《R数据可视化手册》——1.1 安装包
查看>>
《iOS创意程序设计家》——导读
查看>>
spring-aop
查看>>
android RecycleView Adapter简单封装
查看>>
PgSQL · 案例分享 · 递归收敛优化
查看>>
Dart的数据库操作
查看>>
Codeforces 591 B Rebranding【Codeforces Round #327 (Div. 2)】
查看>>
命名难,难于上青天
查看>>
做完和做好不一样
查看>>
APUE读书笔记-05标准输入输出库(7)
查看>>
23 第一周作业
查看>>
DNS解析偶尔延迟
查看>>
iOS打电话,发短信,发邮件,打开网址
查看>>
06-验证码-基本功能实现
查看>>
Java数据结构与算法(六) 希尔排序
查看>>
canvas学习笔记
查看>>
IntelliJ Idea下Go项目开启Debug调试
查看>>
elasticsearch安装步骤
查看>>