-
Notifications
You must be signed in to change notification settings - Fork 130
/
1-verticesandedges.cpp
86 lines (82 loc) · 1.77 KB
/
1-verticesandedges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include<bits/stdc++.h>
#include<vectors>
using namespace std;
pair<int,int> vecount(int **graph, int v, int sv, bool *visited){
if(v==0){
return make_pair(0, 0);
}
int vcount =0;
int ecount =0;
queue<int> q;
q.push(sv);
visited[sv] =true;
vcount++;
while(!q.empty()){
int fron = q.front();
q.pop();
visited[fron] = true;
for(int i=0;i<v;i++){
if(i== fron)
continue;
if(graph[fron][i] == 1 && !visited[i]){
ecount++;
vcount++;
visited[i] = true;
q.push(i);
}
}
}
pair<int,int> p;
p.first=vcount;
p.second=ecount;
return p;
}
pair<int,int> count(int **graph, int v){
bool *visited = new bool[v];
for(int i=0;i<v;i++){
visited[i] =0;
}
pair<int,int> P;
pair<int,int> ans;
ans.first=0;
ans.second=0;
for(int i=0;i<v;i++){
if(visited[i] == 0){
P=vecount(graph, v, i, visited);
ans.first += P.first;
ans.second += P.second;
}
}
return ans;
}
int main() {
int V, E;
cin >> V >> E;
int** graph=new int*[V];
for(int i=0;i<V;i++){
graph[i]=new int[V];
for(int j=0;j<V;j++){
graph[i][j]=0;
}
}
for(int i=0;i<E;i++){
int f,s;
cin>>f>>s;
graph[f][s]=1;
graph[s][f]=1;
}
pair<int,int> P ;
P.first=0;
P.second=0;
P= count(graph, V);
if(P.first%2 == 0){
cout<<"No. of vertices are even: "<<P.first<<endl;
}
else{
cout<<"No. of vertices are odd: "<<P.first<<endl;
}
cout<<"No. of edges: "<<P.second<<endl;
// your code goes here
return 0;
}