1.根节点不一定首先输入
2.用不着使用链表建立好树,用数组表示出来解决题就好
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
int du;
int f;
vector<int> s;
TreeNode(){
f=-1;
du=0;
}
};
int main(){
int T;
scanf("%d",&T);
while(T--){
int N;
scanf("%d",&N);
vector<TreeNode*> vn;
TreeNode node[1001];
for(int i=0;i<N-1;i++){
int f,s;
scanf("%d%d",&f,&s);
node[f].du++;
node[s].du++;
node[f].s.push_back(s);
node[s].f=f;
}
int result=0;
for(int i=0;i<N;i++){
bool ok=true;
int father=node[i].f;
vector<int> vson=node[i].s;
for(int j=0;j<vson.size();j++){
int son=vson[j];
if(node[i].du<node[son].du){
ok=false;
}
}
if(father>=0 && node[i].du<node[father].du){
ok=false;
}
if(ok){
result++;
}
}
printf("%d\n",result);
}
return 0;
}