Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream fin("arbore.in");
- ofstream fout("arbore.out");
- int n, k, muchii[101][101], vizitat[101], tati[101];
- void citire()
- {
- fin >> n >> k;
- for(int i = 1; i <= n - 1; i++)
- {
- int a, b;
- fin >> a >> b;
- muchii[a][b] = 1;
- muchii[b][a] = 1;
- }
- }
- void DFS(int nod, int sursa)
- {
- vizitat[nod] = 1;
- tati[nod] = sursa;
- for(int vecin = 1; vecin <= n; vecin++)
- {
- if(muchii[nod][vecin] == 1 && vizitat[vecin] == 0)
- DFS(vecin, nod);
- }
- }
- void afisare()
- {
- for(int i = 1; i <= n; i++)
- fout << tati[i] << " ";
- }
- int main()
- {
- citire();
- tati[k] = 0;
- DFS(k, 0);
- afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement