Advertisement
hopingsteam

Untitled

Oct 31st, 2020
1,815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream fin("arbore.in");
  6. ofstream fout("arbore.out");
  7.  
  8. int n, k, muchii[101][101], vizitat[101], tati[101];
  9.  
  10. void citire()
  11. {
  12.     fin >> n >> k;
  13.     for(int i = 1; i <= n - 1; i++)
  14.     {
  15.         int a, b;
  16.         fin >> a >> b;
  17.         muchii[a][b] = 1;
  18.         muchii[b][a] = 1;
  19.  
  20.     }
  21. }
  22.  
  23. void DFS(int nod, int sursa)
  24. {
  25.     vizitat[nod] = 1;
  26.     tati[nod] = sursa;
  27.  
  28.     for(int vecin = 1; vecin <= n; vecin++)
  29.     {
  30.         if(muchii[nod][vecin] == 1 && vizitat[vecin] == 0)
  31.             DFS(vecin, nod);
  32.     }
  33. }
  34.  
  35. void afisare()
  36. {
  37.     for(int i = 1; i <= n; i++)
  38.         fout << tati[i] << " ";
  39. }
  40.  
  41. int main()
  42. {
  43.     citire();
  44.  
  45.     tati[k] = 0;
  46.     DFS(k, 0);
  47.  
  48.     afisare();
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement