zoukankan      html  css  js  c++  java
  • Codeforces Round #279 (Div. 2) B

    B. Queue
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 
    Note

    The picture illustrates the queue for the first sample.

    再来看下B题。。开始想的太简单,结果提交过了4组数据。后来问了下别人(别怪我弱~~)。能明白是怎么回事了。 主要将给的数据分为奇数位和偶数位来排。。用next[]来存后继,pre[]存前驱。再用一个flag[]标记. 每次读入两个数 a,b 让next[a] = b,pre[b] = a ,flag [a] = flag[b] = 1;然后让next从0开始扫一遍就能得到处在偶数位置的数。每次访问后将flag[]置0。 接下来找奇数位置的数。扫一遍flag 直到遇到flag[i]的值为1的时候退出循环。然后tmp = i ,然后从tmp开始扫一遍pre找到第一个位置的数,然后就可以填满奇数位置的数了。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 #define M  200010
     8 const int N = 1000010;
     9 
    10 
    11 int main ()
    12 {
    13     int i;
    14     int next[N];
    15     int pre[N];
    16     int ans[M];
    17     int flag[N];
    18     int n;
    19     memset(next,-1,sizeof(next));
    20     memset(pre,-1,sizeof(pre));
    21     memset(ans,0,sizeof(ans));
    22     memset(flag,0,sizeof(flag));
    23     scanf("%d",&n);
    24     for(i=1;i<=n;i++){
    25         int a,b;
    26         scanf("%d%d",&a,&b);
    27         next[a] = b;
    28         pre[b] = a;
    29         flag[a] = 1;
    30         flag[b] = 1;
    31 
    32     }
    33     int tmp=0,k=2;
    34     while(1){
    35         ans[k] = next[tmp];
    36         flag[ans[k]] = 0;
    37         k += 2;
    38         tmp = next[tmp];
    39         if(tmp<=0) break;
    40     }
    41     int cnt;
    42     for(i=1;i<=1000000;i++){
    43         if(flag[i]){
    44             cnt = i;
    45             break;
    46         }
    47     }
    48     tmp = cnt;
    49 
    50     while(1){
    51         if(pre[tmp]<0) break;
    52         tmp = pre[tmp];
    53     }
    54     k = 1;
    55     while(1){
    56         ans[k] = tmp;
    57         k += 2;
    58         tmp = next[tmp];
    59         if(tmp<=0) break;
    60     }
    61     for(i=1;i<=n;i++){
    62         printf("%d ",ans[i]);
    63     }
    64 
    65 return 0;
    66 }
    View Code
    It is loneliness that make you different,not gregariousness
  • 相关阅读:
    微服务2.0时代,论其痛点与触点
    微服务架构的中国式落地
    【干货】微服务技术栈选型手册2.0
    每位开发者都该看:如何在四十岁后还能继续从事软件开发?
    在IBM学到的东西,到底对我的程序生涯产生了多大的影响
    十年程序员老兵告诉你,2018年程序员如何发展
    IntelliJ IDEA 快捷键大全
    List<Integer>.remove()的一个小细节
    eclipse 设置 @author @version等注释模板
    Android 获取当前应用的版本号和当前系统的版本号
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4120731.html
Copyright © 2011-2022 走看看