题目描述
dfs
样例
#include<iostream>
#include<cstring>
#include<queue>
#include<unordered_map>
using namespace std;
int a[200];
int memo[200];
int dfs(int i)
{
if (i == 1)
{
return 1;
}
if (memo[i])
{
return memo[i];
}
return memo[i] = (dfs(a[i]) + 1);
}
int main()
{
int N, M;
cin >> N >> M;
memset(memo, 0, sizeof memo);
while (M--)
{
int parent;
int y;
scanf("%d %d", &parent, &y);
while (y--)
{
int child;
scanf("%d", &child);
a[child] = parent;
}
}
unordered_map<int, int> mp;
int ans = 0;
int c = 1;
for (int i = 1; i <= N; i++)
{
int t = dfs(i);
mp[t]++;
if(mp[t]>=mp[c])
{
ans=mp[t];
c=t;
}
}
cout << ans << " " << c;
return 0;
}