Advertisement
ismaeil

Intercept

Jul 14th, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef vector< ll > vl;
  6. typedef vector< bool > vb;
  7. typedef pair<ll , ll> pll;
  8. typedef vector< pll > vll;
  9.  
  10. #define PB push_back
  11. #define INF ll(4e18)
  12. #define S second
  13. #define F first
  14.  
  15. vector< vll > al ,al_t;
  16. vb articulationPoint;
  17. int n ,m ,s ,e ,cntD;
  18. vl dfs_num ,dfs_low;
  19. vector< vl > al2;
  20.  
  21. void init()
  22. {
  23.     al.assign(n ,vll());
  24.     al2.assign(n ,vl());
  25.     al_t.assign(n ,vll());
  26.    
  27.     cntD = 1;
  28.     dfs_num.assign(n ,0);
  29.     dfs_low.assign(n ,0);
  30.     articulationPoint.assign(n ,false);
  31. }
  32.  
  33. vl Dijkstra()
  34. {
  35.     vl dist(n ,INF);
  36.     dist[s] = 0;
  37.    
  38.     priority_queue< pll , vll , greater< pll > > pq;
  39.     pq.push( pll(0ll ,s) );
  40.    
  41.     while( !pq.empty() )
  42.     {
  43.         ll u = pq.top().S;
  44.         ll d = pq.top().F;
  45.         pq.pop();
  46.        
  47.         if( d > dist[u] ) continue;
  48.        
  49.         for(pll v : al[u])
  50.         {
  51.             if( dist[u] + v.S < dist[v.F] )
  52.             {
  53.                 dist[v.F] = dist[u] + v.S;
  54.                 pq.push( pll(dist[v.F] ,v.F) );
  55.             }
  56.         }
  57.     }
  58.    
  59.     return dist;
  60. }
  61.  
  62. void BFSreverseGraph(const vl &dist)
  63. {
  64.     vb vis(n ,false);
  65.     vis[e] = true;
  66.    
  67.     queue< int > q;
  68.     q.push(e);
  69.    
  70.     while( !q.empty() )
  71.     {
  72.         int u = q.front(); q.pop();
  73.        
  74.         for(pll v : al_t[u])
  75.         {
  76.             if( dist[v.F] + v.S == dist[u] )
  77.             {
  78.                 al2[u].PB(v.F);
  79.                 al2[v.F].PB(u);
  80.                
  81.                 if( !vis[v.F] )
  82.                 {
  83.                     vis[v.F] = true;
  84.                     q.push(v.F);
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. void getArticulationPoint(int u ,int p)
  92. {
  93.     dfs_num[u] = dfs_low[u] = cntD++;
  94.    
  95.     for(int v : al2[u])
  96.     {
  97.         if( !dfs_num[v] )
  98.         {
  99.             getArticulationPoint(v ,u);
  100.            
  101.             if( dfs_low[v] >= dfs_num[u] ) articulationPoint[u] = true;
  102.            
  103.             dfs_low[u] = min(dfs_low[u] ,dfs_low[v]);
  104.         }
  105.         else dfs_low[u] = min(dfs_low[u] ,dfs_num[v]);
  106.     }
  107. }
  108.  
  109. int main()
  110. {
  111.     scanf("%d%d" ,&n ,&m);
  112.    
  113.     init();
  114.     for(int i = 0 ; i < m ; i++)
  115.     {
  116.         int u; scanf("%d" ,&u);
  117.         int v; scanf("%d" ,&v);
  118.         ll w; scanf("%lld",&w);
  119.        
  120.         al  [u].PB( pll(v ,w) );
  121.         al_t[v].PB( pll(u ,w) );
  122.     }
  123.     scanf("%d%d" ,&s ,&e);
  124.    
  125.     vl dist = Dijkstra();
  126.     BFSreverseGraph(dist);
  127.  
  128.     getArticulationPoint(s ,s);
  129.     articulationPoint[s] = true;
  130.     articulationPoint[e] = true;
  131.    
  132.     for(int i = 0 ; i < n ; i++)
  133.         if( articulationPoint[i] )
  134.             printf("%d " ,i);
  135.    
  136.     return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement