[BZOJ1202] [HNOI2005]狡猾的商人

来源:互联网 发布:mac和vb虚拟机如何共享 编辑:程序博客网 时间:2024/05/03 15:44

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1202

题目大意

给定m段区间的和,判断是否合法

题解

对于给定区间[L,R]
sum[R]-sum[L-1]=C
我们用并查集来合并,加入一个权值,为该节点到它父节点的大小,也就是该节点到其父节点的区间和

var fa,g:array[0..105]of longint; t,i:longint; n,m:longint; a,b,c,t1,t2:longint;function get(a:longint):longint;var t:longint;begin if fa[a]=a then exit(a); t:=fa[a]; fa[a]:=get(fa[a]); inc(g[a],g[t]); exit(fa[a]);end;procedure work;var i,j:longint;begin readln(n,m); j:=0; fillchar(fa,sizeof(fa),0); for i:=1 to n do fa[i]:=i; fillchar(g,sizeof(g),0); for i:=1 to m do  begin   readln(a,b,c);   t1:=get(a-1); t2:=get(b);   if t1<>t2   then begin fa[t1]:=t2; g[t1]:=g[b]+c-g[a-1]; end   else    if g[a-1]-g[b]<>c    then j:=1;  end; if j=1 then writeln('false') else writeln('true');end;begin readln(t); for i:=1 to t do  work;end.
0 0
原创粉丝点击