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
  • 相关阅读:
    mysql 分页查询的优化
    将某盘下所有文件名存在一个文件下面
    java 时间处理经典案例
    完整的发邮件并且生成测试报告的例子
    python 定时任务的执行
    打飞机游戏第一天,诸神归位
    数据库中插入几百万条数据
    面向对象的总结
    Python关于文件操作的总结
    python自动化,自动登录并且添加一个门店
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4120731.html
Copyright © 2011-2022 走看看