1094. The largest Generation (25)
Time limit
Ms
Memory limit
65536 KB
Code Length Limitations
16000 B
Procedures for the award of questions
Standard
Author
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same genera tion. Your task is to find the generation with the largest population.
Input Specification:
Each input file contains the one test case. Each case starts with a positive integers N (<100) which is the total number of family members in the tree (and hence Assume that all the members were numbered from () to N), and M (<n) which was the number of family members who had chil dren. Then M. lines follow, each contains the information of a family member in the following format:
ID K id[1] id[2] ... ID[K]
Where ID is a two-digit number representing a family member, K (>0) are the number of his/her children, followed by a SE Quence of Two-digit ID ' s of his/her children. For the sake of simplicity, let us fix the root ID to be 01. All the numbers in a line is separated by a space.
Output Specification:
For each test case, print on one line the largest population number and the level of the corresponding generation. It is assumed that such a generation are unique, and the root level was defined to be 1.
Sample Input:
23 13
21 1 23
01 4 03 02 04 05
03 3 06) 07 08
06 2 12 13
13 1 21
08 2 15 16
02 2 09 10
11 2 19 20
17 1 22
05 1 11
07 1 14
09 1 17
10 1 18
Sample Output:
9 4
It's also possible for students to use and check a set.
Six degree space http://blog.csdn.net/u013167299/article/details/42321615
#include <iostream> #include <cstring> #include <string> #include <cmath> #include <map># include<queue> #include <cstdio> #include <vector> #include <algorithm>using namespace std; const int Maxn=105;const int inf=210000;vector<int> g[maxn];bool vis[maxn];int lev=1;int sum;int ans=1;//ans= 0 Words of the second example, can find this error, I was also drunk void bfs (int s) {queue<int> q; Q.push (s); Vis[s]=true; int tail; int last=s; int cnt=1; while (!q.empty ()) {int K=q.front (); Q.pop (); int n=g[k].size (); for (int i=0;i<n;i++) {if (!vis[g[k][i]]) {Q.push (g[k][i]); Vis[g[k][i]]=true; Tail=g[k][i]; sum++; }} if (K==last) {last=tail; cnt++; if (ans<sum) {ans=sum; lev=cnt; } sum=0; }}}int Main () {INT N,m,i,j,k,t; Freopen ("In.txt", "R", stdin); scanf ("%d%d", &n,&m); for (i=0;i<m;i++) {int id; cin>>id>>k; for (j=0;j<k;j++) {cin>>t; G[id].push_back (t); G[t].push_back (ID); }} BFS (1); printf ("%d%d\n", Ans,lev); return 0;}
1094. The largest Generation (25) This topic and six degrees of space a truth, record the level of BFS