Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- queue<int>q;
- string color[10000]="white";
- int d[100000],p[100000];
- class Node{
- public:
- int destination;
- int weight;
- Node *next;
- };
- class Integer{
- public:
- Node *head = NULL;
- };
- void addEdge(Integer arr[],int sorc,int dest)
- {
- Node *newNode = new Node();
- newNode->destination = dest;
- newNode->next = NULL;
- if( arr[sorc].head == NULL)
- arr[sorc].head = newNode;
- else
- {
- Node *temp = arr[sorc].head;
- while(temp->next != NULL)
- temp = temp->next;
- temp->next = newNode;
- }
- }
- void bfs(Integer arr[],int s)
- {
- q.push(s);
- color[s] = "Gray";
- d[s] = 0;
- p[s] = 0;
- while( !q.empty())
- {
- int parent = q.front();
- q.pop();
- cout<<parent<<" ";
- Node *temp = arr[parent].head;
- while( temp!=NULL)
- {
- if( color[temp->destination] == "white")
- {
- q.push(temp->destination);
- color[temp->destination] = "Gray";
- d[temp->destination] = d[parent] + 1;
- p[temp->destination] = parent;
- }
- temp = temp->next;
- }
- color[parent] = "Black";
- }
- }
- //void display(Integer listt[], int v)
- //{
- // for(int i =0; i<=v; i++)
- // {
- // cout<<"\n Adjacency list of vertex "<<i<<" -->";
- // while(listt[i].head != NULL)
- // {
- // cout<<" "<<listt[i].head->destination;
- // listt[i].head = listt[i].head->next;
- // }
- // }
- //
- //}
- int main()
- {
- freopen("input.txt","r",stdin);
- int v;
- cin>>v;
- Integer arr[v];
- /// eita na korle arr er vitore v+1 dite hobe
- for (int i = 0; i <= v; i++)
- arr[i].head = NULL;
- /// getting input
- int m,n;
- while(scanf("%d%d",&m,&n) == 2)
- {
- addEdge(arr,m,n);
- addEdge(arr,n,m);
- }
- ///display(arr,v);
- bfs(arr,1);
- cout<<endl<<p[6];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement