您的位置:首页 > 其它

【bzoj1907】树的路径覆盖

2017-07-26 19:00 423 查看
Description



Input



Output



Sample Input

1

7

1 2

2 3

2 4

4 6

5 6

6 7

Sample Output

3

HINT



,注意此注释有误,第二种情况应该为{1,2,4,5,6},{3},{7}

题解

f[i][0/1]表示i节点,是否能与父亲相连的最小路径数

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define mod 10007
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int f[10005][2],Head[10005],ret[20005],Next[20005];
int n,tot;
inline void ins(int u,int v)
{
ret[++tot]=v;Next[tot]=H
4000
ead[u];Head[u]=tot;
}
void dfs(int u,int fa)
{
int sum=0;
f[u][0]=f[u][1]=1;
for (int i=Head[u];i;i=Next[i])
{
int v=ret[i];
if (v==fa) continue;
dfs(v,u);
f[u][0]=min(f[u][0]+f[v][0],f[u][1]+f[v][1]-1);
f[u][1]=min(f[u][1]+f[v][0],f[v][1]+sum);
sum+=f[v][0];
}
}
int main()
{
int Case=read();
while (Case--)
{
n=read();tot=0;
for (int i=1;i<=n;i++) Head[i]=0;
for (int i=1;i<n;i++)
{
int u=read(),v=read();
ins(u,v);ins(v,u);
}
dfs(1,0);
printf("%d\n",f[1][0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: