zoukankan      html  css  js  c++  java
  • [SDOI2008]洞穴勘测

    II.[SDOI2008]洞穴勘测

    也是近似的模板题,甚至比模板还要简单,连维护\(pushup\)都不需要。

    主要是为了多敲几遍熟悉代码。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define lson t[x].ch[0]
    #define rson t[x].ch[1]
    int n,m;
    struct LCT{
    	int ch[2],fa,rev;
    }t[100100];
    int identify(int x){
    	if(t[t[x].fa].ch[0]==x)return 0;
    	if(t[t[x].fa].ch[1]==x)return 1;
    	return -1;
    }
    void REV(int x){
    	swap(lson,rson),t[x].rev^=1;
    }
    void pushdown(int x){
    	if(!t[x].rev)return;
    	if(lson)REV(lson);
    	if(rson)REV(rson);
    	t[x].rev=0;
    }
    void rotate(int x){
    	int y=t[x].fa;
    	int z=t[y].fa;
    	int dirx=identify(x);
    	int diry=identify(y);
    	int b=t[x].ch[!dirx];
    	if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
    	if(b)t[b].fa=y;t[y].ch[dirx]=b;
    	t[x].ch[!dirx]=y,t[y].fa=x;
    }
    void pushall(int x){
    	if(identify(x)!=-1)pushall(t[x].fa);
    	pushdown(x);
    }
    void splay(int x){
    	pushall(x);
    	while(identify(x)!=-1){
    		int fa=t[x].fa;
    		if(identify(fa)==-1)rotate(x);
    		else if(identify(x)==identify(fa))rotate(fa),rotate(x);
    		else rotate(x),rotate(x);
    	}
    }
    void access(int x){
    	for(int y=0;x;x=t[y=x].fa)splay(x),rson=y;
    }
    void makeroot(int x){
    	access(x),splay(x),REV(x);
    }
    int findroot(int x){
    	access(x),splay(x);
    	while(lson)pushdown(x),x=lson;
    	splay(x);
    	return x;
    }
    void split(int x,int y){
    	makeroot(x),access(y),splay(y);
    }
    void link(int x,int y){
    	makeroot(x);
    	t[x].fa=y;
    }
    void cut(int x,int y){
    	split(x,y);
    	t[x].fa=t[y].ch[0]=0;
    }
    bool connected(int x,int y){
    	return findroot(x)==findroot(y);
    }
    int main(){
    	scanf("%d%d",&n,&m);
    	for(int i=1,x,y;i<=m;i++){
    		char s[10];
    		scanf("%s%d%d",s,&x,&y);
    		if(s[0]=='Q')puts(connected(x,y)?"Yes":"No");
    		if(s[0]=='C')link(x,y);
    		if(s[0]=='D')cut(x,y);
    	}
    	return 0;
    }
    

  • 相关阅读:
    三 、 Multivariance Linear Regssion练习(转载)
    二、Linear Regression 练习(转载)
    一 、机器学习基础知识(转载)
    R-CNN论文详解(转载)
    ROC,AUC,PR,AP介绍及python绘制
    SVM较全面介绍,干货!(转载)
    ubuntu14.04 + cuda8.0 + cudnnv5 + caffe + py-faster-rcnn配置
    Windows系统 环境变量
    网络加密相关
    急救知识收集
  • 原文地址:https://www.cnblogs.com/Troverld/p/14601864.html
Copyright © 2011-2022 走看看