1094 The Largest Generation (25point(s)) Easy only once

基本思想:

层序遍历问题;

关键点:

无;

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
using namespace std;

int n, m;
int cnt=0;
int f_layer=0;
struct node {
	vector<int>child;
	int layer;
};

vector<node>tree;

void layer_init() {
	if (tree.size() == 0)
		return;
	tree[1].layer = 1;
	queue<int>q;
	q.push(1);
	while (!q.empty()){
		int index = q.front();
		q.pop();
		for (int i = 0; i < tree[index].child.size(); i++) {
			tree[tree[index].child[i]].layer = tree[index].layer + 1;
			q.push(tree[index].child[i]);
		}
	}
}

void layer_find() {
	if (tree.size() == 0)
		return;
	queue<int>q;
	q.push(1);
	int l = 0;
	int c = 0;
	while (!q.empty()) {
		l++;
		c = q.size();
		for (int i = 0; i < c; i++) {
			int index = q.front();
			q.pop();
			for (int j = 0; j < tree[index].child.size(); j++) {
				q.push(tree[index].child[j]);
			}
		}
		if (cnt < c) {
			cnt = c;
			f_layer = l;
		}
	}
}

int main() {
	cin >> n >> m;
	int a, b,c;
	tree.resize(n+1);
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		for (int j = 0; j < b; j++) {
			cin >> c;
			tree[a].child.push_back(c);
		}
	}
	layer_find();
	cout << cnt << " " << f_layer;
	return 0;
}

  

原文地址:https://www.cnblogs.com/songlinxuan/p/12307251.html

时间: 2024-08-30 10:07:36

1094 The Largest Generation (25point(s)) Easy only once的相关文章

1094. The Largest Generation (25) 此题和六度空间一个道理,记录BFS的层次

1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find

1094. The Largest Generation (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1094. The Largest Generation (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the

PAT 1094 The Largest Generation[bfs][一般]

1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Ea

1094. The Largest Generation (25)

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Each input file contains one test ca

PAT (Advanced Level) 1094. The Largest Generation (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; const int maxn=200; int n,m; vector<int>

PAT 1094. The Largest Generation(BFS)

CODE: #include<cstdio> #include<cstring> #include<queue> using namespace std; bool mat[105][105]; bool root[105]; int n,m; int R; int cnt[105]; int ans1,ans2; struct TNode { int num; int level; }; void BFS() { queue<TNode> Q; TNode

1094 The Largest Generation

题意:略. 思路:层序遍历:在结点中增加一个数据域表示结点所在的层次. 代码: #include <cstdio> #include <queue> #include <vector> using namespace std; struct Node{ int layer; vector<int> child; }; vector<Node> tree(110); int level[110]={0}; int maxLevel=1,maxNum

pat1094. The Largest Generation (25)

1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find

PAT-ADVANCED-1094-The Largest Generation

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Each input file contains one test ca