蝉
来源:互联网 发布:java 值传递 引用传递 编辑:程序博客网 时间:2024/04/30 12:24
题目描述
每个人物的黑化值是距离他最近的一只蝉到他的距离
n个点构成一棵树
初始时1号节点上有一只蝉
m个操作
1.在x号节点上放一只蝉
2.求x号节点上人物的黑化值
正解据说是树链剖分
总之我是不会
那就
搜索呗
非常普通的搜索
BFS 80
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<cmath>#include<queue>#include<vector>#include<climits>#include<string>#include<cstdlib>#include<ctime>#define MOD 1000000007#define LL long longusing namespace std;int cnt,num[400005],nxt[400005],head[200005],point[400005],father[400005],blood[200005],n,m;void add(int p1,int p2){ cnt++; num[cnt]=p2; nxt[cnt]=head[p1]; head[p1]=cnt;}void west(int p){ int h=1,t=1,x,i; point[h]=p; father[h]=0; blood[point[h]]=0; while(h<=t) { x=point[h]; for(i=head[x];i;i=nxt[i]) if(num[i]!=father[h]&&blood[num[i]]) { t++; father[t]=x; point[t]=num[i]; blood[point[t]]=min(blood[point[t]],blood[point[h]]+1); } h++; }}int main(){ int i,x,y,opt; scanf("%d%d",&n,&m); memset(blood,127,sizeof(blood)); for(i=1;i<n;i++) { scanf("%d%d",&x,&y); add(x,y); add(y,x); } west(1); for(i=1;i<=m;i++) { scanf("%d%d",&opt,&x); if(opt==1) west(x); if(opt==2) printf("%d\n",blood[x]); } return 0;}
然后
其实再加一个优化
就能够再多过一个点
就是说
不只搜到蝉的时候停止
在搜到实际的黑化值与距离一致的点时
由这个点延伸出去的点
黑化值没有任何变化
直接停掉
有关这个优化和DFS
之后再补上
阅读全文
0 0
- 蝉
- 蝉
- 蝉
- 李商隐诗---蝉
- 蝉花简介
- 蝉出地洞
- 驴子与蝉
- 偶读唐.虞世南诗《蝉》有感
- 金蝉素数
- 金蝉平方数
- 蓝桥杯+金蝉素数
- 金蝉素数
- 蝉游记产品分析
- 【蓝桥杯】【金蝉素数】
- 关于金蝉平方数
- 历届试题 金蝉素数
- 蝉花与冬虫夏草的比较
- 蜕变的蝉----奋斗者的历程
- lintcode -- 生成括号
- POJ 3613 Cow Relays k步最短路 (floyd + 矩阵快速幂)
- 《人人都是产品经理》——第六章笔记
- C++之操作符重载
- 产生随机数的Random类的框架
- 蝉
- scandir() has been disabled for security reasons
- 使用PostgreSql心得
- Vuforia--VirtualButton的使用
- java发送手机验证码
- sqlserver 表被哪个过程引用
- Perl脚本中Expect模块的学习使用
- 通俗、有逻辑的写一篇说下Xgboost的原理,供讨论参考
- 输入相应类型的字符串并显示