zoukankan      html  css  js  c++  java
  • luogu P2016 战略游戏 树形dp

    #include<cstdio>
    #include<cstring> 
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=3010;
    int e[N],h[N],ne[N],idx;
    int f[N][2];
    bool st[N];
    void add(int a,int b)
    {
    	e[idx]=b;
    	ne[idx]=h[a];
    	h[a]=idx++;
    }
    int n,m;
    int a,b;
    void dfs(int u)
    {
    	f[u][1]=1;
    	f[u][0]=0;
    	for(int i=h[u];~i;i=ne[i])
    	{
    		int j=e[i];
    		dfs(j);
    		f[u][0]+=f[j][1];
    		f[u][1]+=min(f[j][0],f[j][1]);
    	}
    }
    int main()
    {
    	idx=0;
    	memset(h,-1,sizeof h);
    	memset(st,0,sizeof st);
    	cin>>n;
    	for(int i=1;i<=n;i++)
    	{
    		int id,cnt;
    		cin>>id>>cnt;
    		while(cnt--)
    		{
    			int ver;
    			cin>>ver;
    			add(id,ver);
    			st[ver]=1;
    		}
    	}
    	int root=0;
    	while(st[root])
    		root++;
    	dfs(root);
    	cout<<min(f[root][1],f[root][0])<<endl;
    	return 0;
    }
    
  • 相关阅读:
    python 文件目录/方法
    python文件
    python模块
    python数据结构
    python函数
    python迭代器和生成器
    python循环语句
    python控制语句 if
    python数字
    个人课程总结
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12568454.html
Copyright © 2011-2022 走看看