Description

The Happy Desert is full of sands. There is only a kind of animal called camel living on the Happy Desert. Cause they live here, they need water here. Fortunately, they find a pond which is full of water in the east corner of the desert. Though small, but enough. However, they still need to stand in lines to drink the water in the pond.

Now we mark the pond with number 0, and each of the camels with a specific number, starting from 1. And we use a pair of number to show the two adjacent camels, in which the former number is closer to the pond. There may be more than one lines starting from the pond and ending in a certain camel, but each line is always straight and has no forks.

Input

There’re multiple test cases. In each case, there is a number N (1≤N≤100000) followed by N lines of number pairs presenting the proximate two camels. There are 99999 camels at most.

Output

For each test case, output the camel’s number who is standing in the last position of its line but is closest to the pond. If there are more than one answer, output the one with the minimal number.

Sample Input

1
0 1
5
0 2
0 1
1 4
2 3
3 5
5
1 3
0 2
0 1
0 4
4 5

Sample Output

1
4
2

Hint

思路:两个数组,一个指向头,另一个维护后面数字的指向

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAXN 100000
int nex[MAXN],h[MAXN];
int main()
{
int t;
int n, m;
while (~scanf("%d", &t))
{
int cnt = 0;
int flag = 1;
memset(nex, -1, sizeof(nex));
memset(h, 0, sizeof(h));
for (int i = 0; i < t; i++)
{
scanf("%d%d", &n, &m);
if (n == 0)
h[cnt++] = m;//当n为0时表示出现另一队,头+1
else nex[n] = m;//否则n的后面为m
}
int ans = t;
while (flag)
{
for (int i = 0; i < cnt; i++)//每次每队往后移一位
{
if (nex[h[i]] == -1)
{
ans = min(ans, h[i]);//当下一个为-1时表示这队已经到末尾了
flag = 0;
}
h[i] = nex[h[i]];//移动头指针
}
}
printf("%d\n", ans);
}
return 0;
}
/**********************************************************************
Problem: 1010
User: leo6033
Language: C++
Result: AC
Time:52 ms
Memory:2804 kb
**********************************************************************/

CSUOJ Water Drinking的更多相关文章

  1. csu1010: Water Drinking

    /* 本题的题意: 沙漠中有很多骆驼和一个池塘,0表示池塘,1-N表示骆驼,输入的两个数表示两只骆驼,其中前面的那一头靠近池塘,所有的骆驼队列不交叉不相连,求站在队尾但是离水井最近的骆驼编号 经过分析 ...

  2. Taking water into exams could boost grades 考试带瓶水可以提高成绩?

    Takeing a bottle of water into the exam hall could help students boost their grades, researchers cla ...

  3. Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)

    #1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...

  4. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  5. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  7. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  9. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

随机推荐

  1. 【洛谷P1991】无线通讯网

    题目大意:给定一个 N 个顶点的完全图,边有边权,现在要求使得图中所有顶点联通的情况下,第 M-1 大的边最小值是多少. 题解:所有点联通的最小要求是所有点和连接这些点的边构成原图的一棵生成树,那么问 ...

  2. Css设置img属性让图片水平居中/居左/居右的写法

    图片的居中显示css有很多方法,但在很多情况下有的方法无效,无意发现这个系统的官方处理图片居中,居左,居右的css写法,喜欢的朋友可以收藏下哦 图片的居中显示css有很多方法,但在很多情况下有的方法无 ...

  3. 变量&常量

    变量:variables 存储数据,以被后面的程序调用,可以看作是:装信息的容器: 变量的作用:(1)标记数据(2)存储数据 变量定义规范1.声明变量:定义变量   name = "Mr H ...

  4. OpenStack 计算服务 Nova介绍和控制节点部署(七)

    介绍 Nova是openstack最早的两块模块之一,另一个是对象存储swift.在openstack体系中一个叫做计算节点,一个叫做控制节点.这个主要和nova相关,我们把安装为计算节点nova-c ...

  5. 转:Xcode打印堆栈信息

    2#   分享于 14-11-26 19:15:36 Chrome 39.0.2171.71 Mac OS X 10.10.1 如果只是看调用栈的话,可以使用 lldb 的功能.在你的代码里面打上一个 ...

  6. C语言复习---选择法排序

    选择排序也是一种简单直观的排序算法 它的工作原理很容易理解:初始时在序列中找到最小(大)元素,放到序列的起始位置作为已排序序列:然后,再从剩余未排序元素中继续寻找最小(大)元素,放到已排序序列的末尾. ...

  7. vim文本删除方法 Linux

    1,先打开某个文件: vim   filename 2,转到文件结尾 在命令模式输入 G 3,转到10行 在命令模式输入 10G 4,删除所有内容:先用G 转到文件尾,然后使用下面命令: :1, .d ...

  8. UVALive - 4636 Cubist Artwork(贪心)

    题目链接 题意 给出正视图和侧视图,判断最少用几个立方体 分析 若存在高度相同的立方块,则以数目多的那面为准. #include <iostream> #include <cstdi ...

  9. bzoj千题计划263:bzoj4870: [六省联考2017]组合数问题

    http://www.lydsy.com/JudgeOnline/problem.php?id=4870 80分暴力打的好爽 \(^o^)/~ 预处理杨辉三角 令m=n*k 要求满足m&x== ...

  10. 流媒体技术学习笔记之(七)进阶教程OBS参数与清晰度流畅度的关系

    源码地址:https://github.com/Tinywan/PHP_Experience 很多主播问过OBS的参数到底什么影响画质,到底什么影响流畅度,那么本篇教程尽量用通俗的语言解释下一些重要参 ...