码迷,mamicode.com
首页 > 其他好文 > 详细

1004 Counting Leaves (30point(s)) Easy only once

时间:2020-02-14 16:45:56      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:stream   leaves   using   str   return   层序遍历   algo   leave   math   

基本思想:

层序遍历问题;

 

关键点:

无;

 

#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;

struct node {
	vector<int>child;
};

vector<node>tree;
vector<int>seq;

/*
void layer_init() {
	if (tree.size() == 0)
		return;
	tree[0].layer = 0;
	queue<int>q;
	q.push(0);
	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) {
		cout << 0 << endl;
		return;
	}
	queue<int>q;
	q.push(1);
	while (!q.empty()){
		int size = q.size();
		int cnt = 0;
		for (int i = 0; i < size; i++) {
			int index = q.front();
			q.pop();
			if (tree[index].child.size() == 0)
				cnt++;
			else {
				for (int j = 0; j < tree[index].child.size(); j++) {
					q.push(tree[index].child[j]);
				}
			}
		}
		seq.push_back(cnt);
	}
}

int main() {
	cin >> n >> m;
	tree.resize(n+1);
	int a, b,c;
	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();
	for (int i = 0; i < seq.size(); i++) {
		if (i == 0)
			cout << seq[i];
		else
			cout << " " << seq[i];
	}
	return 0;
}

  

1004 Counting Leaves (30point(s)) Easy only once

标签:stream   leaves   using   str   return   层序遍历   algo   leave   math   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!