Fork me on GitHub

P1160 队列安排(链表)

https://www.luogu.org/problem/show?pid=1160#sub
模拟,链表实现即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
bool f[100009];
int to[100009],n,fm[100009],m;
int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        int k,p;
        scanf("%d%d",&k,&p);
        if(p==0)
        {
            to[i]=k;
            to[fm[k]]=i;
            fm[i]=fm[k];
            fm[k]=i;
        }
        if(p==1)
        {
            to[i]=to[k];
            fm[i]=k;
            fm[to[k]]=i;
            to[k]=i;
        }
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int x;
        scanf("%d",&x);
        if(!f[x])
        {
            to[fm[x]]=to[x];
            fm[to[x]]=fm[x];
            f[x]=true;
        }
    }
    int k;
    for(int i=1;i<=n;i++)
    if(fm[i]==0) k=i;

    printf("%d ",k);
    while(to[k])
    {
        k=to[k];
        printf("%d ",k);
    }
    return 0;

}
posted @ 2017-04-09 11:11  primes  阅读(174)  评论(0编辑  收藏  举报