zoukankan      html  css  js  c++  java
  • 洛谷 2173 BZOJ 2816 [ZJOI2012]网络

    【题解】

      明显的LCT模板题,c种颜色就开c棵LCT好了。。

      1 #include<cstdio>
      2 #include<algorithm>
      3 #define N 100010
      4 #define C 11
      5 #define rg register
      6 #define ls (son[c][u][0])
      7 #define rs (son[c][u][1])
      8 using namespace std;
      9 int n,m,c,k,opt,x,y,w,top;
     10 int cnt[C][N],fa[C][N],son[C][N][2],val[N],mx[C][N],rev[C][N],st[N],to[C][N][2];
     11 inline int read(){
     12     int k=0,f=1; char c=getchar();
     13     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
     14     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
     15     return k*f;
     16 }
     17 inline bool isroot(int c,int u){
     18     return son[c][fa[c][u]][1]!=u&&son[c][fa[c][u]][0]!=u;
     19 }
     20 inline bool which(int c,int u){
     21     return son[c][fa[c][u]][1]==u;
     22 }
     23 inline void pushup(int c,int u){
     24     mx[c][u]=max(max(mx[c][ls],mx[c][rs]),val[u]);
     25 }
     26 inline void pushdown(int c,int u){
     27     rev[c][ls]^=1; rev[c][rs]^=1; rev[c][u]=0; swap(ls,rs);
     28 }
     29 void rotate(int c,int u){
     30     int f=fa[c][u],gf=fa[c][f],wh=which(c,u);
     31     if(!isroot(c,f)) son[c][gf][which(c,f)]=u;
     32     fa[c][u]=fa[c][f]; fa[c][f]=u; fa[c][son[c][u][wh^1]]=f;
     33     son[c][f][wh]=son[c][u][wh^1]; son[c][u][wh^1]=f; 
     34     pushup(c,f); pushup(c,u);
     35 }
     36 inline void splay(int c,int u){
     37     st[top=1]=u;
     38     for(rg int i=u;!isroot(c,i);i=fa[c][i]) st[++top]=fa[c][i];
     39     for(rg int i=top;i;i--) if(rev[c][st[i]]) pushdown(c,st[i]);
     40     while(!isroot(c,u)){
     41         if(!isroot(c,fa[c][u])) rotate(c,which(c,u)==which(c,fa[c][u])?fa[c][u]:u);
     42         rotate(c,u);
     43     }
     44 }
     45 inline void access(int c,int u){
     46     for(rg int s=0;u;s=u,u=fa[c][u]) splay(c,u),son[c][u][1]=s,pushup(c,u);
     47 }
     48 inline void makeroot(int c,int u){
     49     access(c,u); splay(c,u); rev[c][u]^=1;
     50 }
     51 inline int find(int c,int u){
     52     access(c,u); splay(c,u); 
     53     while(ls) u=ls; return u;
     54 }
     55 inline void split(int c,int x,int y){
     56     makeroot(c,x); access(c,y); splay(c,y);
     57 } 
     58 inline void link(int c,int x,int y){
     59     cnt[c][x]++; cnt[c][y]++;
     60     if(to[c][x][0]==-1) to[c][x][0]=y; else to[c][x][1]=y;
     61     if(to[c][y][0]==-1) to[c][y][0]=x; else to[c][y][1]=x;
     62     makeroot(c,x); fa[c][x]=y;
     63 }
     64 inline void cut(int c,int x,int y){
     65     split(c,x,y); int t=son[c][y][0];
     66     if(!son[c][t][1]&&t==x){
     67         son[c][y][0]=0,fa[c][x]=0,cnt[c][x]--,cnt[c][y]--;
     68         if(to[c][x][0]==y) to[c][x][0]=-1; else to[c][x][1]=-1;
     69         if(to[c][y][0]==x) to[c][y][0]=-1; else to[c][y][0]=-1;
     70     }
     71     else{
     72         while(son[c][t][1]) t=son[c][t][1];
     73         if(t==x){
     74             son[c][fa[c][t]][0]=0,fa[c][x]=0,cnt[c][x]--,cnt[c][y]--;
     75             if(to[c][x][0]==y) to[c][x][0]=-1; else to[c][x][1]=-1;
     76             if(to[c][y][0]==x) to[c][y][0]=-1; else to[c][y][0]=-1;
     77         }
     78     }
     79 }
     80 inline bool haveedge(int c,int x,int y){
     81     split(c,x,y); int t=son[c][y][0];
     82     if(!son[c][t][1]&&t==x) return 1;
     83     else{
     84         while(son[c][t][1]) t=son[c][t][1];
     85         if(t==x) return 1;
     86     }
     87     return 0;
     88 }
     89 int main(){
     90     n=read(); m=read(); c=read()-1; k=read();
     91     for(rg int i=1;i<=n;i++){
     92         val[i]=read();
     93         for(rg int j=0;j<=c;j++) mx[j][i]=val[i];
     94     }
     95     for(rg int i=1;i<=m;i++){
     96         x=read(); y=read(); w=read();
     97         link(w,x,y);
     98     }
     99     while(k--){
    100         if((opt=read())==0){
    101             x=read(),y=read();
    102             for(rg int i=0;i<=c;i++) access(i,x),splay(i,x);
    103             val[x]=y;
    104             for(rg int i=0;i<=c;i++) pushup(i,x);
    105         }
    106         if(opt==1){
    107             x=read(),y=read(),w=read(); bool linked=0; int lastcol=0;
    108             for(rg int i=0;i<=c;i++)if(haveedge(i,x,y)){
    109                 linked=1; lastcol=i; break;
    110             }
    111 //            for(rg int i=0;i<=c;i++)if(to[i][x][0]==y||to[i][x][1]==y){
    112 //                linked=1; lastcol=i; break;
    113 //            } 
    114             if(lastcol==w&&linked){
    115                 puts("Success."); continue;
    116             }
    117             if(!linked){
    118                 puts("No such edge."); continue;
    119             }
    120             if(cnt[w][x]>=2||cnt[w][y]>=2){
    121                 puts("Error 1."); continue;
    122             }
    123             if(find(w,x)==find(w,y)){
    124               puts("Error 2."); continue;
    125             }
    126             cut(lastcol,x,y); link(w,x,y); puts("Success.");
    127         }
    128         if(opt==2){
    129             w=read(); x=read(); y=read();
    130             if(find(w,x)!=find(w,y)){
    131                 puts("-1"); continue;
    132             }
    133             split(w,x,y); printf("%d
    ",mx[w][y]);
    134         }
    135     }
    136 }
    View Code
  • 相关阅读:
    js操作FCKeditor方法(转)
    CommandArgument绑定多个值
    fckeditor给文件(包括图片)及文件夹增加删除功能
    linq中批量删除方法
    .net里使用 escape 和 unescape(转)
    类型转换
    MEP中创建基于面的风口族
    布尔运算符和位运算符
    循环结构之FOR语句
    坏天气
  • 原文地址:https://www.cnblogs.com/DriverLao/p/8781191.html
Copyright © 2011-2022 走看看