【PAT甲级】1094 The Largest Generation (25 分)(DFS)

题意:

输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增)。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 vector<int>v[107];
 5 int ans[107];
 6 void dfs(int x,int storey){
 7     ++ans[storey];
 8     for(auto it:v[x])
 9         dfs(it,storey+1);
10 }
11 int main(){
12     ios::sync_with_stdio(false);
13     cin.tie(NULL);
14     cout.tie(NULL);
15     int n,m;
16     cin>>n>>m;
17     for(int i=1;i<=m;++i){
18         int x;
19         cin>>x;
20         int num;
21         cin>>num;
22         for(int j=1;j<=num;++j){
23             int y;
24             cin>>y;
25             v[x].push_back(y);
26         }
27     }
28     dfs(1,1);
29     int mx=0,pos=0;
30     for(int i=1;i<=n;++i)
31         if(ans[i]>mx){
32             mx=ans[i];
33             pos=i;
34         }
35     cout<<mx<<" "<<pos;
36     return 0;
37 }
保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
原文地址:https://www.cnblogs.com/ldudxy/p/11908656.html