1506傳話_wikioi

來源:互聯網
上載者:User
這個演算法很簡單,但是逾時: var   n,m,i,x,y:longint;   f:array[1..1000,1..1000] of boolean; procedure floyd; var   k,i,j:longint; begin   for k:=1 to n do     for i:=1 to n do       for j:=1 to n do         f[i,j]:=f[i,j] or (f[i,k] and f[k,j]); end; begin   fillchar(f,sizeof(f),false);   read(n,m);   for i:=1 to m do     begin       read(x,y);       f[x,y]:=true;     end;   floyd;   for i:=1 to n do     if f[i,i] then writeln('T')     else write('F'); end. 簡單的深搜仍然逾時: var   n,m,a,b,i,d:longint;   f:array[1..1000,1..1000] of boolean;   used:array[1..1000] of boolean;   flag:boolean; procedure search(x:longint;var d:longint); var   k:longint; begin   if (x=i) and (d>0) and (not flag) then     begin       writeln('T');       flag:=true;       exit;     end;   for k:=1 to n do     if (f[x,k]) and (not used[k]) then       begin         used[k]:=true;         inc(d);         search(k,d);         used[k]:=false;{去掉這一句還是逾時,只能8個點,不要回溯}       end; end; begin   fillchar(f,sizeof(f),false);   read(n,m);   for i:=1 to m do     begin       read(a,b);       f[a,b]:=true;     end;   for i:=1 to n do     begin       fillchar(used,sizeof(used),false);       flag:=false;       d:=0;       search(i,d);       if not flag then writeln('F');     end; end. 弱弱地:還是用鄰接鏈表存邊吧。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.