#include <iostream>
#include <vector>
using namespace std;
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void printGraph(vector<int> adj[], int V)
{
int a[100],i=0;
for (int v = 0; v < V; ++v)
{ i=0;
cout << "Adjacency list of vertex "
<< v<<endl;
for (auto x : adj[v])
{
a[i]=x;
i++;
}
for(int j=i-1;j>=0;j--)
cout << a[j]<<" -> ";
printf("\n");
}
}
int main()
{
int V;
vector<int> adj[100];
cin>>V;
for(int i=0;i<V;i++)
{
int n,m;
cin>>n>>m;
addEdge(adj, n, m);
}
printGraph(adj, V);
return 0;
}
#include <vector>
using namespace std;
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void printGraph(vector<int> adj[], int V)
{
int a[100],i=0;
for (int v = 0; v < V; ++v)
{ i=0;
cout << "Adjacency list of vertex "
<< v<<endl;
for (auto x : adj[v])
{
a[i]=x;
i++;
}
for(int j=i-1;j>=0;j--)
cout << a[j]<<" -> ";
printf("\n");
}
}
int main()
{
int V;
vector<int> adj[100];
cin>>V;
for(int i=0;i<V;i++)
{
int n,m;
cin>>n>>m;
addEdge(adj, n, m);
}
printGraph(adj, V);
return 0;
}
No comments:
Post a Comment