洛谷P1160 队列安排(双向链表模拟)

https://www.luogu.org/problemnew/show/P1160

好久没打双链表模拟了。。意识模糊。tuifei

#include
using namespace std;
typedef long long ll;
const int N = 1e5+100;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
struct node{
    int l,r;
}a[N];
int n,m,k,p,d;
int main()
{
    cin >> n;
    a[0].l = a[1].r = -1,a[0].r = 1,a[1].l = 0;
    for(int i = 2;i <= n;i ++){
        a[i].l = a[i].r = -1;
        cin >> k >> p;
        if(p){
            //右插
            a[i].l = k;
            a[a[k].r].l = i;
            a[i].r = a[k].r;
            a[k].r = i;
        }else{
            //左插
            a[i].r = k;
            a[a[k].l].r = i;
            a[i].l = a[k].l;
            a[k].l = i;
        }
    }
    cin >> m;
    while(m --){
        cin >> d;
        a[a[d].l].r = a[d].r;
        a[a[d].r].l = a[d].l;
        a[d].l = -1;
        a[d].r = -1;
    }
    int t = a[0].r;
    while(1){
        cout << t <<" ";
        if(a[t].r == -1) break;
        t = a[t].r;
    }
    return 0;
}

 

你可能感兴趣的:(基本算法-模拟)