RRFRNDS - editorial

@alexpizarroj just to correct, the answer to your case is 6 not 3.
the relationship is mutual, if 1 is friend of 3 then 3 is also friend of 1.

@chandan721 youā€™re right, I will edit my answer.

I tried it using BFS also, hereā€™s my approach

  1. Build an undirected graph of adjacency list using the matrix

  2. then performed BFS for each node

  3. In each BFS I keep track of levels of nodes, say if I start my BFS on node 1, then node 1 is on level 0 and all nodes adjacent to node 1 are on level 1 and so-onā€¦

  4. after doing all this for each node, I just counted all the nodes which are at level 2, LEVEL 2 because as described in the problem statement above, we need to

Find out number of pairs of vertices
u, v which are connected to each other
not directly by an edge but by another
vertex w such that w is a neighbour of
both u and v.

which means the count the pair of nodes next to nodes which are adjacent to any node.

but this still failed giving another WA :frowning:

somebody please correct where I am going wrong again !!

1 Like

tnx got my mistake :slight_smile:

hey I just tried it the same way, getting WA though :frowning:

Between each BFS, you might switch from one CC (connected component) of the graph to another, effectively not reaching friends that you did on previous iterations. The value of level[] for these friends will remain the same, and thus you might increase the value of ans incorrectly. Anyway, the approach is too slow and even after the correction youā€™ll get TLE.

Seeing authors solution , I think wouldnā€™t it be easier if we just use C++ bitset ? which supports the operation . I am getting WA though here

@alexpizarroj could you/someone else just have a look at this submission and tell why this got TLEā€™d ??

http://www.codechef.com/viewsolution/4360814

have a look hereā€¦

itā€™s a sweet simple and efficient use of bitset<>

http://www.codechef.com/viewsolution/4350498

1 Like

In the worst case, everybody will be friends with everyone. Thus, weā€™ll have V=n and E approximately equal to n^2. BFSā€™s complexity is O(V+E) which gives us O(n^2). Since youā€™re running it from every friend, you end up with O(n^3), which is way too much.
I must add: authorā€™s solution is O(n^3) too, but is has a very small constant factor which results from using bitwise operations wisely.

1 Like

can you explain a bit more what have you done inside the loop ? I mean , you just copied the whole bitset b[i] into cur ? why did you do that?

I got it, the worst case :open_mouth:

thanx again @alexpizarroj :slight_smile:

What is wrong with my code: http://www.codechef.com/viewsolution/4355232 ?? i expected that it will give TLE, but it gives WA.

@tamimcsedu actually the above one is not my solution. and there is indeed no use of ā€˜curā€™

I just got AC using the same approach, I have also put some comments along with code for more understandingā€¦ check outā€¦

http://www.codechef.com/viewsolution/4360933

1 Like

seriously thought of strassenā€™s algorithm but dropped the idea thinking it wont workā€¦damn !!

Can you explain it, please?

My unfinished code is given below:

sa=int(input()) 
flist=[int(input(), 2) for t in range(sa)]

total=0 for i in range(sa-1):
    for j in range(i+1, sa):
        if flist[i]&flist[j]!=0:
            total+=1

The code finds mutual friends; however, if A and B are already friends and share common friends, total is increased by 1,which is unwanted. I am at a loss at how to implement this condition efficiently; furthermore, the editorial does not explain how to do this either. What is the best way to include this restriction?

Thanks,

minimario

youā€™re counting the resp in the wrong place. try this test case:

4

0011

0011

1100

1100

It should output ā€œ2ā€, but your code outputs ā€œ4ā€. Try changing ā€œresp++ā€ to ā€œresp+=2ā€ and breaking the third loop after the first valid ā€œresp+=2ā€

1 Like

keep an adjacency matrix so you can check if A and B are friends in O(1)

Can you provide some code for that?

Thanks!