## DMPG '15 S6 - Apples to Oranges

View as PDF

Points: 17 (partial)
Time limit: 1.4s
Memory limit: 256M

Authors:
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

They say you cannot compare apples to oranges, but you actually can! In fact, by using money as a medium of exchange, we can compare anything in terms of value. Sun has a single apple. Being the incredibly talented person he is, he's visit a variety of markets with different rates of exchange for his apple. His goal is to obtain infinite apples, and therefore total control of the fruit market.

Suppose the rates of exchange are:

• orange → apples
• apple → oranges

Then it is easy to see that it is in fact impossible to obtain more than one apple through exchanges.

However, if the exchange is:

• orange → apples
• apple → oranges
• apple → grape
• grape → orange

Then Sun can exchange his original apple for a grape, a grape for an orange, and an orange for apples! He can then repeat this to get infinite apples.

The next trivial step is to note that he can now dominate and control the entire apple market.

Your goal is to find out if Sun's fruit market domination scheme is feasible.

#### Input Specification

The first line of input will contain two space-separated integers , the number of different types of fruit, and , the number of exchange rates.

The next lines will each contain the name of a fruit, no longer than alphanumeric characters long. Note that Sun starts with one apple, so APPLES will always be a type of fruit given.

Finally, the last lines will contain an exchange in the space-separated form of , , , indicating that fruit may be exchanged for units of fruit .

#### Output Specification

Output YA on a single line if Sun can get infinite apples, or NAW if he can not.

#### Sample Input

3 4
APPLES
ORANGE
GRAPE
ORANGE APPLES 2.0
APPLES ORANGE 0.5
APPLES GRAPE 1.0
GRAPE ORANGE 0.5

#### Sample Output

NAW

• commented on Dec. 12, 2020, 3:22 p.m.

What is wrong with my code? I feel like my approach is probably wrong but I have no clue why it is wrong. Can someone give me a hint as to why my code is failing?

• commented on Dec. 12, 2020, 4:17 p.m.

well first of all you seem to be printing "YAW" instead of the required "YA"

• commented on Dec. 12, 2020, 4:45 p.m.

I changed it to "YA" but I only got 80/100, is it because of decimal precision now?

• commented on Dec. 12, 2020, 6:27 p.m.

Fun fact: sometimes when comparing floating point numbers, you need to add a little bias value.

• commented on Dec. 12, 2020, 7:41 p.m.

Decimal precision is so hard, I tried playing around with it and I passed the cases that I got wrong before but then now I'm getting different cases wrong now. I'll probably give up soon.

• commented on July 10, 2020, 11:03 a.m.

What happens if you can get an infinite amount of another fruit, but not infinite apples?

(Example:

1 apple -> 1 grape

1 grape -> 1 orange

1 orange -> 2 grapes

In this scenario, you can get infinite grapes but not infinite apples.)

• commented on July 10, 2020, 11:42 a.m.

It does not matter whether or not you can get infinite grapes; it has no direct effect on the output. The output depends on whether you can make infinite apples or not.

In the specific example you provided, the output is NAW because you cannot get infinite apples.

• commented on July 29, 2019, 9:26 a.m. edit 2

I have two questions.

Is it possible for a fruit to be exchanged for itself? For example, ORANGE ORANGE 0.9.

Is it always possible to exchange something for "APPLES"? Could there be the possibility that nothing can be exchanged for apples? In a similar way, is there always a path to apples from apples?

Edit: I solved it by using a solution that works in any case.

• commented on July 29, 2019, 3:51 p.m.

NAW

• commented on March 28, 2018, 10:03 a.m. edit 5

Weak cases. Also one of the cases has M>4000

• commented on March 17, 2018, 10:59 p.m.

Help

Can anyone please check my code and see what I am doing wrong? Thanks in advance.

• commented on Oct. 8, 2017, 9:59 p.m.

unit of fruit can be exchanged for units of fruit , so it's .

• commented on Sept. 5, 2017, 8:50 p.m.

Is double accurate enough for this question?

• commented on Sept. 6, 2017, 11:13 a.m.

Yes.

• commented on Sept. 6, 2017, 4:25 p.m.

Thx

• commented on June 25, 2017, 5:07 p.m.

I've been stuck on this problem for a few hours now. Is my idea wrong or is it something else?

• commented on June 25, 2017, 8:30 p.m.

Have you considered decimals?

• commented on June 25, 2017, 10:27 p.m.

I think so. I've used double to for the decimals. Idk if the decimals would be long enough for some precision errors though.

• commented on June 25, 2017, 11:12 p.m. edit 3

I checked your code and the problem doesn't seem to be decimals. In fact, some parts of your code seem to be unnecessary. The problem involves performing a BFS (or a similar SSSP algorithm), which means that you don't have to check for the indegree or outdegree. Also, optimize your code a bit and you will AC. (Try using a HashMap)

• commented on June 20, 2017, 7:22 a.m.

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

• commented on June 25, 2017, 7:53 p.m.
• commented on June 18, 2017, 7:10 p.m. edited

What exactly is the format of the input? The sample case seems to imply that ".0" will be included in the case that it is an integer. However, experimentation says otherwise.

• commented on June 18, 2017, 7:52 p.m.

The exchange rates are decimals

• commented on June 18, 2017, 8:14 p.m.

Thanks, I know that they are decimals, the issue seems to be with the format string, in theory, it should work with integers and decimals.

• commented on June 18, 2017, 1:51 p.m. edit 2

Hint: If you're like me, and tried to scan the input as two integers separated by a dot, be aware that the input contains input like "1e-6". And a note to future problem setters, please, if only for my sanity alone, mention it in the problem statement if you have numbers like that. I wasted eight hours today on this one problem.

• commented on March 27, 2016, 3:30 p.m.

if I have 0.5 of fruit A, and the exchange rate between fruit A and fruit B is 1:2, does that mean that I can trade 0.5 of fruit A for 1 of fruit B?

• commented on March 27, 2016, 4:31 p.m.

Yes

• commented on June 23, 2015, 3:21 p.m.

In description, M <= 1000. But there are at least 3 cases M > 1000.

• commented on May 30, 2015, 4:51 p.m.

Fixed.

• commented on May 30, 2015, 2:36 p.m.

1 Grape = 3 Oranges.

Is this equivalent to:

3 Oranges = 1 Grape?

• commented on Dec. 28, 2015, 9:12 p.m.

NAW

• commented on May 30, 2015, 3:20 p.m.

No.

• commented on May 28, 2015, 11:30 a.m.

is it possible for him to exchange a single apple for half an orange for example?

• commented on May 28, 2015, 11:32 a.m.

Yes.