1004. Counting Leaves (30)

#include "stdafx.h"
#include <iostream>
#include <queue>

using namespace std;

vector<int> v[110];
queue<int> q;
int vis[110], first = 1;

void bfs()
{
	int size = q.size(), cur, c, count = 0, i, next;
	while(size--)
	{
		cur = q.front();
		q.pop();

		c = v[cur].size();
		if(c == 0)
		{
			count++;
		}
		else
		{
			for(i = 0; i < c; i++)
			{
				next = v[cur][i];
				if(vis[next] == 0)
				{
					vis[next] = 1;
					q.push(next);
				}
			}
		}
	}

	if(first == 1)
	{
		first = 0;
	}
	else
	{
		printf(" ");
	}

	printf("%d", count);

	size = q.size();
	if(size > 0)
	{
		bfs();
	}
}

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);

	int i, j, id, k, child;
	for(i = 1; i <= m; i++)
	{
		scanf("%d%d", &id, &k);

		for(j = 1; j <= k; j++)
		{
			scanf("%d", &child);
			v[id].push_back(child);
		}
	}
	
	vis[1] = 1;
	q.push(1);
	bfs();

	printf("\n");

	system("pause");
	return 0;
}

 

posted on 2025-11-23 16:50  王景迁  阅读(3)  评论(0)    收藏  举报

导航