## CCC '07 S3 - Friends

View as PDF

Points: 10
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Canadian Computing Competition: 2007 Stage 1, Senior #3

In a certain school, it has been decided that students are spending too much time studying and not enough time socializing. To address this situation, it has been decided to give every student a friend. Friendship is one-way. That is, if Janet is assigned to be Sarah's friend, Janet must be friendly to Sarah, but Sarah is not required to reciprocate.

The assignment of friends is done by computer using student numbers. Every student is assigned exactly one friend. Sometimes, a 'circle of friends' occurs. For example, if Marc is assigned Fred, Fred is assigned Lori, Lori is assigned Jean, and Jean assigned Marc, we have a circle of 4 friends containing Marc, Fred, Lori, and Jean. In the circle, we can say that Marc has a separation of 0 from Fred, of 1 from Lori, of 2 from Jean, and of 3 from Marc.

Your task is to identify, given the computer assignment of friends, whether two students are in the same circle of friends, and if so, determine their separation.

#### Input Specification

Input begins with a single integer (), on a line by itself, indicating the number of students in the class. The next lines contain the computer assignment of friendship. An assignment is of the form x y (where ). For example, 1234 8765 is a possible friendship assignment indicating that student 1234 must be friends with student 8765.

Following the friendship assignments, there are a series of lines containing two student numbers, separated by a single whitespace. These lines represent the pairs of students that you will determine if they are in same circle of friends and, if so, their separation. The last line of input can be identified by the use of the 0 0 as the friend assignment.

#### Output Specification

For each case, you are to print, on a separate line, either Yes or No depending on whether they are in the same circle of friends. If the answer is Yes, follow the output Yes with a single whitespace and then an integer indicating the friend's separation.

#### Sample Input

6
1 2
2 3
3 1
10 11
100 10
11 100
1 100
2 3
0 0

#### Output for Sample Input

No
Yes 0

• commented on Nov. 7, 2020, 12:09 a.m.

The first paragraph was so randomly sad :(

• commented on Nov. 18, 2019, 8:55 p.m. edited

Why do I keep getting out of memory error?

• commented on Nov. 18, 2019, 10:05 p.m.

Seriously? boolean[][] a = new boolean[10000][10000]; (~100MB) is for sure to MLE.

• commented on Nov. 19, 2019, 8:56 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Aug. 6, 2019, 3:36 p.m. edited

I'm getting MLE in python could someone check out my submission

• commented on Feb. 6, 2019, 12:21 p.m. edited

Does anyone know why I keep getting test case 2 wrong? I'm inputting the sample data values from CEMC and I'm getting the correct output, but on DMOJ it's giving me something else.

update: nevermind got it, dmoj has some different things it does with it's compiler.

• commented on Feb. 6, 2019, 2:12 p.m.

• commented on Jan. 31, 2019, 12:41 p.m.

How many queries are possible?

• commented on Dec. 21, 2017, 8:05 p.m.

Why does it say RTE? can someone check my code?

• commented on Nov. 17, 2019, 10:48 a.m.

You're breathtaking

• commented on Dec. 21, 2017, 8:42 p.m.

Your arrays occupy around 10 GB of memory... Note that each friend is only assigned to one other friend...

• commented on Nov. 25, 2017, 3:40 p.m.

Hi, I'm having issues with the fourth test case, I get "IR (java.lang.NullPointerException)". When I download the data from here everything appears to work correctly. Can someone confirm if this is still an issue or check my code? Thanks

• commented on Nov. 26, 2017, 11:11 a.m.

For some troll reason, it appears that the two student numbers which follow the friendship assignments might not have been assigned friends during the aforementioned friendship assignments.

Additionally, I am not sure why the data on the judge differs from that of the CEMC website, but as the input specifications does not explicitly say that this is an error, I suppose we shall keep it as it is.

• commented on Dec. 1, 2017, 12:14 a.m.

Thanks for the reply. In the problem statement it states "Every student is assigned exactly one friend", so I feel it should be implied that the input will not contain invalid data. In a "real world" scenario the bug would be in the algorithm that assigns students, not in the algorithm we are writing to solve the problem, so as per method contract any invalid data should be throwing errors (i.e. undefined behavior). Besides, this is a programming challenge, not an input validation exercise. Very troll indeed.

For anyone wondering how to fix the error- there are students not assigned a friend so a lookup in a map/dictionary/whatever will result in an error, e.g. 111 222 222 is never assigned a friend, thus the key won't exist.

• commented on July 20, 2017, 3:22 p.m. edit 3

My code keeps raising a Value Error on test case 4 and I have no idea why. There should be no problems and test cases 1-3 run smoothly. Does anyone know what's wrong?

EDIT

I changed my code to use a dictionary and now I get a Key Error on the same test case. This is triggering.

EDIT 2: Jk I misplaced an if statement and screwed it up. All good now

• commented on May 13, 2017, 6:10 p.m. edited

Hey, I am getting a key error on test case 4, but when I run that data from the CEMC website, I get no such error, and my program outputs the correct output. What's going on?

EDIT: Never mind, I changed the way I stored my data and it ended up fixing it.

• commented on Dec. 10, 2016, 7:58 p.m.

can some admin help check? thanks