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

Posted songlinxuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于1094 The Largest Generation (25point(s)) Easy only once的主要内容,如果未能解决你的问题,请参考以下文章

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

PAT 1094. The Largest Generation (层级遍历)

1094 The Largest Generation

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

PAT 1094. The Largest Generation(BFS)

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