【luogu3398】 仓鼠找sugar [LCA 倍增]
生活随笔
收集整理的這篇文章主要介紹了
【luogu3398】 仓鼠找sugar [LCA 倍增]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
P3398 倉鼠找sugar
長期不學競賽...導致1mol的低級錯誤出現
- 把f數組開為f[N][20]
- 寫錯判斷
我爛了QAQ我好瘟死于低級錯誤久久無法判斷出來
如果兩條路徑相交,那么一定有一條路徑的LCA在另一條路徑上
而判斷一個節點x,是否在路徑s-t上需要滿足如下幾個條件
- deep[x]>=deep[LCA(s,t)]- LCA(s,x)=x或LCA(t,x)=x;
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const int N=100000+5,inf=0x3f3f3f3f; 5 int n,q,f[N][30],dep[N]; 6 bool vis[N]; 7 template<class t>void rd(t &x){ 8 x=0;int w=0;char ch=0; 9 while(!isdigit(ch)) w|=ch=='-',ch=getchar(); 10 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 11 x=w?-x:x; 12 } 13 14 int head[N],tot=0; 15 struct edge{int v,nxt;}e[N<<1]; 16 void add(int u,int v){ 17 e[++tot]=(edge){v,head[u]};head[u]=tot; 18 } 19 20 void dfs(int u,int fa){ 21 dep[u]=dep[fa]+1; 22 f[u][0]=fa; 23 for(int i=1;i<=20;++i){ 24 if(dep[u]<(1<<i)) break; 25 f[u][i]=f[f[u][i-1]][i-1]; 26 } 27 for(int i=head[u];i;i=e[i].nxt){ 28 if(e[i].v==fa) continue; 29 dfs(e[i].v,u); 30 } 31 } 32 33 int LCA(int a,int b){ 34 if(dep[a]>dep[b]) swap(a,b); 35 for(int i=20;i>=0;--i){ 36 if(dep[f[b][i]]<dep[a]) continue; 37 b=f[b][i]; 38 } 39 if(b==a) return a; 40 for(int i=20;i>=0;--i){ 41 if(f[a][i]==f[b][i]) continue; 42 a=f[a][i],b=f[b][i]; 43 } 44 return f[a][0]; 45 } 46 47 48 int main(){ 49 freopen("in.txt","r",stdin); 50 memset(dep,0,sizeof(dep)); 51 dep[0]=-1; 52 rd(n),rd(q); 53 for(int i=1,u,v;i<n;++i) rd(u),rd(v),add(u,v),add(v,u); 54 dfs(1,0); 55 for(int i=1,a,b,c,d,x,y;i<=q;++i){ 56 rd(a),rd(b),rd(c),rd(d); 57 x=LCA(a,b),y=LCA(c,d); 58 if(dep[x]<dep[y]) swap(x,y),swap(a,c),swap(b,d); 59 if(LCA(x,c)==x||LCA(x,d)==x) printf("Y\n"); 60 else printf("N\n"); 61 } 62 return 0; 63 }
?
轉載于:https://www.cnblogs.com/lxyyyy/p/11156365.html
總結
以上是生活随笔為你收集整理的【luogu3398】 仓鼠找sugar [LCA 倍增]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10+Linux双系统安装及一些配
- 下一篇: 随心测试_软测基础_005 测试人员工作