Two-hop relay
Statement
A mountain region has N wireless relay stations. The relay stations are numbered from 1 to N.
Some pairs of relay stations can exchange a signal directly. A direct link works both ways. If relay station A can exchange a signal directly with relay station B, then relay station B can also exchange a signal directly with relay station A. No relay station has a direct link to itself.
A signal can travel far by passing through relay stations. When a signal goes from relay station A to relay station B, the number of direct links it rides along is called the hop count. Take the smallest hop count over all routes from A to B. If that value is 2 or less, relay station B is called a two-hop neighbor of relay station A. A relay station is not counted as its own two-hop neighbor.
Write a program that counts the two-hop neighbors of every relay station and computes the largest of those counts.
For example, assume N=5. Suppose the information about direct links is given by the table below. Y means the two stations can exchange a signal directly, and N means they cannot.
| Station 1 | Station 2 | Station 3 | Station 4 | Station 5 | |
|---|---|---|---|---|---|
| Station 1 | N | Y | Y | N | N |
| Station 2 | Y | N | N | N | N |
| Station 3 | Y | N | N | Y | N |
| Station 4 | N | N | Y | N | Y |
| Station 5 | N | N | N | Y | N |
The table shows four direct links, stations 1 and 2, stations 1 and 3, stations 3 and 4, and stations 4 and 5.
Count from relay station 3. Stations 1 and 4 are reached with a hop count of 1. Station 2 is reached through station 1 and station 5 is reached through station 4, both with a hop count of 2. So relay station 3 has 4 two-hop neighbors.
Counting the same way for every relay station gives the table below.
| Station number | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Two-hop neighbors | 3 | 2 | 4 | 3 | 2 |
The largest value in the table is 4, at relay station 3. So the answer for this example is 4.
Now count from relay station 2. Station 1 is reached with a hop count of 1, and station 3 is reached through station 1 with a hop count of 2. Station 4 has to go through stations 1 and 3, so its hop count is 3, and station 5 has a hop count of 4. Both are larger than 2, so neither is a two-hop neighbor. So relay station 2 has 2 two-hop neighbors.
A relay station may have no direct link at all. Such a station cannot send a signal anywhere, so it has 0 two-hop neighbors.
Input and Output Examples
Example 1
Input
5 NYYNN YNNNN YNNYN NNYNY NNNYN
Output
4
Example 2
Input
8 NNYNNNNN NNNNYNNY YNNNNYNN NNNNYNYN NYNYNNNN NNYNNNNN NNNYNNNN NYNNNNNN
Output
4
Example 3
Input
2 NN NN
Output
0
Input
The first line holds the number of relay stations N. N is a natural number from 2 to 50.
The next N lines hold the information about direct links. Each line is a string of length N, and each character is Y or N. If the B-th character of the A-th line is Y, relay station A and relay station B can exchange a signal directly, and if it is N, they cannot. The A-th character of the A-th line is always N, and the B-th character of the A-th line is always the same as the A-th character of the B-th line.
Output
Print on one line the largest number of two-hop neighbors over all relay stations. Do not print which relay station has that value. If there is no direct link at all, so that every relay station has 0 two-hop neighbors, print 0.
Requirements
- Decide two-hop neighbors from the route with the smallest hop count
- Count both the stations with a hop count of 1 and the stations with a hop count of 2 as two-hop neighbors
- Do not count a relay station as its own two-hop neighbor
- A direct link works both ways
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 NN NN
0
8 NNYNNNNN NNNNYNNY YNNNNYNN NNNNYNYN NYNYNNNN NNYNNNNN NNNYNNNN NYNNNNNN
4
5 NYYNN YNNNN YNNYN NNYNY NNNYN
4
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.