Advertisement
thesonpb

Untitled

Aug 18th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. //Một Graph vô hướng được coi là liên thông theo 2 cạnh khi nó vẫn duy trì trạng thái liên thông khi một cạnh bất kỳ bị hủy. Kiểm tra //xem graph hiện tại có phải là graph dạng liên thông 2 cạnh hay không
  2.  
  3. //Đầu vào: Dòng đầu tiên gồm số n và m đại biểu cho số đỉnh và cạnh. m dòng tiếp theo, mỗi dòng chứa hai đỉnh mà cạnh đó nối
  4.  
  5. //Đầu ra: True hoặc False đại biểu cho graph có thỏa mãn điều kiên hay không.
  6.  
  7. #include <iostream>
  8. #include <vector>
  9. #include <unordered_map>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. // data structure to store graph edges
  14. struct Edge {
  15.     int src, dest;
  16. };
  17.  
  18. class Graph
  19. {
  20. public:
  21.     // construct a vector of vectors to represent an adjacency list
  22.     vector<vector<int>> adjList;
  23.  
  24.     // Graph Constructor
  25.     Graph(vector<Edge> const &edges, int N)
  26.     {
  27.         // resize the vector to N elements of type vector<int>
  28.         adjList.resize(N);
  29.  
  30.         // add edges to the undirected graph
  31.         for (auto &edge: edges)
  32.         {
  33.             adjList[edge.src].push_back(edge.dest);
  34.             adjList[edge.dest].push_back(edge.src);
  35.         }
  36.     }
  37. };
  38.  
  39. // Perform DFS on graph starting from vertex v and find
  40. // all Bridges in the process
  41. int DFS(Graph const &graph, int v, vector<bool> discovered,
  42.                 int arrival[], int parent, int &time, bool &econ)
  43. {
  44.     // set arrival time of vertex v
  45.     arrival[v] = ++time;
  46.  
  47.     // mark vertex as discovered
  48.     discovered[v] = true;
  49.  
  50.     // initialize arr to arrival time of vertex v
  51.     int arr = arrival[v];
  52.  
  53.     // (v, w) forms an edge
  54.     for (int w : graph.adjList[v])
  55.     {
  56.         // w is not discovered
  57.         if (!discovered[w]) {
  58.             arr = min(arr, DFS(graph, w, discovered,
  59.                             arrival, v, time, econ));
  60.  
  61.         // w is discovered and w is not parent of v
  62.         } else if (w != parent) {
  63.             // If the vertex w is already discovered, that
  64.             // means that there is a back edge starting
  65.             // from v. Note that as discovered[u] is already
  66.             // true, arrival[u] is already defined
  67.             arr = min(arr, arrival[w]);
  68.         }
  69.     }
  70.  
  71.     // if value of arr remains unchanged i.e. it is equal
  72.     // to the arrival time of vertex v and if v is not root
  73.     // node, then (parent[v] -> v) forms a Bridge
  74.     if (arr == arrival[v] && parent != -1)
  75.         //cout << parent << ", " << v << '\n';
  76.         econ=false;
  77.  
  78.     // we return the minimum arrival time
  79.     return arr;
  80. }
  81.  
  82. // 2-Edge Connectivity in a Graph
  83. int main()
  84. {
  85.     // Number of nodes in the graph
  86.     int N, M;
  87.     cin >> N >> M;
  88.     // vector of graph edges as per above diagram
  89.     vector<Edge> edges;
  90.     int s, d;
  91.     for(int i=0; i<M; i++) {
  92.         cin >> s >> d;
  93.         Edge e = {s, d};
  94.         edges.push_back(e);
  95.     }
  96.     // = { {0, 2}, {1, 2}, {2, 3}, {2, 4}, {3, 4}, {3, 5} }
  97.  
  98.  
  99.     // create a graph from edges
  100.     Graph graph(edges, N);
  101.  
  102.     // stores vertex is discovered or not
  103.     vector<bool> discovered(N);
  104.  
  105.     // stores arrival time of a node in DFS
  106.     int arrival[N];
  107.  
  108.     int start = 0, parent = -1, time = 0;
  109.  
  110.     bool econ = true;
  111.     // As given graph is connected, DFS will cover every node
  112.     DFS(graph, start, discovered, arrival, parent, time, econ);
  113.     //bool all_node_discovered = all_of(discovered.begin(), discovered.end(), [](bool v) { return v; });
  114.     //for(int i=0; i<N; i++) cout << discovered[i] << " ";
  115.     if(econ) {cout << "True";} else {cout << "False";}
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement