还是仙人掌,和1023一样的考虑方法
比1023简单但比1040难
环形dp的处理方法和1040一样
1 type node=record 2 po,next:longint; 3 end; 4 5 var f:array[0..100010,0..1] of longint; 6 e:array[0..400010] of node; 7 a,p,fa,dfn,low:array[0..100010] of longint; 8 h,p0,p1,q0,q1,i,x,y,n,m,len:longint; 9 10 function max(a,b:longint):longint; 11 begin 12 if a>b then exit(a) else exit(b); 13 end; 14 15 function min(a,b:longint):longint; 16 begin 17 if a>b then exit(b) else exit(a); 18 end; 19 20 procedure add(x,y:longint); 21 begin 22 inc(len); 23 e[len].po:=y; 24 e[len].next:=p[x]; 25 p[x]:=len; 26 end; 27 28 procedure work(x,y:longint); 29 var p,q:longint; 30 begin 31 q:=y; 32 while fa[q]<>x do 33 begin 34 p:=fa[q]; 35 p0:=f[p,0]+max(q0,q1); 36 p1:=f[p,1]+q0; 37 q0:=p0; 38 q1:=p1; 39 q:=p; 40 end; 41 p0:=max(q0,q1); 42 p1:=q0; 43 end; 44 45 procedure dp(x,y:longint); 46 begin 47 q0:=f[y,0]; 48 q1:=f[y,1]; 49 work(x,y); 50 f[x,0]:=f[x,0]+p0; 51 q0:=f[y,0]; 52 q1:=-2147483647; 53 work(x,y); 54 f[x,1]:=f[x,1]+p1; 55 end; 56 57 procedure tarjan(x:longint); 58 var i,y:longint; 59 begin 60 inc(h); 61 dfn[x]:=h; 62 low[x]:=h; 63 i:=p[x]; 64 f[x,0]:=0; 65 f[x,1]:=a[x]; 66 while i<>0 do 67 begin 68 y:=e[i].po; 69 if fa[x]<>y then 70 begin 71 if dfn[y]=0 then 72 begin 73 fa[y]:=x; 74 tarjan(y); 75 end; 76 low[x]:=min(low[x],low[y]); 77 if dfn[x]<low[y] then 78 begin 79 f[x,0]:=f[x,0]+max(f[y,0],f[y,1]); 80 f[x,1]:=f[x,1]+f[y,0]; 81 end; 82 end; 83 i:=e[i].next; 84 end; 85 i:=p[x]; 86 while i<>0 do 87 begin 88 y:=e[i].po; 89 if (fa[y]<>x) and (dfn[y]>dfn[x]) then 90 dp(x,y); 91 i:=e[i].next; 92 end; 93 end; 94 95 begin 96 readln(n,m); 97 for i:=1 to m do 98 begin 99 readln(x,y); 100 add(x,y); 101 add(y,x); 102 end; 103 for i:=1 to n do 104 read(a[i]); 105 tarjan(1); 106 // writeln(f[3,1],' ',f[3,0]); 107 writeln(max(f[1,1],f[1,0])); 108 end.