1 条题解

  • 0
    @ 2025-10-8 17:00:44

    by hansang:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=5e4+10;
    vector<int> G[N];
    int f[N][2];
    void dfs(int x, int fa){
    	f[x][1]=1;
    	for(int y: G[x]) if(y!=fa){
    		dfs(y, x); 
    		f[x][0]+=max(f[y][1], f[y][0]);
    		f[x][1]+=f[y][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", max(f[1][0], f[1][1]));
    	return 0;
    }
    
    • 1

    *【树形DP:相邻点互斥】无根树最多不相邻点数 [USACO10NOV] Visiting Cows G

    信息

    ID
    2314
    时间
    1000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    187
    已通过
    31
    上传者