codingame/MOYEN/Teads Sponsored Challenge/script.php
2015-03-02 19:42:39 +01:00

111 lines
2.1 KiB
PHP

<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$n // the number of adjacency relations
);
class Node
{
protected $children = [];
protected $parent = null;
public function addChild(Node $node)
{
$node->setParent($this);
$this->children[] = $node;
}
public function getChildren()
{
return $this->children;
}
public function setParent(Node $node)
{
$this->parent = $node;
}
public function getParent()
{
return $this->parent;
}
}
class Nodes
{
protected static $nodes = [];
public static function addNode($key, Node $node)
{
self::$nodes[$key] = $node;
}
public static function get($key)
{
return isset(self::$nodes[$key]) ? self::$nodes[$key] : null;
}
public static function getNodes()
{
return self::$nodes;
}
}
$rootNode = null;
for ($i = 0; $i < $n; $i++)
{
fscanf(STDIN, "%d %d",
$xi, // the ID of a person which is adjacent to yi
$yi // the ID of a person which is adjacent to xi
);
$parent = Nodes::get($xi);
$child = Nodes::get($yi);
if (null === $parent) {
$parent = new Node();
Nodes::addNode($xi, $parent);
if ($rootNode === null) {
$rootNode = $parent;
}
}
if (null === $child) {
$child = new Node();
Nodes::addNode($yi, $child);
}
$parent->addChild($child);
}
$depth = 0;
foreach (Nodes::getNodes() as $k => $node) {
if ($node->getChildren()) {
continue;
}
$d = 0;
$p = $node->getParent();
while ($p) {
$d++;
$p = $p->getParent();
}
if ($d > $depth) {
$depth = $d;
}
}
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo("$depth\n"); // The minimal amount of steps required to completely propagate the advertisement
?>