POJ 1308(樹的判定)

2021-09-30 14:56:29 字數 1931 閱讀 2007

給定乙個有向圖,問這是不是樹?

各種判……

出現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 並查集判斷「樹」

思路 並查集 includeusing namespace std int pre 500 bool vis 500 void make set 初始化 int find set int x void join int x,int y 並 int main make set bool flag tr...

poj1308 簡單並查集

題目鏈結在這裡 題目大意 給出點和邊,問能否構成一棵樹 思路 用並查集水過去就行了。hdu1272和這道題一樣的,就是換了個題面和輸出。hdu1272 如下 include include include include define rep i,n for int i 0 i n i define...

POJ 1308(並查集,判連通,無環)

題意 給定一組資料 u,v 代表u指向 v。判斷這組資料是否形成一棵樹。題解 利用樹的特性 連通且無環。或者利用樹的性質 無環 或者聯通 且 v e 1 無環可以用並查集去判定。連通可以根據並查集確定有幾個根節點來判斷。邊數 v是加的邊數,點 e可以用 set得出。無環。e v 1。include ...