L2-1 連結串列去重 (25分)
阿新 • • 發佈:2020-11-24
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cstring> #include<string> #include<map> #include<queue> #include<iomanip> using namespace std; #define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout); constint N = 1e7 + 10; int head, ne[N],v[N]; int n; bool st[N]; int main() { // STDIN cin >> head >> n; memset(ne, -1, sizeof ne); for (int i = 1; i <= n; i++) { int id, val, nid; cin >> id >> val >> nid; v[id] = val; ne[id]= nid; } int now = head; // printf("%05d %d %5d\n", head, v[head], ne[head]); vector<pair<int, int > > vec1; vector<pair<int, int> > vec2; vec1.push_back({head, v[head]}); st[abs(v[head])] = true; while (ne[now] != -1) { now = ne[now];if (!st[abs(v[now])]) vec1.push_back({now, v[now]}), st[abs(v[now])] = true; else vec2.push_back({now, v[now]}); } int len = vec1.size(); // cout << vec1.size() << endl; for (int i = 0; i < len; i++) { if (i != len-1) printf("%05d %d %05d\n", vec1[i].first, vec1[i].second, vec1[i+1].first); else printf("%05d %d %d\n", vec1[i].first, vec1[i].second, -1); } len = vec2.size(); for(int i = 0; i < len; i++) { if (i!= len-1) printf("%05d %d %05d\n", vec2[i].first, vec2[i].second, vec2[i+1].first); else printf("%05d %d %d\n", vec2[i].first, vec2[i].second, -1); } }