AC日记——传染病控制 洛谷 P1041
思路:
题目想问的是:
有一棵树;
对于除1外每个深度可以剪掉一棵子树;
问最后剩下多少节点;
题目意思一简单,这个题立马就变水了;
搜索就能ac;
数据有为链的情况,按深度为层次搜索的话要记得提前记录答案并return;
来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 305
#define maxm 90005
#define INF 0x7fffffff int n,m,head[maxn],E[maxm],V[maxm],cnt,ans=INF,dep[maxn];
int l[maxn],r[maxn],size[maxn],map[maxn][maxn],deepest; bool if_[maxn]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline void edge_add(int u,int v)
{
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
} void pre(int now,int fa,int deep)
{
deepest=max(deep,deepest),dep[now]=deep;
map[deep][++size[deep]]=now,l[now]=++cnt;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
pre(V[i],now,deep+);
}
r[now]=cnt;
} void dfs(int now,int ans_)
{
if(ans_>=ans) return ;
if(now==deepest)
{
ans=ans_;
return ;
}
int pos=-;
for(int i=;i<=size[now+];i++) if(!if_[l[map[now+][i]]]) pos++;
if(pos==-)
{
ans=min(ans,ans_);
return ;
}
for(int v=;v<=size[now];v++)
{
if(if_[l[map[now][v]]]) continue;
for(int i=head[map[now][v]];i;i=E[i])
{
if(dep[V[i]]>dep[now]&&!if_[l[V[i]]])
{
for(int j=l[V[i]];j<=r[V[i]];j++) if_[j]=true;
dfs(now+,ans_+pos);
for(int j=l[V[i]];j<=r[V[i]];j++) if_[j]=false;
}
}
}
} int main()
{
in(n),in(m);int u,v;
while(m--)
{
in(u),in(v);
edge_add(u,v);
}
cnt=,pre(,,),dfs(,);
cout<<ans;
return ;
}
AC日记——传染病控制 洛谷 P1041的更多相关文章
- AC日记——最大数 洛谷 P1198 [JSOI2008]
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...
- AC日记——中位数 洛谷 P1168
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- AC日记——忠诚 洛谷 P1816
题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是由于一些人的挑拨,财主还是对管家产生了 ...
- AC日记——独木桥 洛谷 p1007
题目背景 战争已经进入到紧要时间.你是运输小队长,正在率领运输部队向前线运送物资.运输任务像做题一样的无聊.你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在桥下欣赏士兵们.士兵 ...
- AC日记——潜伏者 洛谷 P1071 (模拟)
题目描述 R 国和 S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的 R 国间谍小 C 终于摸清了 S 国军用密码的编码规则: 1. S 国军方内部欲发送的原 ...
- AC日记——机器翻译 洛谷 P1540
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- AC日记——统计和 洛谷 P2068
统计和 思路: 水题: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 int n,m,tree ...
- AC日记——送花 洛谷 P2073
送花 思路: 线段树: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct TreeN ...
- AC日记——方差 洛谷 P1471
方差 思路: 线段树: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct TreeN ...
随机推荐
- Admin站点
使用admin站点 a.在settings.py中设置语言和时区 LANGUAGE_CODE = 'zh-hans' # 使用中国语言 TIME_ZONE = 'Asia/Shanghai' # 使用 ...
- Roads in the North POJ - 2631
Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...
- SHIWEITI
//Wannafly挑战赛19(牛客网) //A 队列Q #include <iostream> #include <cstdio> #include <cstring& ...
- 笔记-python-tutorial-8.errors and exceptions
笔记-python-tutorial-8.errors and exceptions 1. errors and exceptions 1.1. syntax errors >& ...
- 线段树[To be continued]
目录 数据结构--线段树 一.定义 二.性质 三.基本操作 0.结构体 1.建树 2.单点查询 3.单点修改 4.区间修改 5.区间查询 四.题目 单点修改.区域查询模板 五.鸣谢 学姐的Blog 百 ...
- luogu2093 [国家集训队]JZPFAR
题面不符?-- #include <algorithm> #include <iostream> #include <cstdio> using namespace ...
- Oracle数据库迁移--->从Windows到Linux
I did a practice to migrate the oracle database from windows to linux operation system. The followin ...
- ogre3D学习基础16 -- 手动创建实体(ManualObject)
这一节练习一下手动创建实体,用到了对象(ManualObject) 第一,依然是模板 #include "ExampleApplication.h" class Example1 ...
- apple键盘中的Tilde / back-tick key can't work
使用波浪键的时候会输出>或<. 解决办法:http://atodorov.org/blog/2015/04/30/fixing-tilde-and-function-keys-mappin ...
- ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: Incompatible namespaceIDs
用三台centos操作系统的机器搭建了一个hadoop的分布式集群.启动服务后失败,查看datanode的日志,提示错误:ERROR org.apache.hadoop.hdfs.server.dat ...