# Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

# 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

#include <iostream>
#include <vector>
#include <memory.h>

struct Node {
int indegree;
std::vector<int> child;
};

int main()
{
int num, edge;
while (std::cin >> num >> edge && num != 0) {
Node nodes[num+1];
for (int i = 1; i <= num; ++i) {
nodes[i].indegree = 0;
}
bool visited[num+1];
memset(visited, false, sizeof(visited));

bool valid = true;
int papa, son;
for (int i = 0; i < edge; ++i) {
std::cin >> papa >> son;
nodes[son].indegree++;
nodes[papa].child.push_back(son);
if (nodes[son].indegree == 2)
valid = false;
}
if (!valid) {
std::cout << "INVALID" << std::endl;
continue;
}

int height = -1;
int width = 0;
int count = 0;

while (true) {
std::vector<int> root;
for (int i = 1;  i <= num; ++i) {
if (!visited[i] && nodes[i].indegree == 0) {
root.push_back(i);
}
}
if (root.size() == 0)
break;
width = width < root.size() ? root.size() : width;
for (int i = 0; i < root.size(); ++i) {
visited[root[i]] = true;
count++;
for (int j = 0; j < nodes[root[i]].child.size(); ++j)
nodes[nodes[root[i]].child[j]].indegree--;
}
height++;
}
if (count == num) std::cout << height << " " << width << std::endl;
else std::cout << "INVALID" << std::endl;
}
}

#### [sicily online]1034. Forest

2012-11-30 16:30:09

#### Sicily 1034. Forest

2014-10-22 11:23:46

#### sicily1034 Forest

2014-11-04 00:33:53

#### 1034. Forest

2014-11-10 15:03:46

#### 1034. Forest

2010-07-11 00:48:00

#### SOJ 1034. Forest

2013-08-23 20:32:26

#### soj 1034. Forest

2017-02-20 19:55:55

#### 1034_有理数四则运算(20)

2015-03-07 09:43:57

#### pat1034Head of a Gang (30)

2015-10-10 18:30:08

#### 中大SICILY分类

2011-11-03 20:18:21

## 不良信息举报

sicily 1034. Forest