raghuvanshraj

Untitled

Sep 6th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. class graph_t {
  6.     int **adj_mat;
  7.     int num_vertices;
  8. public:
  9.     graph_t(int n);
  10.     ~graph_t();
  11.     void add_edge(int i, int j);
  12.     int get_even_vertices();
  13.     int get_odd_vertices();
  14.     int get_num_edges();
  15.     int get_num_vertices();
  16. };
  17.  
  18. graph_t::graph_t(int n) {
  19.     this->adj_mat = new int*[n]();
  20.     this->num_vertices = n;
  21.     for (int i = 0; i < n; ++i) {
  22.         this->adj_mat[i] = new int[n]();
  23.     }
  24. }
  25.  
  26. graph_t::~graph_t() {
  27.     for (int i = 0; i < this->num_vertices; ++i) {
  28.         delete [] this->adj_mat[i];
  29.     }
  30.  
  31.     delete [] this->adj_mat;
  32. }
  33.  
  34. void graph_t::add_edge(int i, int j) {
  35.     this->adj_mat[i-1][j-1] = 1;
  36.     this->adj_mat[j-1][i-1] = 1;
  37. }
  38.  
  39. int graph_t::get_even_vertices() {
  40.     int count = 0;
  41.     for (int i = 0; i < this->num_vertices; ++i) {
  42.         int sum = accumulate(this->adj_mat[i], this->adj_mat[i] + this->num_vertices, 0);
  43.         if (sum % 2 == 0) {
  44.             count++;
  45.         }
  46.     }
  47.  
  48.     return count;
  49. }
  50.  
  51. int graph_t::get_odd_vertices() {
  52.     int count = 0;
  53.     for (int i = 0; i < this->num_vertices; ++i) {
  54.         int sum = accumulate(this->adj_mat[i], this->adj_mat[i] + this->num_vertices, 0);
  55.         if (sum % 2 == 1) {
  56.             count++;
  57.         }
  58.     }
  59.  
  60.     return count;
  61. }
  62.  
  63. int graph_t::get_num_edges() {
  64.     int sum = 0;
  65.     for (int i = 0; i < this->num_vertices; ++i) {
  66.         sum += accumulate(this->adj_mat[i], this->adj_mat[i] + this->num_vertices, 0);
  67.     }
  68.  
  69.     return sum / 2;
  70. }
  71.  
  72. int graph_t::get_num_vertices() {
  73.     return this->num_vertices;
  74. }
  75.  
  76. int main() {
  77.     ios::sync_with_stdio(false);
  78.     cin.tie(0);
  79.  
  80.     int n, m;
  81.     cin >> n >> m;
  82.     graph_t graph(n);
  83.     while (m--) {
  84.         int i,j;
  85.         cin >> i >> j;
  86.         graph.add_edge(i,j);
  87.     }
  88.  
  89.     cout << graph.get_even_vertices() << endl << graph.get_odd_vertices() << endl << graph.get_num_edges() << endl << graph.get_num_vertices();
  90.  
  91.     return 0;
  92. }
Add Comment
Please, Sign In to add comment