zoukankan      html  css  js  c++  java
  • 战略游戏

    https://loj.ac/problem/10156

    题目描述

      有一棵树形城堡,在一个节点放置士兵时与这个点相连的边都能被看到,求放置最少的节点使得所有边都被看到。

    思路

      我们用(f[i][0])表示在这个点不放的最少代价,(f[i][1])表示在这个点放的最小代价,那么显然如果这个点不放,那么以它的儿子必须都放;如果这个点放,就无所谓儿子节点放与不放,直接树上转移即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1600;
    
    int read()
    {
    	int res=0,w=1;
    	char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}
    	return res*w;
    }
    void write(int x)
    {
    	if(x<0){putchar('-');x=-x;}
    	if(x>9)write(x/10);
    	putchar(x%10+'0');
    }
    void writeln(int x)
    {
    	write(x);
    	putchar('
    ');
    }
    
    int nxt[N<<1],head[N],tot,to[N<<1];
    void add_edge(int x,int y)
    {
    	nxt[++tot]=head[x];
    	head[x]=tot;
    	to[tot]=y;
    }
    int f[N][N];
    void dfs(int u,int fa)
    {
    	f[u][0]=0;f[u][1]=1;
    	for(int i=head[u];i;i=nxt[i])
    	{
    		int v=to[i];
    		if(v==fa)continue ;
    		dfs(v,u);
    		f[u][1]+=min(f[v][1],f[v][0]);
    		f[u][0]+=f[v][1];
    	}
    }
    
    bool a[N];
    int main() 
    {
    	int n=read(),root;
    	for(int i=1;i<=n;i++)
    	{
    		int x=read()+1,k=read();
    		while(k--)
    		{
    			int y=read()+1;
    			add_edge(x,y);add_edge(y,x);
    			a[y]=1;
    		}
    	}
    	for(int i=1;i<=n;i++)
    		if(!a[i])root=i;
    	dfs(root,0);
    //	printf("
    ");
    //	for(int i=1;i<=n;i++)
    //		printf("%d %d
    ",f[i][0],f[i][1]);
    	writeln(min(f[root][0],f[root][1]));
    }
    
  • 相关阅读:
    HIVE 2.1.0 安装教程。(数据源mysql)
    Linux基础命令—sleep
    Linux基础命令—echo
    C语言的基本数据类型
    Linux基础命令—rmdir
    Linux基础命令—mkdir
    Linux基础命令—cd
    Linux基础命令—pwd
    Linux周期性执行任务(crontab)
    Linux执行单一时刻定时任务管理操作(at)
  • 原文地址:https://www.cnblogs.com/fangbozhen/p/11838341.html
Copyright © 2011-2022 走看看