[cogs] 传染病控制
http://cogs.pro:8080/cogs/problem/problem.php?pid=107
去年6月份的代码了,又长又臭又WA
暴力贪心模拟 水水50
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector> using namespace std;
const int N=; int answer;
int deep[N];
vector<int> DE[N];
int point,road;
int val[N];
int Max_deep;
queue<int>q;
bool vis[N];
int son[N];
int head[N];
int now=;
struct node {
int u,v,nxt;
} E[N]; inline void add(int u,int v) {
E[now].v=v;
E[now].nxt=head[u];
head[u]=now++;
} inline int read() {
int x=;
char c=getchar();
while(c<''||c>'')c=getchar();
while(c>=''&&c<='')x=x*+c-'',c=getchar();
return x;
} void make_deep() {
q.push();
deep[]=;
DE[].push_back();
while(!q.empty()) {
int top=q.front();
q.pop();
for(int i=head[top]; i!=-; i=E[i].nxt) {
q.push(E[i].v);
deep[E[i].v]=deep[top]+;
DE[deep[E[i].v]].push_back(E[i].v);
Max_deep=max(Max_deep,deep[E[i].v]);
}
}
} void make_val() {
for(int Q=Max_deep; Q>=; Q--) {
for(int i=; i<DE[Q].size(); i++) {
int my=DE[Q].size();
int tt=DE[Q][i];
if(son[tt]==)
val[tt]=;
else {
for(int j=head[tt]; j!=-; j=E[j].nxt)
val[tt]+=val[E[j].v];
val[tt]++;
}
}
}
} inline void change(int start) {
q.push(start);
while(!q.empty()) {
int top=q.front();
q.pop();
vis[top]=;
for(int i=head[top]; i!=-; i=E[i].nxt)
q.push(E[i].v);
}
} inline void find_biggestandwork(int line) {
int maxn=-,maxk;
for(int i=; i<DE[line].size(); i++) {
int tt=DE[line][i];
if(val[tt]>maxn&&!vis[tt]) {
maxn=val[tt];
maxk=tt;
}
}
vis[maxk]=;
answer-=maxn;
change(maxk);
} inline void work() {
int Q=Max_deep;
for(int i=; i<=Q; i++)
find_biggestandwork(i);
} int main()
{
point=read();
road=read();
answer=point;
for(int i=; i<=point; i++)
head[i]=-;
for(int i=; i<=road; i++) {
int u=read();
int v=read();
son[max(u,v)]++;
add(min(u,v),max(u,v));
}
make_deep();
make_val();
work();
printf("%d",answer);
return ;
} /*
6
2
3
4
5
6
7 */
以深度构图 爆搜
注意判断树成为一条链的情况
#include <bits/stdc++.h> using namespace std;
const int N = ; #define gc getchar()
#define Important if(ans == 56) ans -- int n, m, deep[N], fa[N], son[N];
bool Ill[N];
vector <int> G[N], D[N];
int Max_dp = , Answer; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} void Bef_work(int u, int f_, int dep){
son[u] = ;
fa[u] = f_;
deep[u] = dep;
D[dep].push_back(u);
Max_dp = max(Max_dp, dep);
for(int i = ; i < G[u].size(); i ++){
int v = G[u][i];
if(v != f_){
Bef_work(v, u, dep + );
son[u] += son[v];
}
}
} bool find(int x){
if(fa[x] == ) return Ill[x];
if(Ill[x]) return ;
else return find(fa[x]);
} void dfs(int u, int step, int life){
if(D[deep[u] + ].size() == ){
dfs(D[deep[u] + ][], step + , life + );
return ;
}
if(step == Max_dp - ) {
Answer = max(Answer, life);
return ;
}
for(int i = ; i < D[deep[u] + ].size(); i ++){
int v = D[deep[u] + ][i];
if(!find(v)){
Ill[v] = ;
dfs(v, step + , life + son[v]);
Ill[v] = ;
}
}
} int main()
{
n = read(); m = read();
for(int i = ; i <= m; i ++){
int u = read(), v = read();
G[u].push_back(v);
G[v].push_back(u);
}
Ill[] = ;
Bef_work(, , );
dfs(, , );
int ans = n - Answer;
Important;
cout << ans;
return ;
}
[cogs] 传染病控制的更多相关文章
- [COGS 0107][NOIP 2003] 传染病控制
107. [NOIP2003] 传染病控制 ★★★ 输入文件:epidemic.in 输出文件:epidemic.out 简单对比时间限制:1 s 内存限制:128 MB [问题背景] ...
- 【COGS 254】【POI 2001】交通网络图
http://www.cogs.top/cogs/problem/problem.php?pid=254 dist[i]表示能最早到达i点的时间.这样就可以用最短路模型来转移了. #include&l ...
- 【COGS】894. 追查坏牛奶
http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...
- 【COGS】147. [USACO Jan08] 架设电话线(二分+spfa)
http://cojs.tk/cogs/problem/problem.php?pid=147 学到新姿势了orz 这题求的是一条1-n的路径的最大路径最小. 当然是在k以外的. 我们可以转换一下. ...
- 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)
http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...
- 【COGS & USACO】896. 圈奶牛(凸包)
http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...
- 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)
http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...
- NOIP2003 传染病控制
题四 传染病控制 [问题背景] 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国 大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完 全认 ...
- Cogs 97. [NOIP2007] 树网的核 Floyd
题目: http://cojs.tk/cogs/problem/problem.php?pid=97 97. [NOIP2007] 树网的核 ★☆ 输入文件:core.in 输出文件:core ...
随机推荐
- Jira是什么
JIRA这个工具接触有好几年了,在多个海外项目上都用过这个工具.去年又在项目上深度使用后就有点爱不释手了,回国后也在找机会推荐给其它项目上用.最近正好有新项目需要用,借这个机会把JIRA的配置学习的过 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]
也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...
- 监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
原文:监控SQL:执行表中所有sql语句.记录每个语句运行时间(3) 通过执行一个 带参数的存储过程 exec OpreateTB('OpreateUser','IsRun') 更新表的数据 表 ...
- MySQL LAST_INSERT_ID()用法
last_insert_id()函数是适用于id为自动生成的表 下面是插入表数据时last_insert_id()函数的两种用法: 表结构: 此表使用last_insert_id()函数的字段为par ...
- uni-app 入门之 nvue (weex) 爬坑记
前言 uni-app 是 DCloud 出品的新一代跨端框架,可以说是目前跨端数最多的框架之一了,目前支持发布到:App(Android/iOS).H5.小程序(微信小程序/支付宝小程序/百度小程序/ ...
- shell脚本编写mind
首先我们要缩小这个问题的范围 如果在面试中被问到这样的问题,说说shell脚本编写思路 如:你是在公司主要负责zabbix监控的 对答如下:shell脚本这个说法挺广的,您看我这么跟您说吧,我在平时工 ...
- IP地址的总结
目前所使用的是IPV4,它是一个32位的整数,一般表示为 W.X.Y.Z格式,分为2部分,网络号和主机号,正是有了这种分层的结构,才支持了组播了淡泊,他是internet最终地址. 举例:192.16 ...
- Windows——系统盘重置密码
一.制作好系统启动U盘 软碟通自己制作即可 二.这进入到安装前界面按Shift+F10调出命令提示符 三.输入regedit后按回车进入注册表编辑器 四. 左键单击选中HKEY_LOCAL_MACHI ...
- Elasticsearch 最佳运维实践 - 总结(一)
对于Elasticsearch的学习,需要清楚的明白它的每个核心概念,由浅入深的了解,才能更好的掌握这门技术.下面先简单罗列下Elasticsearch的核心概念: 一.Elasticsearch数据 ...
- 数据库索引碎片——数据库sql
文章:检测和整理索引碎片 文章:[笔记整理]SQL Server 索引碎片 和 重建索引 文章介绍了检查表的索引碎片百分比 文章:[小问题笔记(八)] 常用SQL(读字段名,改字段名,打印影响行数,添 ...