Description
给定M个二元组(A_i, B_i),求X_1, …, X_N满足:对于任意(A_i, B_i),有|X_{A_i} - X_{B_i}| = 1成立。
Solution
这道题目比较明显的一点是只需要0或1就能够把整一幅图染好色,因为对于约束的两个点而言,其中一个为0另一个为1是可行的,对于一条约束条件组成的环来说,若环中节点数为奇数时,这个环明显是不合法的,而对于链来说,一定合法。所以,只需要把任意一个约束关系的一个点染成0,与它相连的染成相反的,遍历完整幅图就能判断了。
Code
var
data,next:array[0..400000] of longint;
last,a:array[0..10000] of longint;
bz:array[0..200000] of boolean;
p:array[0..10000] of boolean;
n,m,i,x,y,sum:longint;
ans:boolean;
procedure ins(x,y:longint);
begin
inc(sum);
data[sum]:=y;
next[sum]:=last[x];
last[x]:=sum;
end;
function pd(x,num:longint):boolean;
var i:longint;
begin
a[x]:=num;p[x]:=true;
i:=last[x];pd:=true;
while i<>0 do
begin
if bz[(i+1) shr 1]=false then
begin
if p[data[i]] then
begin
if abs(a[data[i]]-a[x])<>1 then
begin
p[x]:=false;
exit(false);
end else pd:=true;
end
else
begin
bz[(i+1) shr 1]:=true;
pd:=pd(data[i],1 xor num);
if pd=false then
begin
bz[(i+1) shr 1]:=false;
p[x]:=false;
exit(false);
end;
end;
end;
i:=next[i];
end;
exit(pd);
end;
begin
readln(n,m);
for i:=1 to m do
begin
readln(x,y);
ins(x,y);ins(y,x);
end;
ans:=true;
for i:=1 to n do
if p[i]=false then
if pd(i,0)=false then
begin
ans:=false;
break;
end;
if ans=false then writeln('NO')
else
begin
writeln('YES');
for i:=1 to n do write(a[i],' ');
writeln;
end;
end.