VasilM

BFS_kontrolno

Dec 20th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAXN = 100;
  5. int used[MAXN+1],Q[MAXN],P[MAXN],G[MAXN][MAXN] = {0},N,M,b,e, area = 1, biggestArea = 0, areaInd;
  6.  
  7. void make_empty(){b=0;e=-1;}
  8.  
  9. void push(int x){Q[++e]=x;}
  10.  
  11. int pop(){return Q[b++];}
  12.  
  13. bool emptyQ(){return b>e;}
  14.  
  15. void BFS(int r) {
  16.   int  x,y,i;
  17.   make_empty();push(r);used[r]=area;P[r]=0;
  18.   int br = 1;
  19.   while(!emptyQ()) {  
  20.     x=pop();
  21.     for(i=1;i<=G[x][0];i++) {  
  22.       y=G[x][i];
  23.       if(!used[y]) {
  24.           push(y);
  25.           used[y]=area;
  26.           br++;
  27.           P[y]=x;
  28.       }
  29.     }
  30.   }
  31.   if( biggestArea < br ) {
  32.     biggestArea = br;
  33.     areaInd = area;
  34.     }
  35. }
  36.  
  37.  
  38. int main(){
  39.     for( int i=1;i<=N;i++) used[i]=0;
  40.     cin >> N >> M;
  41.     int x,y;
  42.     while( M-- ){
  43.         cin >> x >> y;
  44.         G[x][++G[x][0]] = y;
  45.         G[y][++G[y][0]] = x;
  46.     }
  47.  
  48.     for(int i=1;i<=N;i++) {
  49.         if(used[i]==0) BFS(i), area++;
  50.     }
  51.  
  52.     cout << biggestArea << endl;
  53.     for(int i=1; i<=N; i++ ) if ( used[i] == areaInd ) cout << i <<" ";
  54.     cout << endl;
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment