It should be (1.5,1.5, 2)
my bad updated it.
I corrected my Solution by taking in account the float values.
But i am getting WA by using Float and getting AC using double
AC code-Click Here
WA Code- Click Here
Can You tell why this happened?
because double values are way more precise than float.
As I had mentioned. DO NOT USE FLOAT! You will get precision loss at some point. Never use float. You can do the comparison by taking cross product. Double should also not pass, if there are strong test cases!
Can You explain following part of your code:
int Amax = A[0], Bmax = B[0];
for(int i = 1; i <n; i++)
{
if(A[i]*Bmax > Amax*B[i])
{
Amax = A[i];
Bmax = B[i];
}
}
vector<int> ct;
vis.assign(n, 0);
for(int i = 0; i < n; i++)
{
if(A[i]*Bmax == B[i]*Amax)
{
vertices.push_back(i);
vis[i] = 1;
}
}
I know in the first part you are calculating maximum per capita income but can you please explain(Mathematically) why it works?
Also, Please Explain the second part of the above code.
Thanks in Advance!
Should we always use double in place of float?
This was informative! Thanks!
depends on the degree of precision you need a code.
in anee004’s code he first stored max ( percapita / population ) in two int values instead of storing their division result in a double or float.
next to compare to ratios of two cities :
insted of doing ( A / P ) == ( A’ / P’ )
he checked for cross product ( A * P’ ) == ( A’ * P )
simple mathematics here.
So I’m essentially storing perMax, but instead of storing it as a double, I’m storing the numerator in Amax, and the denominator in Bmax. So at any point the current perMax = Amax/Bmax.
Now to check if per[i] is greater than current maximum, we have to check if (A[i]/B[i]) > perMAx
or,
A[i]/B[i] > AMax/Bmax
or,
A[i] * Bmax > AMax * B[i]!
That’s how my if condition arises.
In the second part, I’m just marking all the nodes which have per = maxPer as ‘1’, for my dfs later.
@aneee004 @anon41069019
If we Ignore the “float” part (because of weak testcases),
Can You Answer My Original Question(the one which started the discussion)?
The main change in the AC Code and WA code is mainly in the DFS Part.
Ummm… The dfs change is not really a change. It’s essentially the same thing. The “real” difference between the two submissions is the output format. In the “WA” submission, you have not added a “\n” after printing an.size()! 
I am Sure (perhaps) that DFS Part is Correct but Not able to figure out Why i am getting WA. Even The AC code is similar.
One Difference more i want to add,
In AC Code i used visit array int visit[200005]={0};
In WA code i used unordered_map for visit unordered_map<int,int>visit;
But i don’t think that map will lead to WA.
You are storing a[i]/b[i] in int. So, this will always give WA. eg 4/5 and 3/5 both are 0. But according to ques only 4/5 should be considered
Test Cases are Week Buddy,
Through using int i got AC
Can you share that code?