Codeforces/gym/100685/problem/G Gadget Hackwrench ( LCA )
来源:互联网 发布:淘宝一楼土木人怎么样 编辑:程序博客网 时间:2024/06/05 14:50
题目链接:传送门
题意:
给定一棵有向树,判断 U -> V是否可达。
分析:
设dis[u]表示点u到根节点1的距离,val[u]表示点u到根节点1的边权的和。如果U->V可达那么U一定可以到达LCA(U,V),LCA(U,V)一定可以到V,我们在建图的时候按照题目的输入给定边的权值设为1,那么如果U可以到达LCA(U,V),那么dis[U] - dis[LCA(U,V)] 等于 -(val[U] - val[LCA(U,V)]);dis[V] - dis[LCA(U,V)] 等于val[V]-val[LCA(U,V)]
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100010;struct nod{ int to,next,w;}edge[maxn*2];int head[maxn],ip,tot;bool vis[maxn];int R[maxn*2],ver[maxn*2];int dp[maxn*2][25];int first[maxn];int dis[maxn];void init(){ memset(head,-1,sizeof(head)); memset(vis,false,sizeof(vis)); dis[1]=0,ip=0,tot=0;}void add(int u,int v,int w){ edge[ip].to=v; edge[ip].w=w; edge[ip].next=head[u]; head[u]=ip++;}/***ver[i]=x:第i个点是x.first[i]=x: 点i第一次出现的位置是xR[i]=x:第i个点的深度为x;dis[i]=x;点i到根节点的距离为x.***/void dfs(int u,int dept){ vis[u]=true,ver[++tot]=u,first[u]=tot,R[tot]=dept; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(!vis[v]){ dis[v]=dis[u]+edge[i].w; dfs(v,dept+1); ver[++tot]=u,R[tot]=dept; } }}int num[maxn];void dfs2(int u,int pre,int f){ num[u]=f; for(int i=head[u];i!=-1;i=edge[i].next){ int v= edge[i].to; if(v==pre) continue; dfs2(v,u,f+1); }}void ST(int n){ for(int i=1;i<=n;i++) dp[i][0]=i; for(int i=1;(1<<i)<=n;i++){ for(int j=1;j+(1<<i)<=n;j++){ int a = dp[j][i-1],b=dp[j+(1<<(i-1))][i-1]; if(R[a]<R[b]) dp[j][i]=a; else dp[j][i]=b; } }}int RMQ(int l,int r){ int k=0; while(1<<(k+1)<=r-l+1) k++; int x = dp[l][k], y=dp[r-(1<<k)+1][k]; if(R[x]<R[y]) return x; else return y;}int LCA(int u,int v){ u=first[u],v=first[v]; if(u>v) swap(u,v); return ver[RMQ(u,v)];}int main(){ int n,m; while(~scanf("%d",&n)){ init(); for(int i=0;i<n-1;i++){ int u,v; scanf("%d%d",&u,&v); add(u,v,1); add(v,u,-1); } dfs(1,1); dfs2(1,0,0); ST(2*n-1); scanf("%d",&m); while(m--){ int u,v; scanf("%d%d",&u,&v); int var = LCA(u,v); if(dis[u]-dis[var]==-(num[u]-num[var])&&dis[v]-dis[var]==(num[v]-num[var])) puts("Yes"); else puts("No"); } } return 0;}
1 0
- Codeforces/gym/100685/problem/G Gadget Hackwrench ( LCA )
- Gym 100685G Gadget Hackwrench (LCA)
- Gym 100685G Gadget Hackwrench(LCA)
- Gym 100685G Gadget Hackwrench (LCA)
- Gym 100685 G Gadget Hackwrench LCA+DFS标记
- Codeforces-Gadget Hackwrench-LCA最近公共祖先
- Gym100685GGadget Hackwrench(LCA)
- codeforces gym 100548G
- CodeForces Gym 100735G
- CodeForces Gym 100735G
- Codeforces-GYM-100923G
- codeforces [Gym-100814G]
- Codeforces Gym-101617G
- Codeforces gym 101350G 数学
- Codeforces gym 101149 G 想法
- Codeforces Gym 101164 G. Pokemons
- Codeforces Gym-101161E【LCA+主席树】
- Gym 100685G
- Uvalive 6259 Word equations dfs+dp
- ubuntu解压缩、压缩命令
- iOS中_Bool,BOOL,bool,boolean_t,Boolean的区别总结
- nyoj 915 +-字符串
- 五张图带你体会堆算法
- Codeforces/gym/100685/problem/G Gadget Hackwrench ( LCA )
- 每日三个笑话-20151004
- 表的编辑
- C#学习日记10----番外篇 C语言进制转换
- Delphi中的注释
- 【状压dp】hdu 4539 郑厂长系列故事——排兵布阵
- 取mac信息的示例代码
- 关于多网卡的数据包传输问题
- Matlab-vision包学习-Feature Detection,Extraction and Matching-Harris角点检测