fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin>>n;
  7. vector<int> adjList[n];
  8. for(int i=0;i<n-1;i++){
  9. int x,y;
  10. cin>>x>>y;
  11. adjList[x].push_back(y);
  12. adjList[y].push_back(x);
  13. }
  14. for(int i=0;i<n;i++){
  15. if(adjList[i].size()<=2){
  16. cout<<i<<" ";
  17. }
  18. }
  19. return 0;
  20. }
Success #stdin #stdout 0s 5332KB
stdin
4
0 1
1 2
1 3
stdout
0 2 3