SPOJ GraCon Connectivity





#include <bits/stdc++.h>
using namespace std;
vector<long long int>edges[100];
vector<long long int>result;
long long int visited[1000],c=0;


long long int dfs(long long int s)
{
long long int x,y,z;


if(visited[s]==0)
{

visited[s]=1;
for(x=0;x<edges[s].size();x++)
{

z=edges[s][x];
if(visited[z]==0)
{
result.push_back(z);
dfs(z);

}





}

}




}



int main()
{
long long int i,j,k,l,u,v,n,e,t,ct;
cin>>t;
for(j=1;j<=t;j++)
{
ct=0;
cin>>n>>e;
for(i=1;i<=e;i++)
{
cin>>u>>v;
if(i==1)
{
l=u;
}
edges[u].push_back(v);
edges[v].push_back(u);

}
result.push_back(l);
for(i=1;i<=n;i++)
{

if(visited[i]==0)
{
if(i==1)
{
dfs(l);
}
else
{
ct=1;
break;
}

}

}
if(ct==0)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
memset(visited,0, sizeof visited);
memset(edges,0,sizeof edges);
}

return 0;
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment