NonWhite

Articulation Points

Jul 26th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. #define FOR(i,A) for( typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
  9. #define TAM 110
  10.  
  11. using namespace std;
  12.  
  13. vector<int> g[ TAM ] ;
  14. map<string,int> m ;
  15. bool art[ TAM ] ;
  16. int low[ TAM ] , pre[ TAM ] ;
  17. int rt, root, timer ;
  18.  
  19. void init(){
  20.     m.clear() ;
  21.     memset( pre, -1 , sizeof pre ) ;
  22.     memset( art, 0 , sizeof art ) ;
  23.     for(int i = 0 ; i < TAM ; i++) g[ i ] .clear() ;
  24.     timer = 0 ;
  25. }
  26.  
  27. void dfs( int x, int p ){
  28.     pre[ x ] = low[ x ] = timer++ ;
  29.     for(int i = 0 ; i < g[ x ] .size()  ; i++){
  30.         int u = g[ x ][ i ] ;
  31.         if( u == p ) continue ;
  32.         if( pre[ u ] == -1 ){
  33.             dfs( u , x ) ;
  34.             if( x == rt ) root++ ;
  35.             if( pre[ x ] <= low[ u ] ) if( x != rt ) art[ x ] = true ;
  36.             low[ x ] = min( low[ x ] , low[ u ] ) ;
  37.         }else
  38.             low[ x ] = min( low[ x ], pre[ u ] ) ;
  39.     }
  40. }
  41.  
  42. int main(){
  43.  
  44.     int n,c,test = 1 ;
  45.     string s1, s2 ;
  46.     while( cin >> n && n ){
  47.         if( test > 1 ) cout << endl ;
  48.         init() ;
  49.         cin.ignore() ;
  50.         for(int i = 0 ; i < n ; i++){
  51.             getline( cin , s1 ) ;
  52.             m[ s1 ] = i ;
  53.         }
  54.         cin >> c ;
  55.         for(int i = 0 ; i < c ; i++){
  56.             cin >> s1 >> s2 ;
  57.             int a = m [ s1 ] ;
  58.             int b = m [ s2 ] ;
  59.             g[ a ] .push_back( b ) ;
  60.             g[ b ] .push_back( a ) ;
  61.         }
  62. //      for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < g[i].size() ; j++) cout << g[i][j] << ' ' ; cout << endl ; }
  63.         for(int i = 0 ; i < n ; i++){
  64.             if( pre[ i ] == -1 ){
  65.                 rt = i ;
  66.                 root = 0 ;
  67.                 dfs( i , -1 ) ;
  68.                 if( root > 1 ) art[ rt ] = true ;
  69.             }
  70.         }
  71.         int cont = 0 ;
  72.         for(int i = 0 ; i < n ; i++) if( art[i] ) cont++ ;
  73.         printf("City map #%d: %d camera(s) found\n", test++, cont );
  74.         set<string> resp ;
  75.         FOR(i,m) if( art[ i->second ] ) resp.insert( i->first ) ;
  76.         FOR(i , resp ) cout << *i << endl ;
  77.     }
  78.     return 0 ;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment