Bro, I don’t know much about python. I don’t know why this is giving RE
can u provide some test cases so that can i can debug please …
i am new don’t how to generate it.
it will be kind if you tag someone who knows python and can help me out ?
please please…
Thanks! It helped.
I am getting WA . Even after thinking for hours I can’t spot the mistake. Please help.
Here’s my solution CodeChef: Practical coding for everyone
You certainly have overflow while comparing ar1[i]*e > ar2[i]*w
, use long long datatype.
Thank you so much sir !
Now i can happily end the day after getting that green tick.
Thanks again for helping noobs like me.
CodeChef: Practical coding for everyone still not getting accepted… even after using scanf and printf
Friends I am getting TLE. could someone help me please.
Your logic seems wrong-
-
For max you need double.
-
You don’t need to output gdp. You need to output chosen provinces.
-
However, it should also be possible to travel from each of the chosen provinces to any other chosen province without visiting any of the abandoned provinces.
You are not following this condition.
Correct Logic-
You can easily decide max gdp and then take all nodes which have value equal to that gdp because you need to maximize the chosen provinces as well but all these provinces can’t be chosen as the graph may be disconnected. Choose that component that has maximum nodes
thanks
can this question be done by using normal logic i.e. without using graphs
No, you need to know BFS/DFS (Graph Algorithms) to do this problem.
I really appreciate the fact the you took care of all the mistakes you were doing early.
You got rid of floating point values by cross multiplication (comparing integers).
Secondly, you also learnt Graph Algorithms to solve this problem.
The only flaws in your solution are:
You are required to print only those things which are asked in Problem. Like, cout<<num<<" "<<den<<endl;
should be commented. You must have used it while debugging but you need to remove while submitting.
The second mistake is you are decrementing the nodes while taking input like in these lines- g[u-1].push_back(v-1); g[v-1].push_back(u-1);
. While outputting you should increment them back as cout<<x+1<<" ";
. Hope you understood your mistakes.
AC Code
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
#define PI acos(-1.0)
#define Pi 3.141592653589793
#define SQR(n) ( n * n )
/// Math End
/// Pair Start
#define F first
#define S second
#define mp make_pair
/// Pair End
/// Array Start
#define SET(a) memset( a, -1, sizeof a )
#define CLR(a) memset( a, 0, sizeof a )
#define MEM(a,val) memset( a, val, sizeof(a) )
/// Array End
/// Extra Start
#define pb push_back
#define SS stringstream
#define ull unsigned long long
#define mod 1000000007
#define INF 2147483647
#define SIZE 2000001
#define _cin ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
vector<int> paths;
int32_t main() {
_cin
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
vector<int> a(n),b(n);
int x;
for(auto &i: a) {
cin>>x;
i = x;
}
for(auto &i: b) {
cin>>x;
i = x;
}
vector<int> g[n];
for(int i=0; i<m; i++) {
int u,v;
cin>>u>>v;
g[u-1].push_back(v-1);
g[v-1].push_back(u-1);
}
int num=a[0];
int den=b[0];
for(int i=0; i<n; i++)
{
if(a[i]*den > b[i]*num)
{
den=b[i];
num=a[i];
}
}
// cout<<num<<" "<<den<<endl;
vector<int>v(n,0);
vector<int>ans;
for(int i=0; i<n; i++)
{
if(v[i]==0 && (num*b[i]== den*a[i]))
{
queue<int>q;
q.push(i);
vector<int>temp;
v[i]=1;
while(q.empty()!=true)
{
int top=q.front();
q.pop();
temp.push_back(top);
for(auto x:g[top])
{
if(v[x]==0 && num*b[x]== den*a[x])
{
q.push(x);
v[x]=1;
}
}
}
if(temp.size()>ans.size())
{
ans=temp;
}
}
}
cout<<ans.size()<<endl;
for(auto x:ans)
{
cout<<x+1<<" ";
}
cout<<endl;
}
return 0;
}
If you prefer link
Notice the changes in the above codes.
same case for me as that of @ram2012k
TLE without syn_with_studio(false)
AC with syn_with_studio(false).
thank you very much for giving your time and helping me.
Just one last thing
I am able to solve atmost 4 to 5 questions in long challenges.
Can you plzz tell me what i should study to be able to solve more hard problems .
And
How should i approach questions.
I would be very thankful if you could tell me