算法问题实战策略 GALLERY
2024-08-23 04:11:34
地址 https://algospot.com/judge/problem/read/GALLERY
分析
如图 显然是需要在 0 1 2三个点进行监控即可。(0 2 3 也可)
根据题意,不存在回路,也就是不重复经过两画廊之间的走廊是不可能在两画廊之间进行走动的
我们可以将该图看成一棵树,深度优先遍历时,叶子结点的父节点需要放置摄像头,这样能将叶子结点 父节点和父节点的父节点均可监视到。然后根据有无未监视的子节点 决定当前节点的状态(需要放置,被监视,未被监视)
代码如下
#include <iostream>
#include <vector> using namespace std; const int N = ;
int V;
vector<int> adj[];
vector<bool> visited;
const int UNWATCHED = ;
const int WATCHED = ;
const int INSTALLED = ; int installed; int dfs(int here)
{
visited[here] = true;
int children[] = { ,, };
for (int i = ; i < adj[here].size(); i++)
{
int there = adj[here][i];
if (!visited[there])
++children[dfs(there)];
} //后代节点存在没有监视的节点 在该节点安装摄像头
if (children[UNWATCHED]) {
++installed;
return INSTALLED;
} if (children[INSTALLED]) {
return WATCHED;
} return UNWATCHED;
} int installCamera()
{
installed = ;
visited = vector<bool>(V, false);
for (int u = ; u < V; ++u) {
if (!visited[u] && dfs(u) == UNWATCHED)
++installed;
} return installed;
} /*
3
6 5
0 1
1 2
1 3
2 5
0 4
4 2
0 1
2 3
1000 1
0 1
=====================
3
2
999
*/ int main()
{
int sampleCount = ;
cin >> sampleCount;
while (sampleCount--) {
int n, m; visited.clear();
for (int i = ; i < ; i++) adj[i].clear();
cin >> V >> m;
for (int i = ; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cout << installCamera() << endl;
} return ;
}
最新文章
- spring ioc
- 用友U8 归纳采购退货结算三种情况
- Eclipse查看hadoop源代码出现Source not found,是因为没有添加.zip
- 学生各门课程成绩统计SQL语句大全
- linux yum配置
- (转载)MySQL 统计数据行数 Select Count
- ipad pro注定是失败的!
- asp.net访问WebService的各种方式
- linux的学习系列 1---简介
- 请求库-request使用
- Java项目的导入和导出
- 让Windows Server 2008r2 IIS7.5 ASP.NET 支持10万并发请求
- SSM文件下载
- Linux常用命令详解-目录文件操作命令
- LeetCode——8. String to Integer (atoi)
- 解决Oracle+Mybatis批量插入报错:SQL 命令未正确结束
- 微信小程序 折叠效果
- 深入分析ReentrantLock公平锁和非公平锁的区别
- laravel mapSpread 例子
- maxout激活函数
热门文章
- C# copy source directory files with original folder to the destination path
- Java中";或";运算与";与";运算快慢的三三两两
- xshell破解
- [转]UiPath Installing the Firefox Extension
- English: Class logogram
- Hello universe!
- 从0系统学Android--3.7 聊天界面编写
- How do I unmute my Lenovo laptop?
- 大数据基础--R语言(刘鹏《大数据》课后习题答案)
- ReactNative: 使用像素密度类PixelRatio进行适配