1004. Counting Leaves (30)
来源:互联网 发布:mac装win10后启动黑屏 编辑:程序博客网 时间:2024/06/07 23:22
PAT 1004 用一个 BFS 搞定 哈哈
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int maxn=105;
const int INF=1000000000;
struct E{
int from,to;
E(int from,int to):
from(from),to(to){}
bool operator <(const E &rhs) const{
return this->from<rhs.from;
}
};
vector<E> edges;
vector<int> G[maxn];
int deep[maxn];
int deep_zero[maxn];
void init()
{
for(int i=0;i<maxn;i++)
{
G[i].clear();
deep[i]=INF;
deep_zero[i]=0;
}
edges.clear();
deep[1]=1;
}
void addEdge(int from,int to)
{
edges.push_back(E(from,to));
G[from].push_back(edges.size()-1);
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif
int n,m,root,nums,child;
init();
scanf("%d%d",&n,&m);
for(int j=1;j<=m;j++)
{
scanf("%d%d",&root,&nums);
for(int i=1;i<=nums;i++)
{
scanf("%d",&child);
addEdge(root,child);
}
}
queue<int> nodes;
nodes.push(1);
deep[1]=1;
int max_deep=1;
while(!nodes.empty())
{
int from=nodes.front(); nodes.pop();
for(int i=0;i<G[from].size();i++)
{
int to=edges[G[from][i]].to;
deep[to]=deep[from]+1;
nodes.push(to);
if(deep[to]>max_deep)
max_deep=deep[to];
}
}
for(int i=1;i<=n;i++)
{
if(G[i].size()==0)
deep_zero[deep[i]]+=1;
}
for(int i=1;i<=max_deep;i++)
{
cout<<deep_zero[i];
if(i!=max_deep)
cout<<" ";
}
cout<<endl;
return 0;
}
阅读全文
0 0
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- ZCMU1779
- JAVA9 API翻译(Java® Platform, Standard Edition & Java Development Kit Version 9 API Specification)
- 输出一个桃心形状
- 20171113每日一练
- 10 种机器学习算法的要点(附 Python 和 R 代码)
- 1004. Counting Leaves (30)
- UVa 1586 水题
- 在WPF中添加WINFORM控件的方法
- github第一天学习总结
- BMA250E 加速度传感器和插接板,用于移动消费类电子产品
- leetcode Two Sum ----python3
- 电子线路设计技巧7:UC3843A升压电路中振荡频率和占空比的确定方法
- 吴恩达Coursera深度学习课程 DeepLearning.ai 编程作业(4-1)
- wiringpi安装编译问题解决