- 这里链表上还有权值,因此vector存pii
- swap
- i += 2, j – ,且进来i直接加上,i + 1要判断;j也要判断
#include <iostream>
#include <vector>
#define endl '\n'
#define _(a) cout << #a << ": " << (a) << " "
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int h1, h2, n;
int v[N], ne[N];
int main() {
scanf("%d%d%d", &h1, &h2, &n);
while (n -- ) {
int addr, data, next;
scanf("%d%d%d", &addr, &data, &next);
v[addr] = data;
ne[addr] = next;
}
vector<PII> a, b;
for (int i = h1; ~i; i = ne[i]) a.push_back({i, v[i]});
for (int i = h2; ~i; i = ne[i]) b.push_back({i, v[i]});
if (a.size() < b.size()) swap(a, b);
vector<PII> c;
for (int i = 0, j = (int)b.size() - 1; i < a.size(); i += 2, -- j) {
c.push_back(a[i]);
if (i + 1 < a.size()) c.push_back(a[i + 1]);
if (j >= 0) c.push_back(b[j]);
}
for (int i = 0; i < c.size(); ++ i) {
printf("%05d %d ", c[i].first, c[i].second);
if (i == (int)c.size() - 1) cout << -1;
else printf("%05d\n", c[i + 1].first);
}
}