链表做法相同,还是map节省效率,vector输出
附本人AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<math.h>
using namespace std;
struct Node {
int add, val, next;
};
map<int, Node>Ma;
map<int, int>Mb;
void Pri(vector<Node>ans) {
for (int i = 0; i < ans.size(); i++) {
if (i == ans.size() - 1)printf("%05d %d -1\n", ans[i].add, ans[i].val);
else printf("%05d %d %05d\n", ans[i].add, ans[i].val, ans[i + 1].add);
}
}
int main() {
Node tmp;
int add, N;
scanf("%d%d", &add, &N);
for (int i = 0; i < N; i++) {
scanf("%d%d%d", &tmp.add, &tmp.val, &tmp.next);
Ma[tmp.add] = tmp;
}
vector<Node>ans, remove;
while (add != -1) {
Mb[abs(Ma[add].val)]++;
if (Mb[abs(Ma[add].val)] > 1)remove.push_back(Ma[add]);
else ans.push_back(Ma[add]);
add = Ma[add].next;
}
Pri(ans);
Pri(remove);
return 0;
}