明显是一个差分约束系统

对于第一种限制,其实就是x[a]+1<=x[b] x[b]-1<=x[a]

根据三角不等式很容易建图

但这题他比较奇怪,问的是X最多不同取值的个数

根据这张图的特殊性我们不难发现,两个强联通分量内X的取值种类是互不干涉的

也就是说我们可以分别统计每个强联通分量然后累计即可

为什么这样呢?观察这个限制,两个强联通分量之间只可能存在第二种限制的单向边,xc<=xd 显然xc,xd可以取不同取值

怎么统计强联通分量内的答案呢?

首先对于差分约束系统的可行解是最长路,每块的答案就是点与点之间距离绝对值的最大值

注意无解就是存在正环

 const inf=;
type node=record
po,next:longint;
end; var e:array[..] of node;
st,q,p,dfn,low:array[..] of longint;
d:array[..,..] of longint;
v,f:array[..] of boolean;
h,s,ans,x,y,len,j,t,i,n,m1,m2:longint; procedure max(var a:longint;b:longint);
begin
if b>a then a:=b;
end; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure add(x,y:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
p[x]:=len;
end; procedure floyd;
var i,j,k:longint;
begin
for k:= to s do
for i:= to s do
if d[q[i],q[k]]>-inf then
for j:= to s do
if d[q[k],q[j]]>-inf then
max(d[q[i],q[j]],d[q[i],q[k]]+d[q[k],q[j]]);
for i:= to s do
if d[q[i],q[i]]> then
begin
writeln('NIE');
halt;
end; k:=;
for i:= to s do
for j:= to s do
if d[q[i],q[j]]>-inf then max(k,abs(d[q[i],q[j]]));
ans:=ans+k+;
end; procedure dfs(x:longint);
var i,y:longint;
begin
inc(h);
dfn[x]:=h;
low[x]:=h;
v[x]:=true;
inc(t);
st[t]:=x;
f[x]:=true;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
dfs(y);
low[x]:=min(low[x],low[y]);
end
else if f[y] then low[x]:=min(low[x],low[y]);
i:=e[i].next;
end;
if dfn[x]=low[x] then
begin
s:=;
while st[t+]<>x do
begin
inc(s);
q[s]:=st[t];
f[st[t]]:=false;
dec(t);
end;
floyd;
end;
end; begin
readln(n,m1,m2);
for i:= to n do
for j:= to n do
if i<>j then d[i,j]:=-inf;
for i:= to m1 do
begin
readln(x,y);
add(x,y);
add(y,x);
max(d[x,y],);
max(d[y,x],-);
end;
for i:= to m2 do
begin
readln(x,y);
add(x,y);
max(d[x,y],);
end;
for i:= to n do
if not v[i] then
begin
h:=;
t:=;
dfs(i);
end; writeln(ans);
end.
05-11 11:35