File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
C++/Data-Structures/graph Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+
5+ int main () {
6+ int n;// number of vertices
7+ int m;// number of edges
8+ cin>>n>>m;
9+ int i;
10+ vector<int > v[n+1 ];
11+ for (i=0 ;i<m;i++)
12+ {
13+ int x,y;
14+ cin>>x>>y;// input the vertices which are connected
15+ v[x].push_back (y); // put the vertex y in the list of x
16+ v[y].push_back (x); // put the vertex x in the list of y
17+ }
18+ int q;
19+ cin>>q;// queries to check whether a edge exist or not
20+ while (q--)
21+ {
22+ int x,y;
23+ cin>>x>>y;// check whether edge between x or y exist or not
24+ int flg=-1 ;
25+ for (i=0 ;i<v[x].size ();i++)
26+ {if (v[x][i]==y)
27+ {
28+ flg=1 ;break ;
29+ }
30+
31+ }
32+ if (flg==1 )
33+ cout<<" Edge exist\n " ;
34+ else
35+ cout<<" Edge does not exist\n " ;
36+ }
37+ return 0 ;
38+ }
You can’t perform that action at this time.
0 commit comments