UVA 459 Graph Connectivity











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

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,t,l,m,n,u,v,ct;
string s,c;
char d;
//freopen("459in.txt","r",stdin);
//freopen("459out.txt","w",stdout);
cin>>t;
for(i=1;i<=t;i++)
{

cin>>c;
ct=0;
k=c[0]-65;
getline(cin,s);
while(getline(cin,s))
{
if(s.length()==0)
{
break;
}
u=s[0]-65;
v=s[1]-65;

edges[u].push_back(v);
edges[v].push_back(u);
}


for(j=0;j<=k;j++)
{

if(visited[j]==0)
{
ct++;
dfs(j);

}

}
cout<<ct<<endl;
if(i!=t)
{
cout<<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