1 条题解
-
0
by hansang:
#include<bits/stdc++.h> using namespace std; const int N=1e4+10, inf=0x3f3f3f3f; vector<int> G[N]; int f[N][4]; void dfs(int x, int fa){ f[x][1]=1; int t=inf; bool flag=0; for(int y: G[x]) if(y!=fa){ dfs(y, x); flag=1; t=min(t, f[y][1]-f[y][0]); f[x][0]+=min(f[y][1], f[y][0]); f[x][1]+=min({f[y][0], f[y][1], f[y][2]}); f[x][2]+=min(f[y][0], f[y][1]); } if(t>0) f[x][0]+=t; //当是叶子节点的时候,f[x][0]要为正无穷 } int main(){ int n; scanf("%d", &n); for(int i=1; i<n; i++){ int x, y; scanf("%d%d", &x, &y); G[x].push_back(y); G[y].push_back(x); } memset(f, 0, sizeof(f)); dfs(1, 0); printf("%d\n", min(f[1][0], f[1][1])); return 0; }
- 1
信息
- ID
- 2315
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 8
- 标签
- 递交数
- 18
- 已通过
- 7
- 上传者