#POJ1463#Strategic Game(贪心 or Hungary or 树形DP)

Vercel部署个人博客 先去注册一个账号,建议注册一个Github账号后,使用Github账号来登录Vercel。vercel 部署静态资源网站极其方便简单,并且有可观的访问速度,最主要的是免费部署。此时项目已经成功搭建完毕了,点击图片即可跳转到 vercel 所提供的二级域名访问。然后可以直接拉取我的仓库,所有已经配置好了,可以直接使用,建议fork成自己的!然后修改dns就行,注意vercel提供的域名可能访问不了,需要自己绑定!如果有自己的域名,还可以在 vercel 中进行设置。进入Vercel官网。 阅读详情

Strategic game
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 8409 Accepted: 3931

Description

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).

Input

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_identifier1 node_identifier2 ... node_identifiernumber_of_roads 
    or 
    node_identifier:(0) 
The node identifiers are integer numbers between 0 and n-1, for n nodes (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.

Output

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:

Sample Input

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)

Sample Output

1
2

题意:
鲍勃喜欢玩战略游戏。现在有n个城市,他们构成了一棵树。
鲍勃可以在某些城市派一个士兵守护,该士兵可以瞭望到所有与该城市相连的边。
问鲍勃最少要派遣多少个士兵,才能把所有的边都瞭望到。 N <= 1500

这题有三种做法:贪心 匈牙利匹配 和 树形DP,现在只讲最快的贪心法

将图做成无向图。
从度为1的节点开始想,这种节点是必须看到的,但是如果放在它们的位置上,显然不如放在与之相连的位置上好
(因为这样能看见更多的士兵,而且摆放的数量也会更少,请想一想)
我们将与它们相连的点摆上士兵,然后删除度为1节点的边,并且也删除已摆放士兵的节点的边,
剩下的图中,会再次出现度为1的点,重复以上操作。

Code:
Status Accepted
Time 234ms
Memory 1804kB
Length 1393
Lang G++
Submitted
Shared
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

const int Max = 1500;

int num[Max + 5];
vector<int>Tr[Max + 5];
queue<int>Q;
bool vis[Max + 5];

bool  getint(int & num){
    char c;    int flg = 1;    num = 0;
    while((c = getchar()) < '0' || c > '9')    if(c == '-')    flg = -1;
    while(c >= '0' && c <= '9')    {    num = num * 10 + c - 48;    c = getchar();}
    num *= flg;
	return 1;
}

int main(){
	int N;
	while(~scanf("%d", &N)){
		int r, x, n;
		for(int i = 1; i <= N; ++ i){
			getint(r),getint(n);
			for(int j =1; j <= n; ++ j)
				getint(x),
				Tr[r].push_back(x),
				Tr[x].push_back(r),
				++ num[x], ++ num[r];
		}
		if(N == 1){
			printf("1\n");
			continue;
		}
		for(int i = 0; i < N; ++ i)
			if(num[i] == 1)
				Q.push(i);
		int Ans = 0;
		while(! Q.empty()){
			int tmp = Q.front();	Q.pop();
			if(vis[tmp])	continue;
			for(int i = 0; i < (int)Tr[tmp].size(); ++ i)	if(! vis[Tr[tmp][i]]){
				++ Ans;
				int u = Tr[tmp][i];
				vis[u] = 1;
				for(int j = 0; j < (int)Tr[u].size(); ++ j){
					-- num[Tr[u][j]];
					if(! vis[Tr[u][j]] && num[Tr[u][j]] == 1)
						Q.push(Tr[u][j]);
				}
			}
		}
		printf("%d\n", Ans);
		memset(num, 0, sizeof(num));
		memset(vis, 0, sizeof(vis));
		for(int i = 0; i < N; ++ i)
			Tr[i].clear();
	}
	return 0;
}


STM32项目实战_基于STM32F103的智能药箱设计与实现(硬件详解+软件逻辑+调试要点) 本文详细介绍了基于STM32F103的智能药箱设计与实现,涵盖硬件设计、软件逻辑和调试要点。项目核心功能包括精准提醒、药品管理和远程监控,采用STM32单片机作为主控,结合光电开关、蓝牙模块等实现智能化管理。文章还分享了电路图设计、程序优化及常见问题解决方案,为嵌入式开发爱好者提供实用参考。 阅读详情

相关推荐

ABAP代码使用OAuth2.0 Client Credential调用外部API

