P1160队列安排

· · 题解

#include<iostream>
using namespace std;

const int N = 1e5 + 10;

struct node{
    int left;
    int right;
}list[N];

void push_val(int pos, int val, bool flag)
{
    if(flag)
    {
        list[list[pos].right].left = val;
        list[val].right = list[pos].right;
        list[pos].right = val;
        list[val].left = pos;
    }
    else
    {
        list[list[pos].left].right = val;
        list[val].left = list[pos].left;
        list[pos].left = val;
        list[val].right = pos;
    }
}

void remove(int i)
{
    if(list[i].left == -1) return;
    list[list[i].left].right = list[i].right;
    list[list[i].right].left = list[i].left;
    list[i].left = -1;
    list[i].right = -1;
}

int main()
{
    int n;
    cin >> n;
    list[0].right = 1;
    list[0].left = -1;
    list[1].right = -1;
    list[1].left = 0; 

    int k, p;                 
    for(int i = 2; i <= n; i++)
    {
        cin >> k >> p;
        push_val(k, i, p);
    }

    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> k;
        remove(k);
    }

    k = list[0].right;
    while(k != -1)
    {
        cout << k << " ";
        k = list[k].right;
    }

    return 0;
}