Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class graph_t {
- int **adj_mat;
- int num_vertices;
- public:
- graph_t(int n);
- ~graph_t();
- void add_edge(int i, int j);
- int get_even_vertices();
- int get_odd_vertices();
- int get_num_edges();
- int get_num_vertices();
- };
- graph_t::graph_t(int n) {
- this->adj_mat = new int*[n]();
- this->num_vertices = n;
- for (int i = 0; i < n; ++i) {
- this->adj_mat[i] = new int[n]();
- }
- }
- graph_t::~graph_t() {
- for (int i = 0; i < this->num_vertices; ++i) {
- delete [] this->adj_mat[i];
- }
- delete [] this->adj_mat;
- }
- void graph_t::add_edge(int i, int j) {
- this->adj_mat[i-1][j-1] = 1;
- this->adj_mat[j-1][i-1] = 1;
- }
- int graph_t::get_even_vertices() {
- int count = 0;
- for (int i = 0; i < this->num_vertices; ++i) {
- int sum = accumulate(this->adj_mat[i], this->adj_mat[i] + this->num_vertices, 0);
- if (sum % 2 == 0) {
- count++;
- }
- }
- return count;
- }
- int graph_t::get_odd_vertices() {
- int count = 0;
- for (int i = 0; i < this->num_vertices; ++i) {
- int sum = accumulate(this->adj_mat[i], this->adj_mat[i] + this->num_vertices, 0);
- if (sum % 2 == 1) {
- count++;
- }
- }
- return count;
- }
- int graph_t::get_num_edges() {
- int sum = 0;
- for (int i = 0; i < this->num_vertices; ++i) {
- sum += accumulate(this->adj_mat[i], this->adj_mat[i] + this->num_vertices, 0);
- }
- return sum / 2;
- }
- int graph_t::get_num_vertices() {
- return this->num_vertices;
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- graph_t graph(n);
- while (m--) {
- int i,j;
- cin >> i >> j;
- graph.add_edge(i,j);
- }
- cout << graph.get_even_vertices() << endl << graph.get_odd_vertices() << endl << graph.get_num_edges() << endl << graph.get_num_vertices();
- return 0;
- }
Add Comment
Please, Sign In to add comment