链表题全是送分题,大家掌握好基础。
#include<bits/stdc++.h>
using namespace std;
struct node {
int val, next;
}Node[100000];
bool vis[10100];
vector<int> ans1, ans2;
void prt (vector<int> v) {
for (int i = 0; i < v.size(); i++) {
if (i != v.size() - 1) {
printf ("%05d %d %05d\n", v[i], Node[v[i]].val, v[i + 1]);
}
else printf ("%05d %d -1\n", v[i], Node[v[i]].val);
}
}
int main() {
int n, fad, val, ad, next;
scanf ("%d %d", &fad, &n);
for (int i = 0; i < n; i++) {
scanf ("%d %d %d", &ad, &val, &next);
Node[ad].val = val;
Node[ad].next = next;
}
int fAd = fad;
while (fAd != - 1) {
if (vis[abs(Node[fAd].val)] == false) {
ans1.push_back(fAd);
vis[abs(Node[fAd].val)] = true;
}
else {
ans2.push_back(fAd);
}
fAd = Node[fAd].next;
}
prt(ans1);
prt(ans2);
}
|