# HDU 4714 Tree2cycle (构造，树形DP)

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <string>

#define LL long long
#define DB double
#define SI(a) scanf("%d",&a)
#define SD(a) scanf("%lf",&a)
#define SS(a) scanf("%s",a)
#define SF scanf
#define PF printf
#define MM(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define REPD(i,a,b) for(int (i)=(a);(i)>(b);(i)--)
#define N 1000009
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define bug puts("bug")
using namespace std;
vector<int> L[N];
int n,ans;
int v[N];
int solve(int k)
{
if(v[k]) return 0;
v[k] = 1;
int s =0;
REP(i,0,(int)L[k].size())
{
int to = L[k][i];
s += solve(to);
}
if(k==1)
{
if(s==0)
{
ans++;
return 0;
}
if(s==1)
{
ans++;return 0;
}
if(s==2)
{
ans++;///ans+=2;不需要将该节点与父亲节点的边删掉，so，只需要加一次，谢谢qw4990指出错误。
return 0;
}
if(s>2)
{
ans += (s-2)*2+1;
return 0;
}
}else
{
if(s==0) return 1;
if(s==1) return 1;
if(s==2)
{
ans+=2;
return 0;
}
if(s>2)
{
ans += (s-2)*2+2;
return 0;
}
}
return -1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int cas;SI(cas);
while(cas--)
{
SI(n);
REPD(i,n+5,-1) L[i].clear();
int a,b;
MM(v,0);
ans = 0;
REP(i,1,n)
{
SI(a);SI(b);
L[a].push_back(b);
L[b].push_back(a);
}
solve(1);
PF("%d\n",ans);
}
return 0;
}


#### hdu 4714 (树形dp)

2013-10-09 17:34:34

#### hdu4714 Tree2cycle 使一棵树变成环最小代价

2016-04-21 15:21:39

#### HDU 4714 Tree2cycle(树形dp)

2016-08-15 21:28:26

#### hdu 4714 Tree2cycle (树形dp)

2013-09-10 21:24:17

#### hdu 4714 Tree2cycle dp

2013-09-08 21:48:21

#### HDU 6035 树形dp

2017-07-26 13:06:55

#### HDU1520（树形dp入门题）

2016-09-28 21:40:01

#### hdu 4714 (树形dp)

2013-10-09 20:24:22

#### hdu 1054 Strategic Game 树形dp基础&&模板

2012-10-31 21:15:59

#### hdu 4714(树形dp)

2014-05-26 18:06:15