la3486 Cells 图的dfs
来源:互联网 发布:crossover mac 编辑:程序博客网 时间:2024/05/24 05:03
给一棵树,若干个询问,每次询问两个节点x,y,问x是否为y的祖先。
怎么看都是个裸题..但是数据略大..最多可能会有2000W个节点...其中,最多30W个节点是有孩子的....上来直接裸了一个LCA,结果莫名其妙RE,然后想了想,反正是一棵树,DFS一边标上号pre[u],同时记录每个节点的子树中最大的节点编号low[u],这样判断u是不是v的祖先就是看是否满足pre[u]<pre[v] &&low[u]>=low[v],然后又RE了...最后发现居然是数据太大卡递归..改成stack模拟递归就过了...太坑了
#include <iostream>#include <cstdio>#include <algorithm>#include <memory.h>#include <cstring>#include <stack>#include <cmath>using namespace std;const int maxm=20000000+20;const int maxn=300000+30;int pre[maxm],low[maxm];int st[maxn],c[maxn];int n,m,k;int dfs_clock;int dfs(int x){// int lowu=pre[u]=++dfs_clock;// for (int v=st[u]; v<st[u]+c[u]; v++)// {// if (!pre[v])// {// int lowv=dfs(v,u);// lowu=max(lowu,lowv);// }// }// low[u]=lowu;// return lowu; stack<int> s; s.push(x); while(!s.empty()) { int u=s.top(); if (pre[u]) { low[u]=dfs_clock; s.pop(); continue; } else { pre[u]=++dfs_clock; for (int v=st[u]; v<st[u]+c[u]; v++) { if (v<n) { s.push(v); } else { pre[v]=low[v]=++dfs_clock; } } } }}int main(){// freopen("in.txt","r",stdin); int tt; scanf("%d",&tt); for (int ii=1; ii<=tt; ii++) { memset(pre,0,sizeof pre); memset(low,0,sizeof low); printf("Case %d:\n",ii); scanf("%d",&n); int cnt=1; for (int i=0; i<n; i++) { st[i]=cnt; scanf("%d",&c[i]); cnt+=c[i]; } dfs_clock=0; dfs(0); int x,y; scanf("%d",&m); while(m--) { scanf("%d%d",&x,&y); if (pre[x]<pre[y] && low[x]>=low[y]) puts("Yes"); else puts("No"); } if (ii<tt) puts(""); } return 0;}
0 0
- la3486 Cells 图的dfs
- (beginer) DFS LA3486 Cells
- LA3486 Cells
- uva 1357 - Cells(dfs)
- UVa 1357 Cells (栈模拟dfs LCA)
- UVAL 3486 Cells DFS时间戳
- [UVALive 3486] Cells (dfs序)
- Connected Cells in a Grid(dfs)
- Aspose.Cells.dll的运用
- 设备树中address-cells和size-cells的含义
- 在GridView的Cells中使用Dropdownlist
- 使用Aspose.Cells的基础知识整理
- [转]使用Aspose.Cells的基础知识整理
- 使用Aspose.Cells的基础知识整理
- 使用Aspose.Cells的基础知识整理[转]
- js中的rows和cells的使用
- 使用Aspose.Cells的基础知识整理 .
- 使用Aspose.Cells的基础知识整理
- WampServer 中mysql密码设置与修改
- 关于Mac系统下Unity3d的android编译错误android (invokation failed)
- 网站技术架构四(网站高性能架构)
- AOAPC_I 高精度运算类 bign
- linux中cd ~命令
- la3486 Cells 图的dfs
- python机器学习之决策树分类
- 在CentOS上装配rabbitmq-server
- PL /SQL的数据导入导出
- Android监听方式
- google play in app billing
- 关于自己动手做小东西有感
- Unable to render template resolver could not resolve to a file
- java常用设计模式