【PAT-1094】 The Largest Generation (25分)
分析: 简单DFS算法
注意:数据的处理,以及根结点从1开始
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 110;
int n, m;
bool g[maxn][maxn];
int cnt[maxn];
void dfs(int u, int depth)
{
cnt[depth]++;
for (int i = 1; i <= n; i++)
{
if (g[u][i])
{
dfs(i, depth + 1);
}
}
}
int main(int argc, char const *argv[])
{
cin >> n >> m;
while (m--)
{
int id, k;
cin >> id >> k;
while (k--)
{
int son;
cin >> son;
g[id][son] = true;
}
}
dfs(1, 1);
int max_nodes = -1, max_depth = -1;
for (int i = 1; i <= n; i++)
{
if (cnt[i] > max_nodes)
{
max_nodes = cnt[i];
max_depth = i;
}
}
cout << max_nodes << " " << max_depth << endl;
return 0;
}