Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- // Usage: cat [input file] | php [this file]
- $networkSize = fscanf(STDIN, '%d')[0];
- $network = array_pad([], $networkSize, []);
- // At this point, the network is an array where keys are computers' ID and
- // values are empty arrays, which will be used to store the list of computers
- // the key is wired to
- // Wiring the network
- for ($readLines = 0; $readLines < $networkSize - 1; $readLines++) {
- $line = fscanf(STDIN, '%d %d');
- array_push($network[$line[0]], $line[1]);
- array_push($network[$line[1]], $line[0]);
- }
- // $network = array( [2], [2], [0 1 3], [2 4 6], [3 5], [4], [3] )
- // foreach ($network as $index => $reach) {
- // echo 'C' . $index . ' --> [ ' . implode(' ', $reach) . ' ]' . PHP_EOL;
- // } die();
- $prof = array_pad([], $networkSize, 0);
- // By butting the wire between $from and $to, returns the importance of $from
- // in its sub-network (found by recursively cutting more wires)
- // Also the importance of each computer in this sub-network is sored in $prof
- function find(int $from, int $to)
- {
- global $network, $prof;
- $importance = 1;
- foreach ($network[$from] as $computerWithInternet) {
- if ($computerWithInternet !== $to) {
- $importance += find($computerWithInternet, $from);
- }
- }
- $prof[$from] = $importance;
- return $importance;
- };
- // echo find(3, 2) . PHP_EOL;
- // foreach ($prof as $i => $x) {
- // echo 'C' . $i . ' -> ' . $x . PHP_EOL;
- // } die();
- // find(2, 3);
- // find(2, 5);
- // echo '[' . implode(', ', $prof) . ']' . PHP_EOL;
- $mini = 1000000000;
- function find2(int $from, int $to, int $wS)
- {
- global $mini, $network, $prof;
- $v = $w;
- foreach ($network[$from] as $computer) {
- if ($computer != $to) {
- $v = max([$v, $prof[$computer]]);
- find2($computer, $from, $w + $prof[$from] - $prof[$computer]);
- }
- }
- $mini = min($mini, $v);
- }
- find(0, -1);
- find2(0, -1, 0);
- // foreach ($prof as $i => $x) {
- // echo 'C' . $i . ' -> ' . $x . PHP_EOL;
- // } die();
- echo $mini . PHP_EOL;
- // -------- DEBUG ------------------
- function printNetwork(array $net)
- {
- foreach ($net as $sub) {
- echo implode(' - ', $sub) . PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement