Advertisement
Josif_tepe

Untitled

Dec 12th, 2022
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <stack>
  5. using namespace std;
  6. const int maxk=1e5+10;
  7. int n, m;
  8. vector<int>graph[maxk];
  9. vector<int>art;
  10. int low_link[maxk];
  11. int dist[maxk];
  12. bool visited[maxk];
  13. int current_time=0;
  14. void dfs(int node, int parent=-1){
  15.     visited[node]=true;
  16.     low_link[node]=current_time;
  17.     dist[node]=current_time;
  18.     current_time++;
  19.     int children = 0;
  20.     for(int i=0; i<(int)graph[node].size(); i++){
  21.         int sosed=graph[node][i];
  22.         if(sosed!=parent){
  23.             if(visited[sosed]==true){
  24.                 low_link[node]=min(low_link[node], dist[sosed]);
  25.             }
  26.             else{
  27.                 dfs(sosed, node);
  28.                 low_link[node]=min(low_link[node], low_link[sosed]);
  29.                 if(parent!=-1 and low_link[sosed]>=dist[node]){
  30.                     art.push_back(node);
  31.                 }
  32.                 children++;
  33.             }
  34.         }
  35.     }
  36.     if(parent == -1 and children > 1) {
  37.         art.push_back(node);
  38.     }
  39. }
  40. void articulation_points(){
  41.     for(int i=0; i<maxk; i++){
  42.         visited[i]=false;
  43.         dist[i]=-1;
  44.           low_link[i]=-1;
  45.     }
  46. for(int i=0; i<n; i++){
  47.     if(visited[i]==false){
  48.     dfs(i);
  49.     }
  50. }
  51. }
  52. int main()
  53. {
  54.     while(true) {
  55.         cin>>n>>m;
  56.         if(n == 0 and m == 0) {
  57.             break;
  58.         }
  59.         for(int i=0; i<m; i++){
  60.             int a, b;
  61.             cin>>a>>b;
  62.             a--;
  63.             b--;
  64.             graph[a].push_back(b);
  65.             graph[b].push_back(a);
  66.         }
  67.         current_time=0;
  68.         articulation_points();
  69.         cout<<art.size() << endl;
  70.         for(int i = 0; i < maxk; i ++) {
  71.             graph[i].clear();
  72.         }
  73.         art.clear();
  74.     }
  75.     return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement