fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N, M, A[101];
  5. vector<vector<int> > adjList;
  6. vector<bool> visited;
  7. vector<int> achievable;
  8.  
  9. void DFS(int node, int depth = 0) {
  10. for(int i = 0; i < depth; i++)
  11. cout << " ";
  12. cout << node+1 << endl;
  13. visited[node] = true;
  14. for(int i = 0; i < adjList[node].size(); i++)
  15. if(!visited[adjList[node][i]])
  16. DFS(adjList[node][i], depth+1);
  17. return;
  18. }
  19.  
  20. int main() {
  21. cin >> N >> M;
  22. adjList.resize(N);
  23. visited.resize(N, false);
  24. achievable.resize(M+1, 0);
  25. for(int i = 0; i < N; i++)
  26. cin >> A[i];
  27. for(int i = 0; i < N-1; i++) {
  28. int U, V;
  29. cin >> U >> V;
  30. U--; V--;
  31. adjList[U].push_back(V);
  32. adjList[V].push_back(U);
  33. }
  34. achievable[A[0]]++;
  35. DFS(0);
  36. return 0;
  37. }
  38. /*
  39. 5 10
  40. 5 7 2 3 2
  41. 1 2
  42. 2 3
  43. 3 4
  44. 4 5
  45.  
  46. idx 01234567890123456789
  47. 1 01000000000000000000
  48.  12 00110000000000000000
  49.   123 * 00011110000000000000
  50.   124 * 00001111000000000000
  51.  16 00000011000000000000
  52.   165 * 00000110000110000000
  53.  
  54. 1 2 3 4 5 6 7 8 9 10
  55. 1 1 2 2 3 4 2 0 0 0
  56. */
Success #stdin #stdout 0s 5320KB
stdin
6 10
1 2 3 4 5 6
1 2
2 3
2 4
1 6
6 5
stdout
1
 2
  3
  4
 6
  5