给定一个有向图,问这是不是树?
各种判……
出现2条相同的边不是树,自己指向自己不是树,除根节点入度为0外其它点入度必须为1,森林,环都不是树……
program P1308;
const
maxn=15;
Var
i,j:longint;
b:array[1..maxn,1..maxn] of boolean;
indegree:array[1..maxn] of longint;
bo:array[1..maxn] of boolean;
queue:array[1..maxn+10] of longint;
procedure skip;
var
x,y:longint;
begin
repeat
read(x,y);
until (x=0) and (y=0);
end;
function main:longint;
var
i,j,k,x,y,root,node:longint;
begin
read(x,y);
if (x=-1) and (y=-1) then exit(-1);
if (x=0) and (y=0) then exit(1);
while (x>0) and (y>0) do
begin
if b[x,y] or b[y,x] or (x=y) or (indegree[y]=1) then
begin
skip;
exit(0);
end;
b[x,y]:=true;
bo[x]:=true;bo[y]:=true;
inc(indegree[y]);
read(x,y);
end;
root:=0;
node:=0;
for i:=1 to maxn do
begin
if bo[i] then inc(node);
if (indegree[i]=0) and bo[i] then
begin
if root=0 then root:=i else exit(0);
end;
end;
if (root=0) then exit(0);
i:=1;j:=1;
queue[1]:=root;
bo[root]:=false;
while i<=j do
begin
for k:=1 to maxn do
if b[queue[i],k] then
begin
if not(bo[k]) then exit(0);
bo[k]:=false;
inc(j);
queue[j]:=k;
end;
inc(i);
end;
if (j<>node) then exit(0);
exit(1);
end;
begin
i:=1;
while (true) do
begin
fillchar(indegree,sizeof(indegree),0);
fillchar(b,sizeof(b),0);
fillchar(bo,sizeof(bo),0);
j:=main;
if j=1 then writeln('Case ',i,' is a tree.')
else if j=0 then writeln('Case ',i,' is not a tree.')
else break;
inc(i);
end;
end.
分享到:
相关推荐
poj 1308 Is It A Tree_.md
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 解题...
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
poj分类poj分类poj分类poj分类
poj 2763 程序 线段树 LCA 2000MS AC
北大POJ1159-Palindrome 解题报告+AC代码
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
POJ题解 个人写法 线段树每个人都不一样
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
先利用prim算法求出最小生成树,然后通过往MST里加边来判断新生成的最小生成树是否具有最小的权值,POJ上The Unique MST(1679)题是要求判断最小生成树是否唯一,此题其实根本不用这样做,但是为了练习球次小生成树...
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
NULL 博文链接:https://200830740306.iteye.com/blog/603493
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
POJ1083的代码,POJ1083的代码,POJ1083的代码
poj 百练 题目分类 poj 百练 题目分类
POJ1048,加强版的约瑟夫问题 难度中等