Title Address: 1034. Forest
Ideas:
Cond.......
The specific code is as follows:
1#include <iostream>2#include <cstring>3#include <queue>4 using namespacestd;5 6 BOOLpath[101][101];7 BOOLvisited[101];8 BOOLroot[101];9 Ten intMain () One { A intN, M; - while(Cin >> n >> M &&N) - { thememset (Path,false,sizeof(path)); -Memset (visited,false,sizeof(visited)); -memset (Root,true,sizeof(Root)); - + BOOLFlag = n > M?true:false; - for(inti =1; I <= m; i++) + { A intNode1, Node2; atCIN >> Node1 >>Node2; - if(Node1 = = node2) flag =false; -PATH[NODE1][NODE2] =true; - } - if(Flag = =false) { -cout <<"invalid\n"; in Continue; - } to + for(inti =1; I <= N; i++) - for(intj =1; J <= N; J + +) the if(Path[j][i]) *Root[i] =false; $ intMaxWidth =0;Panax Notoginseng for(inti =1; I <= N; i++) - if(Root[i]) { themaxwidth++; +Visited[i] =true; A } thequeue<int>store; + intdepth, maxdepth; -maxdepth = Depth =0; $ intwidth[101] = {0}; $ for(inti =1; I <= N; i++) - { - if(Root[i]) the { - Store.push (i);Wuyidepth =0; the while(!store.empty ()) - { Wu intSize =store.size (); -Width[depth] + =size; About while(size--) $ { - for(intj =1; J <= N; J + +) - if(Path[store.front ()][j]) - { A if(!Visited[j]) { + Store.push (j); theVISITED[J] =true; - } $ Else theFlag =false; the } the Store.pop (); the } - if(!store.empty ()) indepth++; the } themaxdepth = depth > maxdepth?depth:maxdepth; About } the } the the for(inti =1; I <= N; i++) + if(!Visited[i]) { -Flag =false; the Break;Bayi } the the for(inti =0; I <= maxdepth; i++) -MaxWidth = Width[i] > maxwidth?Width[i]: maxwidth; - theFlag = =false? cout <<"INVALID": cout << maxdepth <<" "<<MaxWidth; thecout <<Endl; the } the - return 0; the}
Sicily 1034. Forest