比赛 20120704 评测结果 WTTTTTTWWE
题目名称 危险游戏 最终得分 0
用户昵称 zhangchi 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-04 11:22:03
显示代码纯文本
type
	edge=record
					l,r,w,num:longint;
				end;
var
	i,j,n,t,q,x,y,pz,py,tote,ans:longint;
	temp:edge;
	e:array[1..100000] of edge;
	b:array[1..10000] of longint;
	fa:array[1..10000] of longint;
	function find(x:longint):longint;
	begin
		if fa[x]=x then exit(x)
			else fa[x]:=find(fa[x]);
		find:=fa[x];
	end;
	function get(x:longint):longint;
	var
		l,r,m:longint;
	begin
		l:=1; r:=t;
		while r-l>1 do
			begin
				m:=(l+r) div 2;
				if x=e[m].w then exit(m);
				if x>e[m].w then l:=m else r:=m;
			end;
		if x<e[l].w then exit(l) else exit(r);			
	end;
	procedure sort(l,r:longint);
	var
		i,j,m:longint;
		t:edge;
	begin
		i:=l; j:=r;
		m:=e[(l+r) div 2].w;
		repeat
			while e[i].w<m do inc(i);
			while e[j].w>m do dec(j);
			if i<=j then
				begin
					t:=e[i]; e[i]:=e[j]; e[j]:=t;
					b[e[i].num]:=i; b[e[j].num]:=j;
					inc(i); dec(j);
				end;	
		until i>j;
		if l<j then sort(l,j);
		if i<r then sort(i,r);
	end;
	procedure kruskal;
	begin
		ans:=0; tote:=0;
		for i:=1 to n do
			fa[i]:=i;
		for i:=1 to t do
			begin
				x:=find(e[i].l);
				y:=find(e[i].r);
				if x<>y then
					begin
						ans:=ans+e[i].w;
						fa[x]:=y;
						inc(tote);
						if tote=n-1 then break;
					end;				
			end;		
	end;
begin
	assign(input,'tubea.in'); reset(input);
	assign(output,'tubea.out'); rewrite(output);
	readln(n,t);
	for i:=1 to t do
		begin
			readln(e[i].l,e[i].r,e[i].w);
			e[i].num:=i;
		end;
	sort(1,t);
	kruskal;
	writeln(ans);
	readln(q);
	for i:=1 to q do
		begin
			readln(x,y);
			pz:=get(y);
			py:=b[x];
			temp:=e[py];
			for j:=py downto pz+1 do
				begin
					e[j]:=e[j-1];
					b[e[j].num]:=j;
				end;
			e[pz]:=temp;
			b[e[pz].num]:=pz;
			e[pz].w:=y;
			kruskal;
			writeln(ans);
		end;
	close(input); close(output);
end.