## sicily 1034 Forest 原

Ciel

### 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 0
1 1
1 1
3 1
1 3
2 2
1 2
2 1
0 88``````

### Sample Output

``````0 1
INVALID
1 2
INVALID``````

## 代码：

``````// Problem#: 1034
// Submission#: 1795986
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstring>
using namespace std;

#define N 101
#define M 101

bool 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<m ; i++ ){
cin >> 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;
}``````

### Ciel

--============Oracle ADG搭建============== --==========准备阶段========= 1.检查primary为archivelog模式。 select log_mode from v\$database; 如果为noarchivelog模式，切换到archivelo......

UltraSQL
2018/07/23
0
0
oracle查询数据重复

2013/01/08
489
2
Adobe Illustrator 各个版本注册码 序列号 破解补丁 下载地址专题

2016/01/11
10.6K
0
PHP实现微信开放平台扫码登录源码下载

2当家的
2017/02/06
1K
0

Random forest is a highly versatile machine learning method with numerous applications ranging from marketing to healthcare and insurance. It can be used to model the impact of ......

AC-carrot
2016/06/03
105
0

js如何控制table中的某一行动态置顶

4
0

dragon_tech

3
0

Springboot项目，需要调其他服务的接口，返回值类型是RestResultResponse 打断点的结果集是这个 打印出来的getData(): [{id=3336b624-8474-4dd9-bd5b-c7358687c877, paraNo=104, para=Postpo...

4
0
【小学】 生成10以内的加减法

#!/usr/bin/env python# coding: utf-8from random import randrange# 题目的最大数值R_MAX = 10# 生成的题目的数量R_PAGE = 70# 生成减法列表def get_sub_list():...

Tensor丨思悟

11
0
JavaScript设计模式——适配器模式

适配器模式是设计模式行为型模式中的一种模式； 　　定义： 　　适配器用来解决两个已有接口之间不匹配的问题，它并不需要考虑接口是如何实现，也不用考虑将来该如何修改；适配器不需要修...

3
0