WHCSRL 技术网

PAT甲级 1074(C++)_aoi

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. #define maxn 100000
  5. struct node {
  6. int address;
  7. int next;
  8. int data;
  9. };
  10. node info[maxn];
  11. int main() {
  12. int root, N, K;
  13. cin >> root >> N >> K;
  14. for (int i = 1; i <= N; i++) {
  15. int a1, a2, d;
  16. scanf("%%05d %%d %%05d", &a1, &d, &a2);
  17. info[a1].next = a2;
  18. info[a1].data = d;
  19. info[a1].address = a1;
  20. }
  21. vector<node>temp;
  22. vector<node>result;
  23. int count = 0;
  24. while (root != -1) {
  25. temp.push_back(info[root]);
  26. count++;
  27. root = info[root].next;
  28. if (count == K) {
  29. count = 0;
  30. for (int i = 0; i < K; i++)
  31. result.push_back(temp[K - 1 - i]);
  32. temp.clear();
  33. }
  34. }
  35. if (temp.size() != 0) {
  36. result.insert(result.end(), temp.begin(), temp.end());
  37. }
  38. for (int i = 0; i < result.size(); i++) {
  39. printf("%%05d %%d ", result[i].address, result[i].data);
  40. if (i != result.size() - 1)
  41. printf("%%05d ", result[i + 1].address);
  42. else
  43. printf("%%d ", -1);
  44. }
  45. return 0;
  46. }