Time Limit: 1 secs, Memory Limit: 32 MB
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.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.
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.
1 0
1 1
1 1
3 1
1 3
2 2
1 2
2 1
0 88
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 for if(flag)break; }//end while }//end for i int 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 }