Codeforces Round #279 (Div. 2)B. Queue

针对学生午餐排队场景,设计了一种算法来恢复基于前后同学ID记录的排队序列。该算法适用于2到20万学生规模的队伍,通过两组序列处理,确保了即使在学生离开并返回后也能准确重建原始队列。
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.


胡搞题,分2个序列搞定


#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 200010;
queue<int>qu;
map<int, int>last;
map<int, int>next;
map<int, bool>vis;
struct node
{
	int a, b;
}p[N];

int odd[N], eve[N];

int main()
{
	int n;
	while (~scanf("%d", &n))
	{
		int l, r;
		while (!qu.empty())
		{
			qu.pop();
		}
		vis.clear();
		last.clear();
		next.clear();
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &p[i].a, &p[i].b);
			last[p[i].b] = p[i].a;
			next[p[i].a] = p[i].b;
			if (p[i].a == 0)
			{
				l = p[i].b;
			}
			if (p[i].b == 0)
			{
				r = p[i].a;
			}
		}
		// printf("%d %d\n", l, r);
		if (n == 2)
		{
			printf("%d %d\n", r, l);
			continue;
		}
		odd[1] = l;
		int cnt1 = 1, cnt2 = 1;
		vis[odd[cnt1]] = 1;
		while (next[odd[cnt1]] != 0)
		{
			odd[cnt1 + 1] = next[odd[cnt1]];
			cnt1++;
			vis[odd[cnt1]] = 1;
		}
		int tmp;
		for (int i = 1; i <= n; ++i)
		{
			if (!vis[p[i].a] && p[i].a)
			{
				tmp = p[i].a;
				// printf("%d\n", tmp);
				break;
			}
		}
		while (last[tmp] != 0)
		{
			tmp = last[tmp];
		}
		// printf("%d\n", tmp);
		eve[1] = tmp;
		while (next[eve[cnt2]] != 0)
		{
			eve[cnt2 + 1] = next[eve[cnt2]];
			cnt2++;
		}
		for (int i = 1; i <= min(cnt1, cnt2); ++i)
		{
			qu.push(eve[i]);
			qu.push(odd[i]);
			// printf("%d %d\n", eve[i], odd[i]);
		}
		if(cnt1 != cnt2)
		{
			if (cnt1 > cnt2)
			{
				for (int i = cnt2 + 1; i <= cnt1; ++i)
				{
					qu.push(odd[i]);
				}
			}
			else
			{
				for (int i = cnt1 + 1; i <= cnt2; ++i)
				{
					qu.push(eve[i]);
				}
			}
		}
		while(!qu.empty())
		{
			printf("%d", qu.front());
			qu.pop();
			if (!qu.empty())
			{
				printf(" ");
			}
			else
			{
				printf("\n");
				break;
			}
		}
	}
	return 0;
}


代码转载自:https://pan.quark.cn/s/8ce4326d996e 对于在 CentOS 7 系统中修改网卡配置文件后无法使设置生效的情况,经过实践验证,可以通过使用 nmcli 命令来进行调整。完成修改之后,需要重新启动虚拟机以使更改生效,这样操作流程即告完成。如果设置仍然无法生效,则表明虚拟机在启动过程中所获取的 IP 地址配置并非针对 eth0,此时可以对其它网卡的配置文件进行修改或将其移除。在 CentOS 7 系统中,网络配置的管理机制与早期版本存在差异,主要体现为采用了 Network Manager 服务来负责网络接口的管理。在某些情形下,尽管修改了 `/etc/sysconfig/network-scripts` 目录下的 `ifcfg-eth0` 文件,但网络配置却未能即时生效。此类问题的发生通常源于 CentOS 7 采用了不同于以往的配置读取方法。接下来将具体阐述如何借助 nmcli 命令来处理这一挑战。 以 root 用户身份登录系统并打开终端界面。nmcli 是 Network Manager 提供的命令行界面工具,它支持在命令行环境下执行网络连接的建立、编辑、查询及管理任务。针对修改 eth0 网卡配置的需求,可以遵循以下步骤进行操作: 1. 导航至 `/etc/sysconfig/network-scripts` 目录: ``` cd /etc/sysconfig/network-scripts ``` 2. 检查该目录内是否存在 `ifcfg-eth0.bak` 文件,该备份文件可能是先前调整配置时遗留下来的,若存在可能造成冲突。若发现该文件,可以选择将其删除: ``` [root@localhost netw...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值