#include <iostream>
#include <vector>
#include <fstream>
#include <list>
using namespace std;
int arrr[20],counter=0;
void DFS(int s,vector < vector < int > > v,bool c[200])
{
std::vector<int > ::iterator it;
cout<<s<<"->";
c[s]=true;
for(it=v[s].begin();it!=v[s].end();++it)
{
if(!c[*it])
{
c[*it]=true;
DFS(*it,v,c);
}
}
}
int main()
{
vector < vector < int > > v;
bool c[200];
for (int i = 0; i < 200; i++)
{
c[i]=false;
}
ifstream fin;
fin.open("/home/pawan/Desktop/input1.txt");
if(!fin.is_open())
{
cout<<" file not open";
return 0;
}
int n;
fin>>n;
v.resize(n);
int i,j,w;
while(!fin.eof())
{
fin>>i>>j>>w;
v[i].push_back(j);
v[j].push_back(i);
n--;
}
for (i = 0; i < v.size(); i++)
{
std::vector<int >::iterator it;
cout<<i<<"-> ";
for(it=v[i].begin();it<v[i].end();it++)
cout<<*it<<" ";
cout<<endl;
}
DFS(1,v,c);
}
input1.txt
15 0 2 6 1 2 5 2 3 1 1 4 6 2 7 7 4 7 4 3 6 8 4 5 12 5 8 4 8 7 2 8 11 7 11 6 4 5 9 11 9 10 8 10 11 2
#include <vector>
#include <fstream>
#include <list>
using namespace std;
int arrr[20],counter=0;
void DFS(int s,vector < vector < int > > v,bool c[200])
{
std::vector<int > ::iterator it;
cout<<s<<"->";
c[s]=true;
for(it=v[s].begin();it!=v[s].end();++it)
{
if(!c[*it])
{
c[*it]=true;
DFS(*it,v,c);
}
}
}
int main()
{
vector < vector < int > > v;
bool c[200];
for (int i = 0; i < 200; i++)
{
c[i]=false;
}
ifstream fin;
fin.open("/home/pawan/Desktop/input1.txt");
if(!fin.is_open())
{
cout<<" file not open";
return 0;
}
int n;
fin>>n;
v.resize(n);
int i,j,w;
while(!fin.eof())
{
fin>>i>>j>>w;
v[i].push_back(j);
v[j].push_back(i);
n--;
}
for (i = 0; i < v.size(); i++)
{
std::vector<int >::iterator it;
cout<<i<<"-> ";
for(it=v[i].begin();it<v[i].end();it++)
cout<<*it<<" ";
cout<<endl;
}
DFS(1,v,c);
}
input1.txt
15 0 2 6 1 2 5 2 3 1 1 4 6 2 7 7 4 7 4 3 6 8 4 5 12 5 8 4 8 7 2 8 11 7 11 6 4 5 9 11 9 10 8 10 11 2
No comments:
Post a Comment