#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int N = 1e6 + 10;
int ne[N], e[N], new1[N], e1[N], new2[N], e2[N];
bool book[N];
int main(int argc, char const *argv[]) {
int start, n;
cin >> start >> n;
while (n--) { //邻接表存储
int x, y, z;
cin >> x >> y >> z;
ne[x] = z;
e[x] = y;
}
int t = 0, k = 0;
for (int i = start; i != -1; i = ne[i]) {
if (book[abs(e[i])] == 0) {
book[abs(e[i])] = 1;
new1[t] = i;
e1[t] = e[i];
t++;
} else {
new2[k] = i;
e2[k] = e[i];
k++;
}
}
for (int i = 0; i < t; i++)
if (i == t - 1)//如果是最后一个元素
printf("%05d %d -1\n", new1[i], e1[i]);
else
printf("%05d %d %05d\n", new1[i], e1[i], new1[i + 1]);
for (int i = 0; i < k; i++)
if (i == k - 1)
printf("%05d %d -1\n", new2[i], e2[i]);
else
printf("%05d %d %05d\n", new2[i], e2[i], new2[i + 1]);
return 0;
}
L2-002 链表去重 (25 分)
最新推荐文章于 2024-03-08 17:07:13 发布