Sunday, February 25, 2018

C program : (Graphs) To output the list of vertices which are connected n by an edge

 You will be given a vertex number n. You have to output
the list of vertices which are connected n by an edge, in the order in
which the edges were input.

Input

You are given the following.

1. The first line contains an integer, E, between 1 and 1000

2. This is followed by E lines, where each containing a pair of
numbers i and j where i and j are both non-negative integers <=
34,000. No edge will be listed more than once.

3. The last line contains a non-negative integer n <= 34,000. n is
assured to be a vertex listed in one of the E lines in part (2).

Output

You have to output the list of nodes to which n has an edge, in the
order in which the edges were input, one line for each vertex.


#include<stdio.h>
#include<stdlib.h>

struct edges
{
     int vertex1;
    int vertex2;
};
void main()
{
  int E,n;
  struct edges e[1000];
  scanf("%d",&E);
  for(int i=0;i<E;i++)
  {
       scanf("%d %d",&e[i].vertex1, &e[i].vertex2);
  }
  scanf("%d",&n);
  for(int i=0;i<E;i++)
  {
       if(e[i].vertex1==n)
    { printf("%d\n",e[i].vertex2);}
    if(e[i].vertex2==n)
    { printf("%d\n",e[i].vertex1);}
  }
 
 }

Thanks
Happy Computing !

No comments:

Post a Comment

Sacred Thought

28 April 2024 Today I am going to explain verse 31 - 38 chapter two for you all. There is no opportunity better than a righteous war (सत्य औ...