ABAP-OAuth2.0-调用外部API

m0_74015269的博客 1963

Poj1463 及 Poj3659 树形贪心建立解

Poj1463题目链接:http://poj.org/problem?id=1463 题意:给出一个树形图,在某个节点上放置一个卫兵可以管辖周围所有与该节点相连的边,问最少需要放置多少个卫兵,能管辖所有的边。 贪心思路是,叶节点不放置卫兵,则其父亲节点必放置卫兵以覆盖该边,自叶节

dooder_daodao的专栏 1837

智能座舱测试工程师必看:从零搭建自动化测试框架的5个实战技巧

本文为智能座舱测试工程师提供从零搭建自动化测试框架的5个实战技巧,涵盖环境搭建、ADB高级应用、Python脚本工程化、Monkey测试优化及性能调优。通过ADB深度操作和Python脚本封装,帮助测试人员构建高可复用的智能座舱测试解决方案,显著提升测试效率与问题发现率。

python9snake的博客 648

【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏

DescriptionBob 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

XiaoZheng的博客 1105

海康视觉相机驱动软件MVS参数配置全攻略(附接线图+避坑指南)

本文提供了海康视觉相机驱动软件MVS从硬件连接到软件参数配置的完整指南。详细解析了供电与通讯线缆的区分、PNP/NPN信号逻辑的接线避坑方法,并重点阐述了MVS软件中触发模式、网络IP配置、图像采集及光源控制等核心参数的设置技巧,旨在帮助用户快速完成相机调试,实现稳定高效的图像采集。

weixin_29204205的博客 421

实测OV5640 DVP/MIPI接口720p@60fps配置,为什么我的Zynq-7020开发板只有30帧?

本文深入解析了OV5640摄像头在Zynq-7020开发板上帧率减半的问题,从寄存器配置到硬件链路进行全面排查。重点分析了DVP/MIPI接口的720p@60fps配置,提供了关键寄存器设置、时序参数调整及硬件带宽验证方法,帮助开发者快速定位并解决帧率下降问题。

weixin_42634811的博客 348

poj 1463(树形dp)

题目: 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 me...

偷吃了老鼠的土豆 255

NuPlayer 之 Renderer

触发postDrainAudioQueue_l()或postDrainVideoQueue()开始渲染。解码器输出数据 → NuPlayer::Decoder::onMessageReceived。音频数据存入mAudioQueue,视频数据存入mVideoQueue。• 每次写入后通过mNotifyConsumed通知解码器。• 通过AudioSink::write()写入音频设备。• 根据VSync信号和音频时钟调整渲染时机。• 通过Surface直接提交给显示系统。

aningxiaoxixi的博客 326

HDU 1054Strategic Game 贪心

这个题与http://blog.csdn.net/anqier0468/article/details/9453567看起来没什么联系,但是贪心策略是一样的,(做这个题时还是没想起来),从度数为1的结点a开始算起,因为删除这个度数为1的结点肯定不如删除与它相连接的结点b效率高,所以一定是在与它相连的结点b上房哨兵,然后删除与b相连的边,直到没有度数为1的结点。 特别注意只有一个结点时输出1。

anqier0468的专栏 708

1.2、基于卷积神经网络(CNN)的车牌自动识别系统(matlab)

基于卷积神经网络(CNN)的车牌自动识别系统(matlab)

XU157303764的博客 2915

使用Python的OpenCV库进行三维重建点云

通过这些步骤,我们可以使用Python的OpenCV库进行三维重建,并生成对应的点云。当然,这只是一个简单的示例,实际的三维重建可能涉及更多的图像处理和计算步骤。在计算机视觉领域,三维重建是一项重要的任务,它可以将多个二维图像或视频序列转换为三维场景模型。在本文中,我们将介绍如何使用Python的OpenCV库进行三维重建,并生成相应的点云。以上是使用Python的OpenCV库进行三维重建点云的简要介绍和示例代码。最后,我们可以将计算得到的三维点云可视化。安装完成后,我们可以开始编写代码。

AuSwift的博客 1649

POJ Strategic game(树形DP)

题目链接:http://poj.org/problem?id=1463 其实这个题目也是比较水的一个树状DP,和上面一篇博客思路基本上差不多,甚至动态方程都差不多 开始建立树的时候,直接建立树,建树的时候直接让大的成为小的子树,由于题目说了这是一棵树,所以 就不用担心直接建树就好了,最后从0(根)号节点DFS即可! 每个子树的根节点可有两个状态,要么安排士兵要么不安排,如果安排,那么子节点

