szabivan

PYRA

Oct 22nd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <deque>
  4. using namespace std;
  5.  
  6. vector< deque< int > > theGraph;
  7. int totalSize;
  8. int visited[10000];
  9.  
  10. int bfs( int node ){
  11.   //printf("Visiting node %d...\n",node);
  12.   int result = 1;
  13.   for( deque<int>::iterator it = theGraph[ node ].begin(); it != theGraph[ node ].end(); it++ ){
  14.     if( visited[*it] ) continue;
  15.     visited[*it] = 1;
  16.     result += bfs( *it );
  17.     result++;
  18.   }
  19.   totalSize += result;
  20.   //printf("Length of node %d is %d, total size is %d\n",node,result,totalSize);
  21.   return result;
  22. }
  23.  
  24. int main( int argc, char* argv[] ){
  25.   int t,n,i,j;
  26.   theGraph.resize( 10000 );  
  27.   scanf("%d",&t);
  28.   while( t-- ){
  29.     scanf("%d",&n);
  30.     for( int k = 0; k < n; k++ ) { theGraph[ k ].clear(); }
  31.     totalSize = 0;
  32.     visited[0] = 1;
  33.     for( int k = 1; k < n; k++ ){
  34.       visited[k] = 0;
  35.       scanf("%d %d",&i,&j);
  36.       theGraph[ i ].push_back( j );
  37.       theGraph[ j ].push_back( i );
  38.     }
  39.     bfs( 0 );
  40.     printf("%d\n", totalSize );
  41.   }
  42.   return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment