地址 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 ;
}

最新文章

  1. spring ioc
  2. 用友U8 归纳采购退货结算三种情况
  3. Eclipse查看hadoop源代码出现Source not found,是因为没有添加.zip
  4. 学生各门课程成绩统计SQL语句大全
  5. linux yum配置
  6. (转载)MySQL 统计数据行数 Select Count
  7. ipad pro注定是失败的!
  8. asp.net访问WebService的各种方式
  9. linux的学习系列 1---简介
  10. 请求库-request使用
  11. Java项目的导入和导出
  12. 让Windows Server 2008r2 IIS7.5 ASP.NET 支持10万并发请求
  13. SSM文件下载
  14. Linux常用命令详解-目录文件操作命令
  15. LeetCode——8. String to Integer (atoi)
  16. 解决Oracle+Mybatis批量插入报错:SQL 命令未正确结束
  17. 微信小程序 折叠效果
  18. 深入分析ReentrantLock公平锁和非公平锁的区别
  19. laravel mapSpread 例子
  20. maxout激活函数

热门文章

  1. C# copy source directory files with original folder to the destination path
  2. Java中&quot;或&quot;运算与&quot;与&quot;运算快慢的三三两两
  3. xshell破解
  4. [转]UiPath Installing the Firefox Extension
  5. English: Class logogram
  6. Hello universe!
  7. 从0系统学Android--3.7 聊天界面编写
  8. How do I unmute my Lenovo laptop?
  9. 大数据基础--R语言(刘鹏《大数据》课后习题答案)
  10. ReactNative: 使用像素密度类PixelRatio进行适配