您的位置:首页 > 其它

bzoj 2140: 稳定婚姻 (二分图)

2015-03-22 22:09 295 查看
//==========================

蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/ 转载要声明!

//==========================

判断二分图中某条路是否是唯一的。

网络流做法要加个tarjan

二分图就是再增广看能不能增广

网络流(快但是长)

type
arr=record
toward,next,from:longint;
flag:boolean;
end;
const
maxn=100000;
maxm=1000000;
var
edge:array[0..maxm]of arr;
first,cur,d,p,gap,num,e,match,matche:array[0..maxn]of longint;
chose:array[0..maxn]of boolean;
trie:array[0..maxn,'A'..'z'] of longint;
n,esum,s,t,tot,total,peo,time,scc,top:longint;

procedure addedge(j,k:longint);
begin
inc(esum);
edge[esum].from:=j;
edge[esum].toward:=k;
edge[esum].next:=first[j];
edge[esum].flag:=true;
first[j]:=esum;
end;

function find(ss:string):longint;
var
i,u:longint;
begin
u:=0;
for i:=1 to length(ss) do u:=trie[u][ss[i]];
exit(num[u]);
end;

procedure into;
var
i,j,k,m,u,boy,girl:longint;
ss:string;
begin
esum:=0;
tot:=0;
total:=0;
peo:=0;
fillchar(first,sizeof(first),0);
readln(n);
for i:=1 to n do begin

readln(ss);
ss:=ss+' ';
u:=0;
j:=1;
while (ss[j]<>' ') do begin
if trie[u][ss[j]]=0 then begin
inc(total);
trie[u][ss[j]]:=total;
end;
u:=trie[u][ss[j]];
inc(j);
end;
inc(peo);
num[u]:=peo;
girl:=peo;

inc(j);
u:=0;
while (ss[j]<>' ') do begin
if trie[u][ss[j]]=0 then begin
inc(total);
trie[u][ss[j]]:=total;
end;
u:=trie[u][ss[j]];
inc(j);
end;
inc(peo);
num[u]:=peo;
boy:=peo;

e[i]:=esum+1;
addedge(girl,boy);

end;
readln(m);
while m>0 do begin
dec(m);
readln(ss);
i:=pos(' ',ss);
j:=find(copy(ss,1,i-1));
k:=find(copy(ss,i+1,length(ss)-i));
addedge(j,k);
end;
end;

function dfs(x:longint):boolean;
var
i,too:longint;
begin
i:=first[x];
while i>0 do begin
too:=edge[i].toward;
if edge[i].flag and chose[too] then begin
chose[too]:=false;
if (match[too]=0) or dfs(match[too]) then begin
edge[matche[too]].flag:=true;
matche[too]:=i;
match[too]:=x;
edge[i].flag:=false;
exit(true);
end;
end;
i:=edge[i].next;
end;
exit(false);
end;

procedure work;
var
i,j,boy,girl:longint;
begin
for i:=1 to n do begin
fillchar(chose,sizeof(chose),true);
dfs(edge[e[i]].from);
end;
//for i:=1 to n do writeln(match[i<<1]);
for i:=1 to n do begin
fillchar(chose,sizeof(chose),true);
j:=e[i];
if edge[j].flag then begin
writeln('Unsafe');
continue;
end;
boy:=edge[j].toward;
girl:=edge[j].from;
match[boy]:=0;
if not dfs(girl) then begin
match[boy]:=girl;
matche[boy]:=j;
writeln('Safe');
end
else writeln('Unsafe');
end;
end;

begin
into;
work;
end.


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: