please solve this issue and tell me if there is any mistakes in my logic?
My code accepted here but not accepting on leetcode can you tell why?
#include<bits/stdc++.h>
using namespace std;
int n;
bool check(vectorg[])
{
int v[n];
for(int i=0;i<n;i++)
{
v[i]=-1;
}
for(int i=1;i<n;i++)
{
if(v[i]==-1)
{
queue q;
q.push(i);
int source=i;
while(!q.empty())
{
int f =q.front();
q.pop();
for(int i=0;i<g[f].size();i++)
{
int child=g[f][i];
if(v[child]==0)
{
q.push(child);
v[child]=1;
}
else
{
return false;
}
}
}
}
}
return true;
}
int main()
{
cin>>n;
int m;
cin>>m;
vector<int> z[n];
int x,y;
for(int i=0;i<n;i++)
{
cin>>x>>y;
z[x].push_back(y);
}
bool ans =check(z);
if(ans)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
}
#include<bits/stdc++.h>
using namespace std;
int n;
bool check(vectorg[])
{
int v[n];
for(int i=0;i<n;i++)
{
v[i]=-1;
}
for(int i=1;i<n;i++)
{
if(v[i]==-1)
{
queue q;
q.push(i);
int source=i;
while(!q.empty())
{
int f =q.front();
q.pop();
for(int i=0;i<g[f].size();i++)
{
int child=g[f][i];
if(v[child]==0)
{
q.push(child);
v[child]=1;
}
else
{
return false;
}
}
}
}
}
return true;
}
int main()
{
cin>>n;
int m;
cin>>m;
vector<int> z[n];
int x,y;
for(int i=0;i<n;i++)
{
cin>>x>>y;
z[x].push_back(y);
}
bool ans1 =check(z);
if(ans1)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
}
Hey @shivammishra20121999
Please share it in coding blocks ide.
And also the link to the question on both leetcode as weel as hackerblocks.
…
same code i am submitting on leetcode also
How can u submit the same code, in leetcode u can’t make you own main() in the given question and u have to call from the provded function.
So the problem is that you have prerequisite pair in the given vector on leetcode and not the actual graph which u made in your previous code.
Please re read the question on leetcode.
Each vector inside prerequisite vector is of size 2
can you modify my code for leetcode
can you please modify my code for leetcode i am not getting how to do
can you give me your number ?
Basically in Hacker blocks, you are getting m prerequisite edges
But in leetcode that is given to u inside prerequisite vector
so simply traverse the prerequisite vector and add edges to your graph .
vector z[n];
for(int i=0;i<prerequisites.size();i++){
int a=prerequistes[i][0];
int b=prerequisites[i][1];
z[a].push_back(b);
}
No I won’t
v[0]=0;if i initialise to zero instead of -1 it is not accepting can you explain why?
v[0]=0;if i initialise to zero instead of -1 it is not accepting can you explain why?
if it possible to explain with some example?