P1041 传染病控制
#include <bits/stdc++.h>
using namespace std;
const int maxn = 301;
std::vector<int> son[maxn];
vector<int> G[maxn];
int deep[maxn], size[maxn];
vector<int> dep[maxn];
int vis[maxn];
int n, p, ans = 0x3f3f3f;
inline void dfs(int f) {
vector<int>::iterator it;
size[f] = 1;
for (it = son[f].begin(); it != son[f].end(); it++) {
int x = *it;
deep[x] = deep[f] + 1;
dfs(x);
size[f] += size[*it];
}
}
inline void hit(int root, int state) {
vector<int>::iterator it;
if (state == 1)
vis[root] = 1;
else
vis[root] = 0;
for (it = son[root].begin(); it != son[root].end(); it++) {
hit(*it, state);
}
}
inline void search(int d, int now) {
ans = min(ans, now);
vector<int>::iterator it;
for (it = dep[d].begin(); it != dep[d].end(); it++) {
if (!vis[*it]) {
hit(*it, 1);
search(d + 1, now - size[*it]);
hit(*it, 0);
}
}
}
int v[maxn];
inline void trans(int root) {
v[root] = 1;
vector<int>::iterator it;
for (it = G[root].begin(); it != G[root].end(); it++) {
if (!v[*it]) {
son[root].push_back(*it);
trans(*it);
}
}
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
// freopen("input.in", "r", stdin);
cin >> n >> p;
for (int i = 1; i <= p; i++) {
int x, y;
x = read();
y = read();
G[x].push_back(y);
G[y].push_back(x);
}
memset(v, 0, sizeof(v));
trans(1);
deep[1] = 0;
dfs(1);
for (int i = 1; i <= n; i++) {
dep[deep[i]].push_back(i);
}
search(1, n);
printf("%d\n", ans);
return 0;
}
P1041 传染病控制的更多相关文章
- P1041 传染病控制(dfs)
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- 【搜索】P1041 传染病控制
题目链接:P1041 传染病控制 题解: 这个题目是看别人的博客做出来的,其实挺不错的一个题目,考察的东西挺多的, 一个dfs可以处理5个东西: 1.找出父亲 2.找出深度 3.每一层的节点,存进Ve ...
- 洛谷 P1041 传染病控制
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- P1041 传染病控制——暴力遍历所有相同深度的节点
P1041 传染病控制 说实话这种暴力我还是头一次见,每次病毒都会往下传染一层: 数据范围小,我们可以直接枚举当前层保护谁就好了: 用vector 记录相同层数的节点:维护已经断了的点: 如果超出最底 ...
- [luogu]P1041 传染病控制[dfs]
[luogu]P1041 传染病控制 ——!x^n+y^n=z^n 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的 ...
- Luogu P1041 传染病控制(搜索)
P1041 传染病控制 题意 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这 ...
- P1041 传染病控制【暴搜】
P1041 传染病控制 提交 10.78k 通过 3.74k 时间限制 1.00s 内存限制 125.00MB 题目提供者CCF_NOI 难度提高+/省选- 历史分数100 提交记录 查看题解 标签 ...
- 洛谷P1041 传染病控制
解:搜索...... 我们可以每次选择分支少的搜索,或者说,贪心的搜索当前更优的决策. 每一层把能剪的点搞出来,按照度数/SIZ排序,然后依次搜索.加个最优化剪枝就完事了. #include < ...
- luogu P1041 传染病控制
题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...
随机推荐
- Spring Security控制权限
Spring Security控制权限 1,配置过滤器 为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了. < ...
- jQuery treetable【表格多重折叠树功能及拖放表格子元素重新排列】
今天有个表格需求做到多重折叠子元素功能,仔细想了下实现原理, 1.在html中,把父子节点的关系写在自定义属性,但对于节点是否有孩子(hasChild),是否是最后一个节点(isLastOne),是否 ...
- MyBatis学习(二)
mybatis开发dao的方法 作用范围 SqlSessionFactoryBuilder是以工具类方式来使用,需要创建sqlSessionFactory就new一个SqlSessionFactory ...
- Mybatis 复习 Mybatis 配置 Mybatis项目结构
pom.xml文件已经贴在了文末.该项目不使用mybatis的mybatis-generator-core,而是手写Entities类,DaoImpl类,CoreMapper类 其中,Entities ...
- Mac系统 安装SVN
- Andriod学习笔记1:代码优化总结1
多行变一行 比如说开发一个简单的计算器应用程序,需要定义0-9的数字按钮,第一次就习惯性地写出了如下代码: Button btn0; Button btn1; Button btn2; Button ...
- POJ 3294 Life Forms 后缀数组+二分 求至少k个字符串中包含的最长子串
Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, ...
- wenbenfenlei
ICTCLAS: 该分词系统的主要思想是先通过CHMM(层叠形马尔可夫模型)进行分词,通过分层,既增加了分词的准确性,又保证了分词的效率.基本思路是:先进行原子切分,然后在此基础上进行N-最短路径粗切 ...
- 简单CSS3动画制作
本贴已重新编辑至http://www.cnblogs.com/fastmover/p/4977358.html 最近需要用到了一些CSS3动画,基本用Animate.css(https://githu ...
- js中的call和apply
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:赵望野链接:https://www.zhihu.com/question/20289071/answer/14745394来源 ...