fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. set<int> ans;
  4. vector< vector<int> > tree(10001);
  5. int mx = 0;
  6. void ldfs(int id, int ht, int p=-1){
  7. if(ht > mx){
  8. mx = ht;
  9. ans.insert(id);
  10. //cout<<id<<endl;
  11. }
  12. for(int i=0; i<tree[id].size(); ++i)
  13. if(tree[id][i] != p)
  14. ldfs(tree[id][i], ht+1, id);
  15. }
  16. void rdfs(int id, int ht, int p=-1){
  17. if(ht > mx){
  18. mx = ht;
  19. ans.insert(id);
  20. //cout<<id<<endl;
  21. }
  22. for(int i=tree[id].size()-1; i>=0; --i)
  23. if(tree[id][i] != p)
  24. rdfs(tree[id][i], ht+1, id);
  25. }
  26. int main(){
  27. #ifndef ONLINE_JUDGE
  28. freopen("inp.txt", "r", stdin);
  29. #endif // ONLINE_JUDGE
  30. int test;
  31. cin>>test;
  32. while(test--){
  33. int n;
  34. int x, y;
  35. cin >> n;
  36. assert(n<=100000 and n>=3);
  37. ans.clear();
  38. tree.clear();
  39. tree.resize(n+1);
  40. for(int i=1; i<=n-1; ++i){
  41. cin >> x >> y;
  42. //assert(x<=y and y<=n and x>=1);
  43. tree[x].push_back(y);
  44. tree[y].push_back(x);
  45. }
  46. /*
  47.   for(int i=1; i<=n; ++i){
  48.   for(auto it: tree[i])
  49.   cout<<it<<" ";
  50.   cout<<endl;
  51.   }
  52.   */
  53. mx = 0;
  54. ldfs(1, 1);
  55. mx = 0;
  56. rdfs(1, 1);
  57. for(auto it: ans)
  58. cout<<it<<" ";
  59. cout<<endl;
  60.  
  61. }
  62.  
  63. }
Runtime error #stdin #stdout #stderr 0s 3420KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: prog.cpp:36: int main(): Assertion `n<=100000 and n>=3' failed.