PAT 1002. A+B for Polynomials

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <functional>
#include <string>
#include <queue>

using namespace std;

vector<int> nodes[105];


void bfs(int root)
{
	queue<int> Q;
	Q.push(root);
	Q.push(-1);

	int num = 0;
	while(Q.size()>1)
	{
		int cur = Q.front();
		Q.pop();
		if(cur == -1)
		{
			printf("%d ",num);
			num=0;
			Q.push(-1);
		}
		if(nodes[cur].size()==0)
			num++;
		for(int i=0;i<nodes[cur].size();i++)
			Q.push(nodes[cur][i]);
	}
}
int main()
{
	int N,M;
	scanf("%d %d",&N,&M);
	for(int i=0;i<M;i++)
	{
		int id,K;
		scanf("%d %d",&id,&K);
		for(int k=0;k<K;k++)
		{
			int tmp;
			scanf("%d",&tmp);
			nodes[id].push_back(tmp);
		}
	}
	bfs(1);
	return 0;
}



你可能感兴趣的:(PAT 1002. A+B for Polynomials)