【Codeforces Round#279 Div.2】B. Queue
这题看别人的。就是那么诚实。http://www.cnblogs.com/zhyfzy/p/4117481.html
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.
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.
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.
4
92 31
0 7
31 0
7 141
92 7 31 141
The picture illustrates the queue for the first sample.

附代码(当然也是人家的。0。0)
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn = ;
int stu[maxn][], l[maxn], r[maxn];
int line[maxn];
int main()
{
//freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d%d", &l[i], &r[i]);
stu[r[i]][] = l[i];
stu[l[i]][] = r[i];
}
int j, i;
/*找出偶数位的人,并将确定位置的人的值设为-1*/
for(i = stu[][], j = ; i; j+=)
{
line[j] = i;
int tmp = stu[i][];
stu[i][] = -; stu[i][] = -;
i = tmp;
}
/*根据前前位为0找第一个奇数; 开始此处不理解,其实就是在定义的时候已经把第一个奇数的前前位设为0了,而第一个偶数的前前位经上面步骤变为-1,故不会引起冲突*/
for(int k = ; k < n; k++)
{
if(!stu[l[k]][]) {i = l[k]; break;}
}
/*找奇数*/
for(j = ; i; j+=)
{
line[j] = i;
i = stu[i][];
}
for(int l = ; l <= n; l++) {if(l != ) printf(" "); printf("%d", line[l]);}
printf("\n");
return ;
}
难道只能当一题党吗。
【Codeforces Round#279 Div.2】B. Queue的更多相关文章
- 【Codeforces Round #406 (Div. 2)】题解
The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...
- 【Codeforces Round #405 ( Div 2)】题解
Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...
- 【Codeforces Round #404 (Div. 2)】题解
A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<alg ...
- 【Codeforces Round #518 (Div. 2)】
A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...
- 【Codeforces Round #506 (Div. 3) 】
A:https://www.cnblogs.com/myx12345/p/9844334.html B:https://www.cnblogs.com/myx12345/p/9844368.html ...
- 【Codeforces Round #503 (Div. 2)】
A:https://www.cnblogs.com/myx12345/p/9843198.html B:https://www.cnblogs.com/myx12345/p/9843245.html ...
- 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)
传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...
- 【Codeforces Round #501 (Div. 3)】
A:https://www.cnblogs.com/myx12345/p/9842904.html B:https://www.cnblogs.com/myx12345/p/9842964.html ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- Python的数据类型的一些知识
int 类型表示(正负)整数,str 类型表示字符串(Unicode字符序列): 所表示的整数大小只受限于机器内存,而非固定的字节数. 字符串可以用双引号或是单引号封装,需要成对的出现. 传统上,py ...
- C# 多线程 方法,类的标记
[MethodImplAttribute(MethodImplOptions.Synchronized)] 此标记可用于方法,将一个方法标记为单线程进入,但在多个实例的时候仍有可能导致问题,比较有共享 ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 2013年信1204-1-2班小学期<程序设计技能训练>作品
int add(int x,int y) { return x+y; } 请写明题目,学号,姓名,班级,日期 程序要有详细的注释
- 50道经典的JAVA编程题(31-35)
50道经典的JAVA编程题(31-35),今天考完了java,在前篇博客里面贴出了题了,见:<今天考试的JAVA编程题>.考完了也轻松了,下个星期一还考微机原理呢,啥都不会,估计今天就做到 ...
- [iOS基础控件 - 5.3] 电台APP可滚动主界面(UIScrollView制作)
A.功能 1.上下可滚动的电台 2 x n 的图标集 2.顶部半透明标题 3.底部半透明功能按钮 B.实现思路 1.设置图标.启动画面 2.拖入UIScrollView,设置背景色 ( ...
- 转载 How to Encrypt connection string in web.config
转载原地址: https://chiragrdarji.wordpress.com/2008/08/11/how-to-encrypt-connection-string-in-webconfig/ ...
- 使用JavaScript实现分页效果
应公司需求,有时需要制作静态页面网站,而一旦涉及到文章的分页,实现起来非常麻烦,自己又刚入门,对js不是很熟悉,所以就拿来练练手. 首先页面结构如下,分别展示数据跟分页: <!DOCTYPE h ...
- CGAffineTransform相关函数
CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2); [xxx setTransform ...
- Java Web连接各种数据库方式汇总
1.Oracle数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String ...