1034 forest
来源:互联网 发布:广电怎么看网络电视 编辑:程序博客网 时间:2024/05/17 03:21
大水题,WA到吐了,结果发现犯了一弱智错误,擦
1.定义dept[]数组,主要用bfs ,将子节点的dept值累加父节点的dept值,遍历一颗或者若干树。从根节点(bepointed[]值为false的点)出发遍历全棵树。
2.存储方式:邻接表存边 vector+queue ,
#include <iostream>#include<queue>#include<vector>#include<cstring>#include<algorithm>using namespace std;int n,m,a,b;const int N=102;bool bepointed[N];bool circle[N];int dept[N];bool flag;vector<int>vec[N];bool cmp(const int&a,const int &b){return a>b;}void bfs(int vertex){queue<int>que1;que1.push(vertex);while (!que1.empty()){int a=que1.front();circle[a]=true;que1.pop();for (int i=0;i<vec[a].size();i++){int j=vec[a][i];if(circle[j]==false){dept[j]=dept[a]+1;que1.push(j);}}}}int main(){while(cin>>n>>m&&n!=0){flag=0;memset(bepointed,false,sizeof(bepointed));memset(circle,false,sizeof(circle));memset(dept,0,sizeof(dept));fill(vec,vec+n+1,vector<int>());while (m--){cin>>a>>b;if(bepointed[b]==true) flag=1; //terrible mistake i've madebepointed[b]=true; vec[a].push_back(b);}if(flag){cout<<"INVALID"<<endl; continue;} int count=0;for (int i=1;i<=n;i++){if(bepointed[i]==false)bfs(i);elsecount++;}if(count==n){cout<<"INVALID"<<endl;continue;}int count2=0;for (int i=1;i<=n;i++)if(circle[i]==true)count2++;if(count2!=n) //value of count2 stands for the numbers of vertex that has been pushed into the queue.If it does not equal to n, loop exists{cout<<"INVALID"<<endl;continue;}if(flag) continue;int maxx=0;for (int i=1;i<=n;i++)maxx=max(maxx,dept[i]);int wid[102]={0};for (int i=1;i<=n;i++)wid[dept[i]]++;sort(wid,wid+102,cmp);cout<<maxx<<" "<<wid[0]<<endl;}return 0;}
0 0
- 1034 forest
- Sicily 1034 Forest(DFS)
- sicily 1034(Forest)
- Sicily 1034 Forest
- Sicily 1034 Forest
- Forest
- Forest
- 1034. Forest
- 1034. Forest
- Biridian Forest
- sicily1034. Forest
- 《Forest Gump》
- sicily1034 Forest
- 1034. Forest
- 1034. Forest
- Random Forest
- Random Forest
- <OJ_Sicily>Forest
- c++常用函数所在头文件一览
- Verilog中的wire和reg区别
- 描述性统计与性能结果分析
- MySQL性能优化的最佳20+条经验
- Merge Sorted Array
- 1034 forest
- JAVA的Random类(转)
- 比较器报错:Comparison method violates its general contract
- 第5天:散列HASH(二)冲突解决办法之线性再散列法
- ubuntu 连不到外网
- OpenCV247 鼠标选择ROI区域
- 第几天?
- 程序员,在大场面也要hold住
- 编程思想与编程技法是相互影响的两件事,然而也是不可拆分的一回事儿!