题意:
给你m条线,从一个点到另一个点,黑线白线自己随便想,同种颜色的线不能相交,问你是否可以连成功,可以输出sane不然输出non。
思路:
1.强行把第一个为白色,然后和他冲突的为黑色,如果有一个在后面被搜索到,而且颜色和要改的颜色不同,那么就是不可以达成的。用并查集或者dfs实现。
程序:
const
maxn=10000;
var
f,x,y:array [1..maxn] of longint;
k,t,i,j,n,m:longint;
lyt:boolean;
begin
readln(t);
for k:=1 to t do
begin
readln(n,m);
lyt:=true;
for i:=1 to m do
f[i]:=0;
for i:=1 to m do
begin
readln(x[i],y[i]);
if x[i]>y[i] then
begin
j:=x[i];
x[i]:=y[i];
y[i]:=j;
end;
end;
for i:=1 to m do
begin
if f[i]=0 then f[i]:=1;
for j:=i+1 to m do
if ((x[i]<x[j]) and (x[j]<y[i]) and (y[i]<y[j])) or ((x[j]<x[i]) and (x[i]<y[j]) and (y[j]<y[i])) then
begin
if (f[i]=f[j]) then
begin
writeln('non');
lyt:=false;
break;
end;
if f[i]=1 then f[j]:=2;
if f[i]=2 then f[j]:=1;
end;
if not lyt then break;
end;
if not lyt then continue;
writeln('sane');
end;
end.