1094. The Largest Generation (25)

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define maxn 200

struct node
{
  int data;
  int level;
  vector<int> child;
}tree[maxn];

int head = 1,N,K,max_level=1,max_cnt=0;
int father,n;
int has[maxn]={0};
void level_order(int head)
{
  queue<int> qu;
  qu.push(head);
  tree[head].level = 1;
  while (!qu.empty())
  {
    int front = qu.front();
    qu.pop();
    has[tree[front].level]++;
    for(int i=0; i<tree[front].child.size(); i++)
    {
      int child = tree[front].child[i];
      tree[child].level = tree[front].level+1;
      qu.push(child);
    }
  }
}
int main()
{

  //获取数据,并构造树
  scanf("%d%d",&N,&K);
  for(int i=0; i<K; i++)
  {    
    scanf("%d%d",&father,&n);
    for(int j=0; j<n; j++)
    {
      int x;
      scanf("%d",&x);
      tree[father].child.push_back(x);
    }
  }
  //层次遍历,并计算最大宽度
  level_order(head);
  //输出结果
  for(int i=0; i<maxn; i++)
  {
    if(has[i]>max_cnt)
    {
      max_cnt = has[i];
      max_level = i;
    }
  }
  printf("%d %d",max_cnt,max_level);
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值