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 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
題目分析:
大意就是判斷一個森林是否成立,並且求出該森林的寬度(橫向)和深度(縱向)
我是用map來儲存圖,key是標示,value是與它相連的點的集合,通過廣度優先遍曆(隊列)來求出每個節點所在的層,然後就很容易求出寬度和深度了
#include<iostream>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>using namespace std;typedef struct INFO{bool isUsed;bool isRoot;int layer;vector<int> child;}node;int main(){int n,m;while(cin>>n>>m&&n!=0){map<int, node>gra;//map應該不能指定尺寸,因為預設值都一樣,map是不準key一樣的for(int i=0;i<n;i++){//處理點node tmp;tmp.isUsed=false;tmp.isRoot=true;tmp.layer=0;gra[i]=tmp;}for(int i=0;i<m;i++){//處理邊int x1,x2;cin>>x1>>x2;gra[--x1].child.push_back(--x2);gra[x2].isRoot=false;}vector<int> root;//統計根節點for(int i=0;i<n;i++)if(gra[i].isRoot)root.push_back(i);int count=0;int flag=false;for(int i=0;i<root.size();i++){queue<int> st;count++;if(gra[root[i]].isUsed){//cout<<"INVALID"<<endl;flag=true;break;}gra[root[i]].isUsed=true;st.push(root[i]);while(!st.empty()){int index=st.front();st.pop();vector<int>::iterator ite;for(ite=gra[index].child.begin();ite!=gra[index].child.end();ite++){if(gra[*ite].isUsed){//cout<<"INVALID"<<endl;flag=true;break;}else{count++;gra[*ite].isUsed=true;gra[*ite].layer=gra[index].layer+1;st.push(*ite);}//end else}//edn forif(flag)break;}//end while}//end for iint width=1,height=0;map<int,int> tmpWidth;for(int i=0;i<n;i++){tmpWidth[gra[i].layer]++;if(gra[i].layer>height)height=gra[i].layer;}for(map<int,int>::iterator ite=tmpWidth.begin();ite!=tmpWidth.end();ite++)if(width<ite->second)width=ite->second;if(count<n||flag)cout<<"INVALID"<<endl;else cout<<height<<" "<<width<<endl;}//end whie}