Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <cassert>
- #include <queue>
- using namespace std;
- class Graph {
- public:
- vector<vector<int>> data;
- Graph(const int x) : data(x, vector<int>(x, 0)) {};
- Graph(vector<vector<int>> x) : data(std::move(x)) {};
- friend ostream &operator<<(ostream &out, const Graph &x) {
- for (int i = 0; i < x.data.size(); ++i) {
- for (int j = 0; j < x.data.size(); ++j) {
- out << x.data[i][j] << ' ';
- }
- out << '\n';
- }
- return out;
- }
- void AddVertice() {
- for (auto &i : data) {
- i.push_back(0);
- }
- data.emplace_back(data.size() + 1, 0);
- }
- void DeleteVertice(const int &x) {
- assert(x < data.size());
- auto temp = vector<vector<int>>(data.size() - 1, vector<int>(data.size() - 1));
- int i1 = 0, j1 = 0;
- for (int i = 0; i < data.size() && i != x; ++i) {
- if (i != x) {
- for (int j = 0; j < data.size(); ++j) {
- if (j != x) {
- temp[i1][j1] = data[i][j];
- ++j1;
- }
- }
- ++i1;
- }
- }
- data = temp;
- }
- void AddEdge(const int &a, const int &b) {
- assert(a != b);
- data[a][b] = 1;
- data[b][a] = 1;
- }
- void DeleteEdge(const int &a, const int &b) {
- data[a][b] = 0;
- data[b][a] = 0;
- }
- int UsedVertices() {
- int count = 0;
- for (int i = 0; i < data.size(); ++i) {
- for (int j = 0; j < data.size(); ++j) {
- if (data[i][j] == 1)
- ++count;
- }
- }
- return count / 2;
- }
- bool IsCycled() {
- vector<bool> used(data.size(), false);
- return DfsCycle(used, 0, -1);
- }
- bool DfsCycle(vector<bool> &visited, int x, int p) {
- visited[x] = true;
- for (int i = 0; i < visited.size(); ++i) {
- if (data[x][i] == 1 && i != p) {
- if (visited[i] == true) {
- return true;
- }
- bool q = DfsCycle(visited, i, x);
- if (q == true) {
- return q;
- }
- }
- }
- return false;
- }
- vector<int> Conherence() {
- vector<int> g(data.size(), -1);
- int index = 0;
- for (int i = 0; i < data.size(); ++i) {
- if (g[i] == -1) {
- DfsConherence(g, i, -1, index);
- ++index;
- }
- }
- g.push_back(index);
- return g;
- }
- void DfsConherence(vector<int> &g, int a, int b, int index) {
- g[a] = index;
- for (int i = 0; i < g.size(); ++i) {
- if (data[a][i] == 1 && i != b) {
- DfsConherence(g, i, a, index);
- }
- }
- }
- bool IsTree() {
- bool q1 = IsCycled();
- vector<int> q2 = Conherence();
- return !IsCycled() && q2[q2.size() - 1] == 1;
- }
- // int Len(int a, int b) {
- // assert(a != b);
- // vector<int> l = Conherence();
- // assert(l[a] == l[b]);
- // vector<int> d (data.size(), 10000), p (data.size());
- // vector<bool> u (data.size());
- // d[a] = 0;
- // for (int i = 0; i < data.size(); ++i) {
- // int v = -1;
- // for (int j = 0; j < data.size(); ++j) {
- // if (!u[j] && (v == -1 || d[j] < d[v]))
- // v = j;
- // }
- // if (d[v] == 10000)
- // break;
- // u[v] = true;
- // for (int j = 0; j < data.size(); ++j) {
- // if (data[i][j] == 1) {
- // if (d[v] + 1 < d[j]){
- // d[j] = d[v] + 1;
- // p[j] = v;
- // }
- // }
- // }
- // }
- // return d[4];
- // }
- int Len(int a, int b) {
- assert(a != b);
- vector<int> l = Conherence();
- assert(l[a] == l[b]);
- queue<int> q;
- vector<int> d(data.size(), 1000);
- q.push(a);
- d[a] = 0;
- while (q.size() != 0) {
- int temp = q.front();
- q.pop();
- for (int i = 0; i < data.size(); ++i) {
- if (data[temp][i] == 1) {
- if (d[i] == 1000) {
- d[i] = d[temp] + 1;
- q.push(i);
- }
- }
- }
- }
- return d[b];
- }
- };
- //class Tree: public Graph {
- //
- // void AddTree(Tree &a, Graph &b, int na, int nb) {
- // assert(b.IsTree());
- //
- // }
- //};
- int main() {
- vector<vector<int>> a(5, vector<int>(5, 0));
- a[0][1] = 1; a[0][2] = 0; a[0][3] = 1; a[0][4] = 0;
- a[1][0] = 1; a[1][2] = 0; a[1][3] = 0; a[1][4] = 0;
- a[2][0] = 0; a[2][1] = 0; a[2][3] = 1; a[2][4] = 0;
- a[3][0] = 1; a[3][1] = 0; a[3][2] = 1; a[3][4] = 1;
- a[4][0] = 0; a[4][1] = 0; a[4][2] = 0; a[4][3] = 1;
- Graph s = Graph(a);
- vector<int> f = s.Conherence();
- cout << s.Len(2,4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement