@OceanEye6年前
08/8
21:55
先上一份代码……回去研究研究 @Sdchr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
#include <cstdio> #include <cctype> #include <algorithm> using namespace std; #define F(i,a,b) for (register int i=(a);i<=(b);i++) #define ls tr[x].c[0] #define rs tr[x].c[1] const int N=300005; int n; struct LCT { int c[2],f; int key; int mx; int tag; int rev; }tr[N]; namespace input { const int S=2000000; char s[S]; char *h=s+S; char *t=h; inline char getchr(void) { if (h==t) { fread(s,1,S,stdin); h=s; } return *h++; } inline int rd(void) { int x=0,f=1; char c=getchr(); for (;!isdigit(c);c=getchr()) if (c=='-') f=-1; for (;isdigit(c);c=getchr()) x=x*10+c-'0'; return x*f; } } using input::rd; namespace output { const int S=10000000; char s[S]; char *t=s; inline void print(int x) { if (!x) { *t++='0'; *t++='\n'; return; } if (x<0) *t++='-',x=-x; static int lis[64]; int top=0; while (x>0) lis[++top]=x%10,x/=10; while (top>0) *t++=lis[top--]+'0'; *t++='\n'; } inline void Flush(void) { fwrite(s,1,t-s,stdout); } } using output::print; inline void gmax(int &x,int y) { x=(x>y?x:y); } inline int isr(int x) { int pre=tr[x].f; return tr[pre].c[0]!=x&&tr[pre].c[1]!=x; } inline void Update(int x) { tr[x].mx=tr[x].key; if (ls>0) gmax(tr[x].mx,tr[ls].mx); if (rs>0) gmax(tr[x].mx,tr[rs].mx); } inline void Rot(int x) { int y=tr[x].f,z=tr[y].f; int l=(tr[y].c[1]==x),r=l^1; if (!isr(y)) tr[z].c[tr[z].c[1]==y]=x; tr[x].f=z; if (tr[x].c[r]>0) tr[tr[x].c[r]].f=y; tr[y].c[l]=tr[x].c[r]; tr[x].c[r]=y; tr[y].f=x; Update(y); Update(x); } inline void Push_rev(int x) { swap(ls,rs); tr[x].rev^=1; } inline void Push_tag(int x,int tag) { tr[x].key+=tag; tr[x].mx+=tag; tr[x].tag+=tag; } inline void Clear(int x) { if (ls>0) { if (tr[x].rev) Push_rev(ls); if (tr[x].tag!=0) Push_tag(ls,tr[x].tag); } if (rs>0) { if (tr[x].rev) Push_rev(rs); if (tr[x].tag!=0) Push_tag(rs,tr[x].tag); } tr[x].rev=0,tr[x].tag=0; } void Splay(int x) { static int lis[N]; int top=0; lis[++top]=x; for (int i=x;!isr(i);i=tr[i].f) lis[++top]=tr[i].f; while (top>0) Clear(lis[top--]); while (!isr(x)) { int y=tr[x].f,z=tr[y].f; if (!isr(y)) ((tr[z].c[0]==y)^(tr[y].c[0]==x))?Rot(x):Rot(y); Rot(x); } } void Expose(int x) { for (int t=0;x>0;t=x,x=tr[x].f) { Splay(x); tr[x].c[1]=t; if (t>0) tr[t].f=x; Update(x); } } void Evert(int x) { Expose(x); Splay(x); Push_rev(x); } void Link(int x,int y) { Evert(x); tr[x].f=y; } void Cut(int x,int y) { Evert(x); Expose(y); Splay(y); if (tr[y].c[0]>0) tr[tr[y].c[0]].f=0; tr[y].c[0]=0; Update(y); } int Find(int x) { Expose(x); Splay(x); int t=x; while (tr[t].c[0]>0) t=tr[t].c[0]; Splay(t); return t; } int Judge(int x,int y) { int fx=Find(x); int fy=Find(y); return fx==fy; } void Max(int x,int y,int tag) { Evert(x); Expose(y); Splay(y); Push_tag(y,tag); } int Query(int x,int y) { Evert(x); Expose(y); Splay(y); return tr[y].mx; } int main(void) { #ifndef ONLINE_JUDGE freopen("sdchr.in","r",stdin); // freopen("sdchr.out","w",stdout); #endif n=rd(); F(i,1,n-1) { int u=rd(),v=rd(); Link(u,v); } F(i,1,n) { int x=rd(); Max(i,i,x); } int m=rd(); F(t,1,m) { int k=rd(); if (k==1) { int x=rd(),y=rd(); if (Judge(x,y)) print(-1); else Link(x,y); } else if (k==2) { int x=rd(),y=rd(); if (!Judge(x,y)) print(-1); else Cut(x,y); } else if (k==3) { int w=rd(),x=rd(),y=rd(); if (!Judge(x,y)) print(-1); else Max(x,y,w); } else if (k==4) { int x=rd(),y=rd(); if (!Judge(x,y)) print(-1); else print(Query(x,y)); } } output::Flush(); return 0; } |