Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <vector>
- #include <map>
- #include <set>
- #define FOR(i,A) for( typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
- #define TAM 110
- using namespace std;
- vector<int> g[ TAM ] ;
- map<string,int> m ;
- bool art[ TAM ] ;
- int low[ TAM ] , pre[ TAM ] ;
- int rt, root, timer ;
- void init(){
- m.clear() ;
- memset( pre, -1 , sizeof pre ) ;
- memset( art, 0 , sizeof art ) ;
- for(int i = 0 ; i < TAM ; i++) g[ i ] .clear() ;
- timer = 0 ;
- }
- void dfs( int x, int p ){
- pre[ x ] = low[ x ] = timer++ ;
- for(int i = 0 ; i < g[ x ] .size() ; i++){
- int u = g[ x ][ i ] ;
- if( u == p ) continue ;
- if( pre[ u ] == -1 ){
- dfs( u , x ) ;
- if( x == rt ) root++ ;
- if( pre[ x ] <= low[ u ] ) if( x != rt ) art[ x ] = true ;
- low[ x ] = min( low[ x ] , low[ u ] ) ;
- }else
- low[ x ] = min( low[ x ], pre[ u ] ) ;
- }
- }
- int main(){
- int n,c,test = 1 ;
- string s1, s2 ;
- while( cin >> n && n ){
- if( test > 1 ) cout << endl ;
- init() ;
- cin.ignore() ;
- for(int i = 0 ; i < n ; i++){
- getline( cin , s1 ) ;
- m[ s1 ] = i ;
- }
- cin >> c ;
- for(int i = 0 ; i < c ; i++){
- cin >> s1 >> s2 ;
- int a = m [ s1 ] ;
- int b = m [ s2 ] ;
- g[ a ] .push_back( b ) ;
- g[ b ] .push_back( a ) ;
- }
- // for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < g[i].size() ; j++) cout << g[i][j] << ' ' ; cout << endl ; }
- for(int i = 0 ; i < n ; i++){
- if( pre[ i ] == -1 ){
- rt = i ;
- root = 0 ;
- dfs( i , -1 ) ;
- if( root > 1 ) art[ rt ] = true ;
- }
- }
- int cont = 0 ;
- for(int i = 0 ; i < n ; i++) if( art[i] ) cont++ ;
- printf("City map #%d: %d camera(s) found\n", test++, cont );
- set<string> resp ;
- FOR(i,m) if( art[ i->second ] ) resp.insert( i->first ) ;
- FOR(i , resp ) cout << *i << endl ;
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment