HDU5002 tree
Your task is to deal with M operations of 4 types:
1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge.
2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x.
3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d.
4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of {3, 5, 2, 5, 3} is 3.
InputThe first line contains an integer T (T<=3), which means there are T test cases in the input.
For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4).
In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b.
The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation.
If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c.
If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c.
If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c.
If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c.
All these parameters have the same meaning as described in problem description.OutputFor each test case, first output "Case #x:"" (x means case ID) in a separate line.
For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output "ALL SAME" (without quotes).Sample Input
2 3 2 1 1 2 1 2 1 3 4 1 2 4 2 3 7 7 5 3 2 1 7 3 6 1 2 1 3 3 4 3 5 4 6 4 7 4 2 6 3 4 5 -1 4 5 7 1 3 4 2 4 4 3 6 2 3 6 5 4 3 6
Sample Output
Case #1: ALL SAME 1 2 Case #2: 3 2 1 1 3 2 ALL SAME
题解:
else if(val==mx1[x])c1[x]+=c;
else if(val>mx2[x])mx2[x]=val,c2[x]=c;
else if(val==mx2[x])c2[x]+=c;
solve(x,mx1[l],c1[l]),solve(x,mx2[l],c2[l]);
solve(x,mx1[r],c1[r]),solve(x,mx2[r],c2[r]);
参考代码:
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #define inf 2000000000 8 #define ll long long 9 #define N 100005 10 using namespace std; 11 inline int read() 12 { 13 int x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 15 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 int T; 19 int n,m,top; 20 int q[N]; 21 int c[N][2],fa[N],v[N]; 22 int mx1[N],mx2[N],c1[N],c2[N],size[N]; 23 int ta[N],tc[N]; 24 bool rev[N]; 25 void solve(int x,int val,int c) 26 { 27 if(val>mx1[x])mx2[x]=mx1[x],mx1[x]=val,c2[x]=c1[x],c1[x]=c; 28 else if(val==mx1[x])c1[x]+=c; 29 else if(val>mx2[x])mx2[x]=val,c2[x]=c; 30 else if(val==mx2[x])c2[x]+=c; 31 } 32 void update(int x) 33 { 34 int l=c[x][0],r=c[x][1]; 35 mx1[x]=mx2[x]=-inf;c1[x]=c2[x]=0; 36 solve(x,v[x],1); 37 if(l)solve(x,mx1[l],c1[l]),solve(x,mx2[l],c2[l]); 38 if(r)solve(x,mx1[r],c1[r]),solve(x,mx2[r],c2[r]); 39 size[x]=size[l]+size[r]+1; 40 } 41 void add(int y,int val) 42 { 43 mx1[y]+=val;v[y]+=val; 44 if(mx2[y]!=-inf)mx2[y]+=val; 45 ta[y]+=val; 46 } 47 void change(int y,int val) 48 { 49 mx1[y]=val;v[y]=val;c1[y]=size[y]; 50 mx2[y]=-inf;c2[y]=0; 51 tc[y]=val; 52 if(ta[y])ta[y]=0; 53 } 54 void pushdown(int x) 55 { 56 int l=c[x][0],r=c[x][1]; 57 if(rev[x]) 58 { 59 rev[x]^=1;rev[l]^=1;rev[r]^=1; 60 swap(c[x][0],c[x][1]); 61 } 62 if(tc[x]!=-inf) 63 { 64 if(l)change(l,tc[x]); 65 if(r)change(r,tc[x]); 66 tc[x]=-inf; 67 } 68 if(ta[x]) 69 { 70 if(l)add(l,ta[x]); 71 if(r)add(r,ta[x]); 72 ta[x]=0; 73 } 74 } 75 bool isroot(int x) 76 { 77 return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; 78 } 79 void rotate(int x) 80 { 81 int y=fa[x],z=fa[y],l,r; 82 if(c[y][0]==x)l=0;else l=1;r=l^1; 83 if(!isroot(y)) 84 { 85 if(c[z][0]==y)c[z][0]=x;else c[z][1]=x; 86 } 87 fa[x]=z;fa[y]=x;fa[c[x][r]]=y; 88 c[y][l]=c[x][r];c[x][r]=y; 89 update(y);update(x); 90 } 91 void splay(int x) 92 { 93 top=0;q[++top]=x; 94 for(int i=x;!isroot(i);i=fa[i]) 95 q[++top]=fa[i]; 96 while(top)pushdown(q[top--]); 97 while(!isroot(x)) 98 { 99 int y=fa[x],z=fa[y]; 100 if(!isroot(y)) 101 { 102 if(c[y][0]==x^c[z][0]==y)rotate(x); 103 else rotate(y); 104 } 105 rotate(x); 106 } 107 } 108 void access(int x) 109 { 110 for(int t=0;x;t=x,x=fa[x]) 111 splay(x),c[x][1]=t,update(x); 112 } 113 void makeroot(int x) 114 { 115 access(x);splay(x);rev[x]^=1; 116 } 117 void link(int x,int y) 118 { 119 makeroot(x);fa[x]=y; 120 } 121 void cut(int x,int y) 122 { 123 makeroot(x);access(y);splay(y); 124 c[y][0]=fa[x]=0;update(y); 125 } 126 void query(int x,int y) 127 { 128 makeroot(x);access(y);splay(y); 129 if(c1[y]==size[y]) puts("ALL SAME"); 130 else printf("%d %d\n",mx2[y],c2[y]); 131 } 132 int main() 133 { 134 T=read(); 135 for(int cas=1;cas<=T;cas++) 136 { 137 printf("Case #%d:\n",cas); 138 n=read();m=read(); 139 for(int i=1;i<=n;i++) 140 v[i]=read(); 141 for(int i=1;i<=n;i++) 142 { 143 mx1[i]=v[i],c1[i]=1; 144 mx2[i]=-inf,c2[i]=0; 145 size[i]=1; 146 } 147 for(int i=1;i<=n;i++) 148 { 149 fa[i]=c[i][0]=c[i][1]=0; 150 ta[i]=rev[i]=0;tc[i]=-inf; 151 } 152 for(int i=1;i ) 153 { 154 int u=read(),v=read(); 155 link(u,v); 156 } 157 int opt,x,y,a,b,d; 158 while(m--) 159 { 160 opt=read(); 161 if(opt==1) 162 { 163 x=read();y=read();a=read();b=read(); 164 cut(x,y);link(a,b); 165 } 166 else if(opt==2) 167 { 168 a=read();b=read();x=read(); 169 makeroot(a);access(b);splay(b); 170 change(b,x); 171 } 172 else if(opt==3) 173 { 174 a=read();b=read();d=read(); 175 makeroot(a);access(b);splay(b); 176 add(b,d); 177 } 178 else 179 { 180 a=read();b=read(); 181 query(a,b); 182 } 183 } 184 } 185 return 0; 186 }