【1094】The Largest Generation (25 分)

简介: 【1094】The Largest Generation (25 分)【1094】The Largest Generation (25 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;  
//key:用DFS递归遍历所有孩子结点,每次进入hashTable[level]加1
//另外key:Node[][]二维数组,Node[j]存放结点j的所有孩子结点编号
const int MAXN=110;//树的结点个数
vector<int> Node[MAXN];//树的静态写法,Node[i]存放结点i的孩子结点编号
int hashTable[MAXN]={0};  //记录每层的结点个数
void DFS(int index,int level){  //index为当前访问的结点编号
  hashTable[level]++; //第level层的结点个数加1
  for(int j=0;j<Node[index].size();j++){
    DFS(  Node[index][j] , level+1);  //遍历所有孩子结点,进行递归
  }
}
int main(){   
  int n,m,parent,k,child;
  scanf("%d%d",&n,&m);
  for(int i=0;i<m;i++){
    scanf("%d%d",&parent,&k); //父亲结点编号,孩子个数
    for(int j=0;j<k;j++){
      scanf("%d",&child);  //孩子结点编号,注意和for外面的scanf“隔开”
      Node[parent].push_back(child);  //建树
    }
  }
  DFS(1,1);  //根结点为1号结点,层号为1
  int maxLevel=-1,maxValue=0;
  for(int i=1;i<MAXN;i++){  //计算hashTable数组的最大值
    if(hashTable[i]>maxValue){
      maxValue=hashTable[i];
      maxLevel=i;
    }
  }
  printf("%d %d\n",maxValue,maxLevel);  //输出最大结点数与该层层号
  system("pause");
}
相关文章
|
机器学习/深度学习 人工智能 物联网
Lora升级!ReLoRa!最新论文 High-Rank Training Through Low-Rank Updates
Lora升级!ReLoRa!最新论文 High-Rank Training Through Low-Rank Updates
104 0
|
机器学习/深度学习 C++
【PAT甲级 - C++题解】1094 The Largest Generation
【PAT甲级 - C++题解】1094 The Largest Generation
68 0
PAT甲级 1004. Counting Leaves(30分)
PAT甲级 1004. Counting Leaves(30分)
69 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
114 0
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
110 0
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
105 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
126 0
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
143 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
112 0
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
114 0