qduoj 生化危机

广搜
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
#include <vector>
#define MAXN 10010

using namespace std;
struct city {
	int id;
	int day;
};

int n, k;
vector <int> map[MAXN];
queue <city> q;
int vis[MAXN];
int ans[MAXN];
int ANS;

void bfs(int k) {
	while(!q.empty()) q.pop();
	memset(vis, 0, sizeof(vis));
	q.push(city {k, 1} );
	vis[k] = 1;
	while(!q.empty()) {
		city c;
		c = q.front();
		q.pop();
		int nowid = c.id;
		int nowday = c.day;
		ANS = nowday;
		ans[nowday]++;
		int i;
		for(i = 0; i < (int)map[nowid].size(); i++) {
			int next = map[nowid][i];
			if(!vis[next]) {
				c.id = next, c.day = nowday + 1;
				vis[next] = 1;
				q.push(c);
			}
		}
	}
}
int main() {
	int T;
	cin >> T;
	while(T--) {
		cin >> n >> k;
		int i;
		int x, y;
		for(i = 1; i <= n; i++) map[i].clear();
		for(i = 1; i <= n; i++) ans[i] = 0;
		ANS = 0;
		for(i = 1; i < n; i++) {
			cin >> x >> y;
			map[x].push_back(y);
			map[y].push_back(x);			
		}
		bfs(k);
		cout << ANS << endl;
		for(i = 1; i <= ANS; i++) cout << ans[i] << ' ';
		cout << endl;
	}	
	return 0;
}

你可能感兴趣的:(qduoj 生化危机)