SHARE
TWEET

Untitled

a guest Dec 8th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream fin("lant1.in");
  6. ofstream fout("lant1.out");
  7.  
  8. int n , p , q , r , a[105][105];
  9. int x[105], // coada pentru parcurgerea in latime
  10.     v[105], // vector caracteristic care precizeaza daca un varf a fost sau nu vizitat
  11.     t[105]; // vectorul de tati
  12. int sol[205], nsol;
  13.  
  14. void bfs(int p, int q)
  15. {
  16.     int st, dr;
  17.     st = dr = 1;
  18.     v[p] = 1;
  19.     x[1] = p;
  20.     t[p] = 0;
  21.     while(st <= dr)
  22.     {
  23.         int k = x[st];
  24.         for(int i = 1; i <= n ; ++i)
  25.             if(v[i] == 0 && a[k][i] == 1)
  26.             {
  27.                 dr ++;
  28.                 v[i] = 1;
  29.                 x[dr] = i;
  30.                 t[i] = k;
  31.             }
  32.         st ++;
  33.     }
  34. }
  35.  
  36. void reconstituire(int p)
  37. {
  38.     if(p)
  39.     {
  40.         reconstituire(t[p]);
  41.         sol[ ++nsol ] = p;
  42.     }
  43. }
  44.  
  45. void afis(int *x , int n)
  46. {
  47.     for(int i = 1; i <= n ;++i)
  48.         fout << x[i] << " ";
  49.     fout << endl;
  50. }
  51.  
  52. int main()
  53. {
  54.     int i , j;
  55.     fin >> n >> p >> q >> r;
  56.     while(fin >> i >> j)
  57.     {
  58.         a[i][j] = a[j][i] = 1;
  59.     }
  60.     for(int  i = 1 ; i <= n ; ++i)
  61.         t[i] = 0, v[i] = 0;
  62.     bfs(p , r);
  63.     reconstituire(t[r]);
  64.     for(int  i = 1 ; i <= n ; ++i)
  65.         t[i] = 0, v[i] = 0;
  66.     bfs(r , q);
  67.     reconstituire(q);
  68.     fout << nsol << "\n";
  69.     afis(sol, nsol);
  70.     return 0;
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top