题目:http://10.105.242.80/problem/p/100/
树形结构问题,运用数组来存储树,bfs。
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
int num;
vector<int> son;
};
int main(){
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++){
TreeNode tree[110];
printf("Q%d:\n",i);
int N,M;
scanf("%d%d",&N,&M);
while(N--){
int s,f;
scanf("%d%d",&s,&f);
tree[f].son.push_back(s);
}
queue<int> q;
q.push(1);
while(!q.empty()){
vector<int> temp;
while(!q.empty()){
temp.push_back(q.front());
q.pop();
}
for(int i=0;i<temp.size();i++){
printf("%d",temp[i]);
if(i!=temp.size()-1){
putchar(' ');
}
vector<int> son=tree[temp[i]].son;
for(int j=0;j<son.size();j++){
q.push(son[j]);
}
}
printf("\n");
}
}
return 0;
}