日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

BZOJ2816:[ZJOI2012]网络(LCT)

發(fā)布時(shí)間:2025/3/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ2816:[ZJOI2012]网络(LCT) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Description

  有一個(gè)無(wú)向圖G,每個(gè)點(diǎn)有個(gè)權(quán)值,每條邊有一個(gè)顏色。這個(gè)無(wú)向圖滿足以下兩個(gè)條件:

  • 對(duì)于任意節(jié)點(diǎn)連出去的邊中,相同顏色的邊不超過(guò)兩條。

  • 圖中不存在同色的環(huán),同色的環(huán)指相同顏色的邊構(gòu)成的環(huán)。

  •    在這個(gè)圖上,你要支持以下三種操作:

  • 修改一個(gè)節(jié)點(diǎn)的權(quán)值。

  • 修改一條邊的顏色。

  • 查詢由顏色c的邊構(gòu)成的圖中,所有可能在節(jié)點(diǎn)u到節(jié)點(diǎn)v之間的簡(jiǎn)單路徑上的節(jié)點(diǎn)的權(quán)值的最大值。

  • Input

       輸入文件network.in的第一行包含四個(gè)正整數(shù)N, M, C, K,其中N為節(jié)點(diǎn)個(gè)數(shù),M為邊數(shù),C為邊的顏色數(shù),K為操作數(shù)。

       接下來(lái)N行,每行一個(gè)正整數(shù)vi,為節(jié)點(diǎn)i的權(quán)值。

       之后M行,每行三個(gè)正整數(shù)u, v, w,為一條連接節(jié)點(diǎn)u和節(jié)點(diǎn)v的邊,顏色為w。滿足1 ≤ u, v ≤ N,0 ≤ w < C,保證u ≠ v,且任意兩個(gè)節(jié)點(diǎn)之間最多存在一條邊(無(wú)論顏色)。

       最后K行,每行表示一個(gè)操作。每行的第一個(gè)整數(shù)k表示操作類型。

  • k = 0為修改節(jié)點(diǎn)權(quán)值操作,之后兩個(gè)正整數(shù)x和y,表示將節(jié)點(diǎn)x的權(quán)值vx修改為y。

  • k = 1為修改邊的顏色操作,之后三個(gè)正整數(shù)u, v和w,表示將連接節(jié)點(diǎn)u和節(jié)點(diǎn)v的邊的顏色修改為顏色w。滿足0 ≤ w < C。

  • k = 2為查詢操作,之后三個(gè)正整數(shù)c, u和v,表示查詢所有可能在節(jié)點(diǎn)u到節(jié)點(diǎn)v之間的由顏色c構(gòu)成的簡(jiǎn)單路徑上的節(jié)點(diǎn)的權(quán)值的最大值。如果不存在u和v之間不存在由顏色c構(gòu)成的路徑,那么輸出“-1”。

  • Output

       輸出文件network.out包含若干行,每行輸出一個(gè)對(duì)應(yīng)的信息。

  • 對(duì)于修改節(jié)點(diǎn)權(quán)值操作,不需要輸出信息。

  • 對(duì)于修改邊的顏色操作,按以下幾類輸出:

  •     a) 若不存在連接節(jié)點(diǎn)u和節(jié)點(diǎn)v的邊,輸出“No such edge.”。

        b) 若修改后不滿足條件1,不修改邊的顏色,并輸出“Error 1.”。

        c) 若修改后不滿足條件2,不修改邊的顏色,并輸出“Error 2.”。

        d) 其他情況,成功修改邊的顏色,并輸出“Success.”。

      ? 輸出滿足條件的第一條信息即可,即若同時(shí)滿足b和c,則只需要輸出“Error 1.”。

  • 對(duì)于查詢操作,直接輸出一個(gè)整數(shù)。
  • Sample Input

    4 5 2 7
    1
    2
    3
    4
    1 2 0
    1 3 1
    2 3 0
    2 4 1
    3 4 0
    2 0 1 4
    1 1 2 1
    1 4 3 1
    2 0 1 4
    1 2 3 1
    0 2 5
    2 1 1 4

    Sample Output

    4
    Success.
    Error 2.
    -1
    Error 1.
    5

    Solution

    一個(gè)非常裸的lct……
    按顏色維護(hù)多顆lct即可
    讓題意坑了一次……如果改顏色的邊之前就是當(dāng)前顏色的話就Success
    在此感謝夫哥向我伸出的援手

    Code

    1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<cctype> 6 #include<algorithm> 7 #define N (100000+100) 8 using namespace std; 9 struct node 10 { 11 int x,y,c; 12 } E[N]; 13 int n,m,c,k,p=10000,x,y,z,opt,val; 14 int Father[N],Son[N][2],Val[N],Max[N],Rev[N],Ind[N]; 15 bool cmp(node a,node b){return a.x<b.x || (a.x==b.x && a.y<b.y);} 16 17 inline int read() 18 { 19 int X=0,w=0; char ch=0; 20 while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} 21 while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); 22 return w?-X:X; 23 } 24 25 int Get (int x){return Son[Father[x]][1]==x;} 26 void Update(int x){Max[x]=max(Val[x],max(Max[Son[x][0]],Max[Son[x][1]]));} 27 int Is_root(int x){return Son[Father[x]][0]!=x && Son[Father[x]][1]!=x;} 28 29 void Rotate(int x) 30 { 31 int wh=Get(x); 32 int fa=Father[x],fafa=Father[fa]; 33 if (!Is_root(fa)) Son[fafa][Son[fafa][1]==fa]=x; 34 Father[fa]=x; Son[fa][wh]=Son[x][wh^1]; 35 if (Son[fa][wh]) Father[Son[fa][wh]]=fa; 36 Father[x]=fafa; Son[x][wh^1]=fa; 37 Update(fa); Update(x); 38 } 39 40 void Pushdown(int x) 41 { 42 if (Rev[x] && x) 43 { 44 if (Son[x][0]) Rev[Son[x][0]]^=1; 45 if (Son[x][1]) Rev[Son[x][1]]^=1; 46 swap(Son[x][0],Son[x][1]); 47 Rev[x]=0; 48 } 49 } 50 51 void Push(int x){if (!Is_root(x)) Push(Father[x]); Pushdown(x);} 52 void Splay(int x) 53 { 54 Push(x); 55 for (int fa; !Is_root(x); Rotate(x)) 56 if (!Is_root(fa=Father[x])) 57 Rotate(Get(fa)==Get(x)?fa:x); 58 } 59 60 void Access(int x){for (int y=0; x; y=x,x=Father[x]) Splay(x),Son[x][1]=y,Update(x);} 61 void Make_root(int x){Access(x); Splay(x); Rev[x]^=1;} 62 int Find_root(int x){Access(x); Splay(x); while (Son[x][0]) x=Son[x][0]; return x;} 63 void Link(int x,int y){Make_root(x); Father[x]=y;} 64 void Cut(int x,int y){Make_root(x); Access(y); Splay(y); Son[y][0]=Father[x]=0;} 65 int Query(int x,int y){Make_root(x); Access(y); Splay(y); return Max[y];} 66 67 int getid(int x,int y) 68 { 69 int l=1,r=m; 70 while (l<=r) 71 { 72 int mid=(l+r)/2; 73 if (E[mid].x==x && E[mid].y==y) return mid; 74 if (E[mid].x<x || (E[mid].x==x && E[mid].y<y)) 75 l=mid+1; 76 else 77 r=mid-1; 78 } 79 return 0; 80 } 81 82 int main() 83 { 84 n=read(); m=read(); c=read(); k=read(); 85 for (int i=1; i<=n; ++i) 86 { 87 x=read(); 88 for (int j=0; j<c; ++j) 89 Val[i+j*p]=x; 90 } 91 for (int i=1; i<=m; ++i) 92 { 93 x=read(); y=read(); z=read(); 94 if (x>y) swap(x,y); 95 E[i].x=x, E[i].y=y, E[i].c=z; 96 Link(x+z*p,y+z*p); 97 Ind[x+z*p]++; 98 Ind[y+z*p]++; 99 } 100 sort(E+1,E+m+1,cmp); 101 for (int i=1; i<=k; ++i) 102 { 103 opt=read(); 104 switch (opt) 105 { 106 case 0: 107 { 108 x=read(); val=read(); 109 for (int i=0; i<c; ++i) 110 { 111 Splay(x+i*p); 112 Val[x+i*p]=val; 113 Update(x+i*p); 114 } 115 break; 116 } 117 case 1: 118 { 119 x=read(); y=read(); val=read(); 120 if (x>y) swap(x,y); 121 int id=getid(x,y); 122 if (id && E[id].c==val) 123 { 124 printf("Success.\n"); 125 break; 126 } 127 if (id==0) 128 { 129 printf("No such edge.\n"); 130 break; 131 } 132 if (Ind[x+val*p]>=2 || Ind[y+val*p]>=2) 133 { 134 printf("Error 1.\n"); 135 break; 136 } 137 if (Find_root(x+val*p)==Find_root(y+val*p)) 138 { 139 printf("Error 2.\n"); 140 break; 141 } 142 Cut(x+E[id].c*p,y+E[id].c*p); 143 Ind[x+E[id].c*p]--; 144 Ind[y+E[id].c*p]--; 145 Link(x+val*p,y+val*p); 146 Ind[x+val*p]++; 147 Ind[y+val*p]++; 148 E[id].c=val; 149 printf("Success.\n"); 150 break; 151 } 152 case 2: 153 { 154 val=read(); x=read(); y=read(); 155 if (Find_root(x+val*p)!=Find_root(y+val*p)) 156 { 157 printf("-1\n"); 158 break; 159 } 160 printf("%d\n",Query(x+val*p,y+val*p)); 161 break; 162 } 163 } 164 } 165 }

    轉(zhuǎn)載于:https://www.cnblogs.com/refun/p/8685657.html

    總結(jié)

    以上是生活随笔為你收集整理的BZOJ2816:[ZJOI2012]网络(LCT)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。