拓排+各种判……
Program P1094;
type
map3=record
indegree:array['A'..'Z'] of longint;
map:array['A'..'Z',1..26] of char;
outdegree:array['A'..'Z'] of longint;
end;
var
n,m,i,j,num,value,topvalue:longint;
s:string;
topout:string;
map,map2:map3;
mark:array['A'..'Z'] of boolean;
tag:boolean;
Function topsort:longint;
var
j,k,h,s,zero:longint;
flag:boolean;
i:char;
begin
topsort:=3;
topout:='';
flag:=false;
h:=1;
s:=0;
zero:=0;
for i:='A' to 'Z' do if mark[i] then if map2.indegree[i]=0 then begin inc(zero); topout:=topout+i; end;
if zero=0 then exit(2);
s:=length(topout);
while (h<=s) do
begin
if (h<s) then flag:=true;
for j:=h to s do
begin
for k:=1 to map2.outdegree[topout[j]] do
begin
dec(map2.indegree[map2.map[topout[j],k]]);
if map2.indegree[map2.map[topout[j],k]]=0 then
begin
topout:=topout+map2.map[topout[j],k];
end;
end;
if length(topout)>num then exit(2);
end;
h:=s+1;
s:=length(topout);
end;
if s<num then exit(2);
if (s=num) and (num<n) then exit(3);
if flag or (num<n) then exit(3) else exit(1);
end;
Procedure ski(step:longint);
var
i:longint;
s:string;
begin
for i:=step+1 to m do readln(s);
end;
Procedure pri(value,step:longint);
begin
if value=1 then writeln('Sorted sequence determined after ',step,' relations: ',topout,'.');
if value=3 then writeln('Sorted sequence cannot be determined.');
if value=2 then
begin
writeln('Inconsistency found after ',step,' relations.');
ski(step);
end;
if value=1 then ski(step);
end;
begin
{ assign(input,'P1094.in');
assign(output,'p1094.out');
reset(input);
rewrite(output); }
readln(n,m);
while (n+m>0) do
begin
topout:='';
fillchar(map,sizeof(map),0);
fillchar(mark,sizeof(mark),false);
num:=0;
value:=3;
for i:=1 to m do
begin
readln(s);
if (ord('A')-1+n<ord(s[1])) or (ord('A')-1+n<ord(s[3])) or (s[1]=s[3]) then
begin
value:=2;
break;
end;
if not(mark[s[1]]) then begin mark[s[1]]:=true; inc(num); end;
if not(mark[s[3]]) then begin mark[s[3]]:=true; inc(num); end;
tag:=false;
for j:=1 to map.outdegree[s[1]] do
if map.map[s[1],j]=s[3] then begin tag:=true; break; end;
if tag then continue;
inc(map.indegree[s[3]]);
inc(map.outdegree[s[1]]);
map.map[s[1],map.outdegree[s[1]]]:=s[3];
map2:=map;
topvalue:=topsort;
if topvalue<=2 then begin value:=topvalue; break; end;
end;
pri(value,i);
readln(n,m);
end;
{ close(input);
close(output); }
end.
分享到:
相关推荐
NULL 博文链接:https://200830740306.iteye.com/blog/603488
poj 1091 拓扑排序加上foyld_warshall算法实现
北大POJ1094-Sorting It All Out 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) ... (4)拓扑排序 (poj1094) (5)二分图的最大匹配 (匈牙利算法) (poj3041,poj3020) (6)最大流的增广路算法(KM算法). (poj1459,poj3436) ......
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
POJ上的一道题,我感觉挺难的。分享给大家,这是利用拓扑排序实现,也算是拓扑排序的一道例题。有助于大家对拓排的理解
北大POJ1159-Palindrome 解题报告+AC代码
poj分类poj分类poj分类poj分类
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
北大POJ2002-Squares 解题报告+AC代码
POJ1048,加强版的约瑟夫问题 难度中等
POJ1083的代码,POJ1083的代码,POJ1083的代码