• B. Queue


    During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.

    Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).

    After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.

    Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.

    Input

    The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue.

    Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.

    The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.

    Output

    Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.

    Sample test(s)
    input
    4
    92 31
    0 7
    31 0
    7 141
    
    output
    92 7 31 141 
    
    这道题错了很久,不是wa,re就是te,最后发现是数组开小了。。。这题先记录每个数的前面和后面分别是谁,分两种情况讨论,一种总数是偶数,那么只要找到0,然后从0向前循环一遍,向后循环一遍就行了,如果是奇数,那么除了0向后循环一遍外,还要找到除0外的最后一个数,然后再向前循环一遍。
    #include<stdio.h>
    #include<string.h>
    #define count 1000005
    int next[count],pre[count],c[count],vis[count],a[count],b[count];
    int main()
    {
    int n,m,i,j,t,u;
    while(scanf("%d",&n)!=EOF)
    {
    memset(next,-1,sizeof(next));memset(pre,-1,sizeof(pre));memset(c,0,sizeof(c));
    memset(vis,0,sizeof(vis));memset(a,0,sizeof(a));memset(b,0,sizeof(b));
    if(n==2){
    for(i=1;i<=n;i++){
    scanf("%d%d",&a[i],&b[i]);
    if(a[i]==0){
    c[1]=b[i];
    }
    else if(b[i]==0){
    c[0]=a[i];
    }
    }
       printf("%d %d ",c[0],c[1]);
    continue;
    }
    for(i=1;i<=n;i++){
    scanf("%d%d",&a[i],&b[i]);
    next[a[i]]=b[i];
    pre[b[i]]=a[i];
    }
    if(n%2==0){
     t=1;
     for(i=next[0];i!=-1 && t<n;i=next[i]){
    c[t]=i;t=t+2;
     }
     t=n-2;
     for(i=pre[0];i!=-1 && t>=0;i=pre[i]){
    c[t]=i;t=t-2;
     }
    }

    if(n%2!=0){
     t=1;
     for(i=next[0];i!=-1 && t<n;i=next[i]){
    c[t]=i;t=t+2;vis[i]=1;
     }
     u=-1;
     for(i=1;i<=n;i++){
      if(vis[a[i]]==0 && a[i]!=0){
      u=a[i];break;
    }
       }
       while(next[u]!=-1){
        u=next[u];
       }
       t=n-1;
       for(i=u;i!=-1 && t>=0;i=pre[i]){
      c[t]=i;t=t-2;
      }
     
    }


    for(i=0;i<n;i++){
    if(i!=n-1){
    printf("%d ",c[i]);
    }
    else printf("%d ",c[i]);
    }
    }
    return 0;
    }

  • 相关阅读:
    拟真世界精密驾驶挑战 《微软飞行模拟》新截图放出
    抖音“统治”美国年轻人
    Google Play商店为预注册的游戏和应用提供自动安装功能
    小米Note 10 Lite海外发布 无缘中国市场
    亚马逊允许数万名员工在家工作直到10月2日
    刷新 翻看 我 关注 实时 疫情 何时彻底“解封”?王辰院士:北京防疫未到松懈时
    “胡汉三”饰演者刘江今晨去世,享年95岁
    虎牙年报披露2019年扭亏为盈,腾讯操持下与斗鱼合并倒计时?
    微软宣布一批新获得Microsoft Teams认证的会议硬件
    美版健康码要来了!苹果Google被网友质疑:这是变相的监视系统吗?
  • 原文地址:https://www.cnblogs.com/herumw/p/9464844.html
Copyright © 2020-2023  润新知