SOCIALNE - Possible Friends

no tags 

Josué, one of the undergraduate students in PUCMM, is developing a Social Network, but he is having difficulties in identifying the person that has more possible friends. Two persons are possible friends if they are not friends and they have at least one common friend, for example if person A is friend only to person B, and person B is friend of C, then, A and C are possible friends. You are about to help him in this task. Given the network table, you have to write a program that finds the person that has more possible friends, if more than one person matches this criteria, then select the one that comes first ( the one that has the lower ID).


The first line is T (1 ≤ T ≤ 1,000), the number of test cases, then T test cases follow.

Each test case consists in a 'Y' or 'N' square matrix(MxM) representing the friendship of the network, where M is the number of persons.


M (1 ≤ M ≤ 50)

The square matrix has M lines, each line has M characters.

The first line of the matrix corresponds to person 0, the next line to person 1, and so on.

On each line the leftmost character corresponds to person 0, the next character to person 1, and so on.

So if character j of the line i is 'Y', it means that person 'i' is a friend of 'j'.

For each person i, character i of line i will be 'N'.

For each i,j character j of line i will be the same as character i of line j.


For each test case, you have to output one line containing the ID of the person(0-based) that has more possible friends and the number of possible friends this person has, separated by a blank space.


0 1
2 2
3 4

hide comments
n_o_o_b: 2019-11-08 12:25:12

Does bruteforce work?

hoangvnh: 2019-10-16 19:43:57

AC in 1 go !!! Just a little bit tricky when read input, and then use FloydWarshall and AC :D

prestonbui97: 2018-04-08 07:27:33

AC in 2nd go!!

ameyanator: 2018-03-30 09:47:53

If A is friend of B then B has to be a friend of A right?

Sigma Kappa: 2017-08-11 23:37:47


vengatesh15: 2017-01-25 10:32:11

AC in 1 go:-)

sushantoberoi3: 2016-10-31 07:46:40

bahut tagda question really liked it!! first time missing that check condition...edited the code n AC in 2nd go!!

Aman Singh: 2016-08-14 07:26:01

Problem setter is Naive. It's not mentioned but for case:
you have to give output 0 0.

sudip_95: 2016-06-16 08:27:42

it's quite silly! using cin,cout gives TLE. Adding ios::sync_with_stdio(false); gives the same code AC!!!

Ankit Sultana: 2015-06-08 20:29:45

Forgot to note this: "if more than one person matches this criteria, then select the one that comes first ( the one that has the lower ID)."

Added by:Olson Ortiz
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:C CSHARP C++ 4.3.2 CPP JAVA
Resource:Used in 1st Programming contest PUCMM ACM-ISC 2010 (Dominican Republic)