您的位置:首页 > 其它

【NOIP2016提高A组集训第1场10.29】完美标号

2016-10-29 21:27 357 查看
Description

给定M个二元组(A_i, B_i),求X_1, …, X_N满足:对于任意(A_i, B_i),有|X_{A_i} - X_{B_i}| = 1成立。

Input

第1行,2个整数N、M。

第2行到第M + 1行,2个整数A_i和B_i。

Output

第1行,1个字符串,”YES”表示有解,”NO”表示无解。

第2行,N个整数,X_1, X_2, …, X_N,无解则不输出。

要求|X_i| <= 1,000,000,000,任意一解均可。

Sample Input

输入1:

3 3

1 2

2 3

3 1

输入2:

6 5

1 2

2 3

3 4

4 1

5 6

Sample Output

输出1:

NO

输出2:

YES

0 1 0 1 -99 -100

Data Constraint

对于40%的数据,1 <= N <= 10。

对于100%的数据,1 <= N <= 10,000,0 <= M <= 100,000,1 <= A_i, B_i <= N。

方法

直接模拟

贴代码

var
a,b:array[0..200005,1..2]of longint;
bz:array[0..100005]of boolean;
ans,cc:array[0..100005]of longint;
i,j,k,l,n,m,x:longint;
bq:boolean;
procedure qsort(l,r:longint);
var
i,j,mid:longint;
begin
i:=l;
j:=r;
mid:=a[(i+j) div 2,1];
repeat
while a[i,1]<mid do inc(i);
while a[j,1]>mid do dec(j);
if i<=j then
begin
a[0]:=a[i];
a[i]:=a[j];
a[j]:=a[0];
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
procedure star;
begin
b[a[1,1],1]:=1;
for i:=2 to m do
if a[i,1]<>a[i-1,1] then
begin
b[a[i-1,1],2]:=i-1;
b[a[i,1],1]:=i;
end;
b[a[m,1],2]:=m;
end;
procedure dfs(x:longint);
var
i:longint;
begin
bz[x]:=true;
for i:=b[x,1] to b[x,2] do
if i<>0 then
begin
if bz[a[i,2]]=false then
begin
ans[a[i,2]]:=ans[x]+cc[x];
if cc[x]=1 then cc[a[i,2]]:=-1 else cc[a[i,2]]:=1;
dfs(a[i,2]);
end else if abs(ans[x]-ans[a[i,2]])<>1 then bq:=true;
end;
end;
begin
//assign(input,'t1.in'); reset(input);
assign(input,'perfect.in'); reset(input);
assign(output,'perfect.out'); rewrite(output);
readln(n,m);
for i:=1 to m do
begin
readln(a[i,1],a[i,2]);
a[m+i,1]:=a[i,2];
a[m+i,2]:=a[i,1];
end;
m:=m*2;
qsort(1,m);
star;
fillchar(ans,sizeof(ans),128);
for i:=1 to n do
if bz[i]=false then
begin
ans[i]:=0;
cc[i]:=1;
if bq=true then break;
dfs(i);
end;
if bq=true then writeln('NO') else
begin
writeln('YES');
for i:=1 to n do write(ans[i],' ');
writeln;
end;
close(input); close(output);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: