# 【南外】DPS1-C

/*

res=1 中不把 x 连进去的原因是，给他的父亲多留 res，最终答案一定不劣。
*/

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int T,n,u,v,f[200005];
bool fl[200005];
vector<int> a[200005];
void dfs(int x,int fa){
f[x]=1;
int res=0;
for(auto tmp:a[x]){
if(tmp==fa) continue;
dfs(tmp,x);
f[x]+=f[tmp];
if(!fl[tmp]) res++;
}
if(res>=2) fl[x]=1,f[x]-=2;
else if(res==1) f[x]--;
}
signed main(){
IOS;TIE;
cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;i++){
a[i].clear();
fl[i]=f[i]=0;
}
for(int i=1;i<n;i++){
cin>>u>>v;
a[u].pb(v),a[v].pb(u);
}
dfs(1,0);
cout<<f[1]<<endl;
}
return 0;
}


posted @ 2023-09-15 20:50  Binary_Lee  阅读(9)  评论(0编辑  收藏  举报
Title