洛谷:P1160 队列安排

#include<iostream>
#include<algorithm>
using namespace std;
int nownum=0,total=0;
class node{//使用链表
public:
    int num;
    int flag;
    node * next,*last;//需要保存下一个与上一个,来找到左与右的位置

    node(){flag=1;num=++nownum;next=last=NULL;}//初始化
    void show(){
        if(flag)//flag表示不需要输出,被移除的学生
            cout<<num;
        if(next!=NULL){
            if(flag)
                cout<<" ";//避免多输出空格
            next->show();//下一个
        }
    }
}list[100000];


int main(){
    int i,M,t1,t2;
    int head=0;//保存头部学生的序号
    cin>>total;
    for(i=1;i<total;i++){
        cin>>t1>>t2;
        if(t2){ //右       //根据左与右的排列分别将左与右的学生改变
            list[i].next=list[t1-1].next;
            if(list[t1-1].next!=NULL)
                list[t1-1].next->last=list+i;

            list[t1-1].next=list+i;
            list[i].last=list+t1-1;           
        }else{//左
            list[i].last=list[t1-1].last;
            if(list[t1-1].last!=NULL)
                list[t1-1].last->next=list+i;

            list[t1-1].last=list+i;
            list[i].next=list+t1-1;   

            if(t1-1==head){head=i;}//如果分配到头部的左侧,则成为头部
        }
    }
    cin>>M;
    for(i=0;i<M;i++){
        cin>>t1;
        list[t1-1].flag=0;//标记
    }
    list[head].show();cout<<endl;

    return 0;
}

地址:https://www.luogu.com.cn/problem/P1160

猜你喜欢

转载自www.cnblogs.com/forwhat00/p/13210259.html
今日推荐