1034. Forest
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
Problem Source
ZSUACM Team Member
题目要求树的最大深度和最大宽度,不过题目中说的树未必是那种只有一个根节点的树,可能是多棵树(即有多个根节点)组成的树集。
注意题目给出的不成树的充分条件是有两个节点指向同一个子节点或者成环,除此以外还要注意自身成环(即自身指向自身的情况)和没有根节点的情况。之后就是利用深搜分别遍历每个节点来不停的更新最大深度和最大宽度即可。
// Problem#: 1034
// Submission#: 5054912
// 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<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
#include<cstring>
#define Maxn 101
using namespace std;
struct node{ //树种的一个节点,child容器保存它的子节点,father保存它的父节点
vector<int> child;
vector<int> father;
};
vector<int> tmp;//深搜的时候的路径节点,记录搜索路径,同时可以用来判断是否成环(即是否会向这个容器中添加相同的节点)
node tree[Maxn];//用来保存树节点的一维数组
int n,m,isInvalid,level;//n为节点数,m为边数,isInvalid用来判断是否是一棵树,level用来保存当前深度
int maxlevel;//用来保存最大深度
int width[Maxn];//用来保存每层的宽度
void readData()
{
int a,b;
for(int i=1;i<=n;++i)//清空父子容器
{
tree[i].child.clear();
tree[i].father.clear();
}
for(int i=0;i<m;++i)
{
cin>>a>>b;
if(a==b)//自身成环,不构成一棵树,所以isInvalid置为1
{
isInvalid=1;
}
tree[a].child.push_back(b);
tree[b].father.push_back(a);
}
}
void dfs(int pos)//深搜来求深度和宽度 ,pos为即将加入路径的节点
{
if(find(tmp.begin(),tmp.end(),pos)!=tmp.end()||isInvalid==1)//若路径容器中已存在了pos,则说明成环,故不构成树
{
isInvalid=1;
return;
}
else //否则则将pos加入路径中
{
tmp.push_back(pos);
level++;//当前深度加一
width[level]++;//当前层宽度加一
if(tree[pos].child.size()==0)//若pos是叶节点,则将根到这一叶节点的深度与最大深度进行比较
{
maxlevel=max(maxlevel,level);
}
else//若不为叶节点,则继续向下遍历
{
for(int i=0;i<tree[pos].child.size();++i)
{
dfs(tree[pos].child[i]);
tmp.pop_back();//注意回溯
level--;
}
}
}
}
int maxwidth()//width数组保存了各层的宽度,找出其中的最大值即为整棵树的宽度
{
int maxw=-1;
for(int i=0;i<Maxn;++i)
{
if(width[i]>maxw)
maxw=width[i];
}
return maxw;
}
int main()
{
while(cin>>n>>m&&n)
{
if(m==0) cout<<0<<" "<<n<<endl;//若边数为0则相当于将n个节点一字排开,所以深度为0,宽度为n
else
{
isInvalid=0;
memset(width,0,sizeof(width));
readData();
tmp.clear();
maxlevel=-1;
for(int i=1;i<=n;++i)//若两个父节点指向同一个子节点也不能成树,这一循环判断是否有节点父节点的数量大于1,若有则将isInvalid置为1
{
if(tree[i].father.size()>1)
isInvalid=1;
}
if(isInvalid) cout<<"INVALID"<<endl;//先进行一次验证,若不成树则直接输出
else
{
isInvalid=1;
for(int i=1;i<=n;++i)//父节点数量为0的节点即为根节点,先行将valid置为1,若没有节点的父节点数量为0,即无根节点,则valid仍然为1,表明无法构成树
{
level=-1;
if(tree[i].father.size()==0)//如果有根节点,则将valid置为0,并分别对每个根节点进行深搜
{
isInvalid=0;
dfs(i);
if(isInvalid)
{
break;
}
}
}
if(isInvalid) cout<<"INVALID"<<endl;
else cout<<maxlevel<<" "<<maxwidth()<<endl;
}
}
}
return 0;
}