# 11029: 【原1029】整理书架

### 题目描述

author: yydcool 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1029

## Sample Input

3
1 2 3
4 5
6 7
3 1
2 1


## Sample Output

1 2 3 6 7 4 5


## FineArtz's solution

/* 整理书架 */
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main(){
int n;
cin >> n;
vector<vector<int>> book;
string ss;
getline(cin, ss);
for (int i = 1; i <= n ; ++i){
string st;
getline(cin, st);
istringstream iss(st);
int t;
vector<int> tv;
while (iss >> t)
tv.push_back(t);
book.push_back(tv);
}
for (int i = 1; i < n; ++i){
int x, y;
cin >> x >> y;
book[y - 1].insert(book[y - 1].end(), book[x - 1].begin(), book[x - 1].end());
book[x - 1].clear();
}
for (int i = n - 1; i >= 0; --i){
if (!book[i].empty()){
for (auto j : book[i]){
cout << j << ' ';
}
cout << endl;
return 0;
}
}
return 0;
}


## ligongzzz's solution

#include "iostream"
#include "cstdio"
#include "cstring"
using namespace std;

class node {
public:
int val = 0;
node* next = nullptr;
};

node* heads[109] = { nullptr };
node* rears[109] = { nullptr };

char ch[10009] = { 0 };

int main() {
int n;
scanf("%d", &n);

for (int i = 1; i <= n; ++i) {
rears[i] = heads[i] = new node;
}

cin.getline(ch, 10009);

for (int i = 1; i <= n; ++i) {
cin.getline(ch, 10000);
int length = strlen(ch);

int num = 0;

for (int j = 0; j < length; ++j) {
if (ch[j] == ' ') {
continue;
}
else if (ch[j] == '\n') {
break;
}
else {
num = num * 10 + ch[j] - '0';
if (ch[j + 1]<'0' || ch[j + 1]>'9') {
rears[i]->next = new node;
rears[i] = rears[i]->next;
rears[i]->val = num;
num = 0;
}
}
}
}

for (int i = 0; i < n - 1; ++i) {
int a, b;
scanf("%d %d", &a, &b);

rears[b] = rears[a];
}

for (int i = n; i > 0; --i) {
continue;
for (auto p = heads[i]->next; p; p = p->next)
cout << p->val << " ";
}

return 0;
}


## yyong119's solution

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n, i, h, t, indexx;
int main() {
queue<string> que[101];
string temp;
scanf("%d\n", &n);
for (i = 1; i <= n; i++) {
getline(cin, temp);
que[i].push(temp);
}
for (i = 1; i < n; i++) {
scanf("%d%d", &t, &h);
while (que[t].size()) {
que[h].push(que[t].front());
que[t].pop();
}
}
for (indexx = 1; indexx <= n; indexx++) if (que[indexx].size()) break;
while (que[indexx].size()) {
cout<<que[indexx].front()<<" ";
que[indexx].pop();
}
return 0;
}