ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1004 Strategic game 树的最小点覆盖

2022-08-01 19:04:19  阅读:167  来源: 互联网

标签:node identifier idx int number Strategic game st 1004


 链接:https://ac.nowcoder.com/acm/contest/25022/1004
来源:牛客网

题目描述

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? 

Your program should find the minimum number of soldiers that Bob has to put for a given tree. 

For example for the tree: 

the solution is one soldier ( at the node 1).

输入描述:

The input contains several data sets in text format. Each data set represents a tree with the following description: 
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1node\_identifier_1node_identifier1​ node_identifier2node\_identifier_2node_identifier2​ ... node_identifiernumberofroadsnode\_identifier_{number_of_roads }node_identifiernumbero​fr​oads​
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0<n≤1500)(0 \lt n \leq 1500)(0<n≤1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

输出描述:

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:
示例1

输入

复制
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

输出

复制
1
2

分析

树的最小点覆盖:每两个相连的结点之间至少有一个节点被覆盖。

f[i][1]表示选择这个点,f[i][0] 表示不选择这个点 

状态转移:f[i][1] = 1 + E min(f[k][0],f[k][1]);k表示子节点

f[i][0] = E f[k][1];

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 1510;
int h[N],e[N],w[N],ne[N],idx;
int n;
int f[N][N];
bool st[N];
void add(int a,int b)
{
    e[idx] = b,ne[idx] = h[a],h[a] = idx++;
}

void dfs(int u)
{
    f[u][0] = 0;
    f[u][1] = 1;
    for(int i = h[u]; i != -1; 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()
{
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    while(cin >> n)
    {
        memset(h, -1, sizeof h);
        memset(st, 0, sizeof st);
        idx = 0;
        for(int i = 0; i < n; i ++ )
        {
            int id,cnt;
            scanf("%d:(%d)",&id,&cnt);
            while(cnt -- )
            {
                int ver;
                cin >> ver;
                add(id, ver);
                st[ver] = true;
            }
        }
        int r = 0;
        while(st[r]) r++;
        dfs(r);
        cout << min(f[r][0],f[r][1]) << "\n";
    }
    return 0;   
}

 

标签:node,identifier,idx,int,number,Strategic,game,st,1004
来源: https://www.cnblogs.com/er007/p/16541455.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有