Topological Sorting by daira

<?for(;$a=fgets(STDIN);$e++){list($n,$v)=split(":",$a);$l[$n]=array();foreach(split(" ",trim($v)) as$v)$v?$l[$n][$v]=1:0;}for(;$l;)foreach($l as$k=>$v)if(!$v){unset($l[$k]);for($i=0;$i++<$e;)unset($l[$i][$k]);echo"$k\x0d
";break;}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page