zoukankan      html  css  js  c++  java
  • P3398 仓鼠找sugar

    判断树上两条路径是否相交

    我们可以判断两条路径各自的LCA的子树是否有交集即可,或者可以判断其中一条路径的LCA 是否在另一条路径上

    inline int LCA(int x, int y) {
    	while(top[x]^top[y]) {
    		if(dep[top[x]] < dep[top[y]]) std::swap(x, y);
    		x = fa[top[x]];
    	}
    	return dep[x] < dep[y] ? x : y;
    }
    
    inline bool in(int anc, int b) {
    	return dfn[anc] <= dfn[b] && dfn[b] <= dfn[anc] + siz[anc] - 1;
    }
    
    inline int dis(int a, int b) {
    	return dep[a] + dep[b] - (dep[LCA(a, b)] << 1);
    }
    
    int main() {
    	read(n);
    	read(q);
    	int x, y;
    	rep(i, 2, n) {
    		read(x);
    		read(y);
    		G.add(x, y);
    		G.add(y, x);
    	}
    	dfs(1);
    	dfs2(1, 1);
    
    	int a, b, c, d;
    	while(q--) {
    		read(a);
    		read(b);
    		read(c);
    		read(d);
    
    		int x(LCA(a, b));
    		int y(LCA(c, d));
    		
    		
    		if(dis(a, y) + dis(b, y) == dis(a, b) || dis(c, x) + dis(d, x) == dis(c, d)) {
    			puts("Y");
    		} else
    			puts("N");
    		/*if( (in(x, c) || in(x, d)) && (in(y, a) || in(y, b))  ) {
    			puts("Y");
    		} else puts("N");*/
    	}
    
    	return 0;
    }
    

    本文来自博客园,作者:{2519},转载请注明原文链接:https://www.cnblogs.com/QQ2519/p/15501011.html

  • 相关阅读:
    CTS2019 题解
    CTS2019 & APIO2019 游记
    WF 2019
    BZOJ 2560 及其加强
    UOJ 191
    SCOI2019 退役记
    HTML5 本地存储
    js数据类型
    解析json成javascript对象
    http状态码;
  • 原文地址:https://www.cnblogs.com/QQ2519/p/15501011.html
Copyright © 2011-2022 走看看