[sicily online]1034. Forest

來源:互聯網
上載者:User
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}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.