#include #include #include #include #include int tree[100]; vector< int > childs[100]; bool parse( int parent, istrstream &stream ) { stream >> ws; if( stream.peek() != '(' ) return false; char c; stream >> c; int node; stream >> node; tree[node] = 1; if( parent >= 0 ) childs[parent].push_back( node ); if( parent >= 0 ) childs[node].push_back( parent ); while( parse( node, stream ) ); stream >> c; assert( c == ')' ); return true; } int main() { ifstream file( "code.in" ); while( 1 ) { for( int i = 0; i < 100; i++ ) { tree[i] = -1; childs[i].clear(); } string line; if( !getline( file, line ) ) break; istrstream stream( line.c_str() ); parse( -1, stream ); int flag = false; while( 1 ) { int i = 0; for( i = 0; i < 100; i++ ) if( tree[i] >= 0 && childs[i].size() == 1 ) break; if( i >= 80 ) break; int parent = childs[i][0]; childs[i].clear(); tree[i] = -1; int k; for( k = 0; k < childs[parent].size(); k++ ) if( childs[parent][k] == i ) { childs[parent][k] = childs[parent].back(); childs[parent].pop_back(); k = -1; break; } assert( k == -1 ); if( flag ) cout << " "; cout << parent; flag = true; } cout << endl; } return 0; }