sunrain的专栏 794

用miniconda建立PyTorch、Keras、TensorFlow三个环境

如果还没有安装Miniconda,请从[Miniconda官网](https://docs.conda.io/en/latest/miniconda.html)下载合适的版本并进行安装。如果遇到问题,可以通过编辑`~/.condarc`文件来临时禁用某个源,或者通过`conda config --remove channels`命令来移除配置的镜像源。要切换到不同的环境,先使用`conda deactivate`命令退出当前环境,再用`conda activate 环境名称`命令激活另一个环境。

数字人生 2468

poj 1463 树形dp或者贪心(树的最小点覆盖)

题意:给定一棵树,选点最少的点

dearmango 1034

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

#POJ1521#Huffman编码

[POJ1521]Huffman编码 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入一个字符串,长度不超过100,仅由大写字母和下划线分组成。求用最好的字符编码方式,令总长度最小。 输入 多组数据,每组数据在一行上输入一个字符串,格式如前所述 当遇到END时,表示输入结束。 输出 对应每个输入,在一行上输出3个信息:首先是

ElaineZXY的博客 931

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

#POJ3696# The Luckiest number

The Luckiest number   Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is

ElaineZXY的博客 677

#Poj1845#幂的约数之和

[POJ1845]幂的约数之和 时间限制: 1 Sec  内存限制: 128 MB [提交][状态][我的提交] 题目描述 给定正整数A, B,求A^B的所有因数之和,并模9901。 输入 仅一行,有两个整数A和B(0 输出 第1行:问题的答案 样例输入 Copy (如果复制到控制台无换行

ElaineZXY的博客 657

#POJ1173#Bar Code

【POJ1173】条形码 时间限制: 1 Sec  内存限制: 128 MB [提交][状态][我的提交] 题目描述 条形码如下图所示,由黑白两色构成,每种颜色分布一定的宽度。宽度是一个有上限的整数值。例如下图的条形码有4个条形,宽度分别是1,2,3,4,总宽度为:1+2+3+1 =7 定义BC(n,k,m) 表示有k个条形,每个条形的宽度不超过m,总宽度为n的条

ElaineZXY的博客 632

#POJ3040#Allowance(贪心经典)

Allowance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3887   Accepted: 1563 Description As a reward for record milk production, Farmer John has decided

ElaineZXY的博客 623

#POJ1014#Dividing(多重背包二进制优化)

Dividing Time Limit: 1000MS   Memory Limit: 10000K Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both recei

ElaineZXY的博客 557

#POJ3714#Raid(不同类最小点对 + 分治)

Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12529   Accepted: 3701 Description After successive failures in the battles against the Union, the Emp

ElaineZXY的博客 555

#poj1180#Batch Scheduling (经典逆推Dp+斜率优化)

Batch Scheduling Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4068   Accepted: 1860 Description There is a sequence of N jobs to be processed on one

ElaineZXY的博客 478

#POJ2516#Minimum Cost(拆点+Km完备匹配)

Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16562   Accepted: 5817 Description Dearboy, a goods victualer, now comes to a big problem, and

ElaineZXY的博客 455

#Poj1769#Minimizing maximizer(Dp+线段树优化)

Minimizing maximizer Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 4367   Accepted: 1799 Description The company Chris Ltd. is preparing a new sorting ha

ElaineZXY的博客 420

#POJ2750#Potted Flower(线段树)

Potted Flower Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4883   Accepted: 1861 Description The little cat takes over the management of a new park. The

ElaineZXY的博客 405

#POJ1987#Distance Statistics(树+点分治)

Distance Statistics Time Limit: 2000MS   Memory Limit: 64000K Total Submissions: 2472   Accepted: 883 Case Time Limit: 1000MS Description Frustrated at the number

ElaineZXY的博客 387

#POJ1475#Pushing Boxes(A*_搜索)

Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 6117   Accepted: 2115   Special Judge Description Imagine you are standing inside a two-di

ElaineZXY的博客 383
上一篇: #POJ1328#Radar Installation(贪心)
下一篇: #POJ2376#Cleaning Shifts(贪心 -> 最小区间覆盖)
itselaineZ
博客等级 码龄11年 25粉丝 162原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值