L2-002 链表去重

link

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 10;
int vis[N], next_[N];

struct Node {
    int now, val, ne;
}node[N];

void print(vector<Node> v) {
    for(int i = 0; i < v.size(); ++i) {
        printf("%05d %d", v[i].now, v[i].val);
        if(i + 1 < v.size()) printf(" %05d\n", v[i+1].now);
        else printf(" -1\n");
    }
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int head, num;
    cin >> head >> num;

    int a, b, c;
    while(num--) {
        cin >> a >> b >> c;
        node[a] = Node{a, b, c};
        next_[a] = c;
    }

    vector<Node> v1, v2;

    for(int i = head; i != -1; i = next_[i]) {
        if(!vis[abs(node[i].val)]) {
            vis[abs(node[i].val)] = true;
            v1.push_back(node[i]);
        }else v2.push_back(node[i]);
    }

    print(v1);
    print(v2);
    return 0;
}
posted @ 2025-02-24 09:29  awei040519  阅读(26)  评论(0)    收藏  举报