/*

本题的题意:

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

经过分析最后还是要先构造一个树,然后寻找离0最近的一个点,当结果是相等的级别的时候将结果返回最小的那个值

*/

参考代码:

 #include<stdio.h>
#include<string.h>
#define maxn 100010 int pre[maxn], dis[maxn];
//pre 数组代表当前骆驼的前一个骆驼, dis 数组代表当前骆驼到水井的距离
int main()
{
int n;
while(scanf("%d", &n) != EOF){
int x, y, i, ans, mind;
memset(dis, , sizeof(dis));
//父节点初始化 并查集必备
for(i = ; i < n; i++)
pre[i] = i;
for(i = ; i < n; i++){
scanf("%d %d", &x, &y);
pre[y] = x;
dis[x] = ;
}
for(mind = n + , ans = i = ; i <= n; i++){//注意:mind的初始值要大于n
//如果dis[i]为0 即该节点无子节点
if(!dis[i]){
x = i; //x 是一个临时变量
//如果这个节点有父节点 递归求出距离
while(pre[x] != x){
x = pre[x];
dis[i]++;
}
//找出最大距离所在的骆驼
if(!x && dis[i] < mind){
mind = dis[i];
ans = i;
}
}
}
printf("%d\n", ans);
}
return ;
}

csu1010: Water Drinking的更多相关文章

  1. CSUOJ Water Drinking

    Description The Happy Desert is full of sands. There is only a kind of animal called camel living on ...

  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. 寒假学干货之------LinearLayout.layout.weight

    所有原始代码由这个大神写的--http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864237.html layout/activity_mai ...

  2. Winform制作圆弧panel

    原理就是手动去画边框留出四个角 然后绘制四张圆弧的图片到panel上 public class ArcPanel : Panel { protected override void OnPaint(P ...

  3. hbase自带mapreduce计数表行数功能

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename’ mapreduce来计数,很快的!!!

  4. MYSQL导入中文数据乱码的四种解决办法

    方法一:通过增加参数 --default-character-set = utf8 解决乱码问题 方法一:通过增加参数 --default-character-set = utf8 解决乱码问题 my ...

  5. MITMF

    一. 安装 项目地址:https://github.com/byt3bl33d3r/MITMf 需要自行手动安装python-nfqueue,pefile模块 sudo apt-get install ...

  6. JS复习:第二十三章

    一.Cookie的构成: 1.名称:一个唯一确定cookie的名称.cookie名称不区分大小写,必须是经过URL编码的. 2.值:存储在cookie中的字符串值,必须被URL编码. 3.域:cook ...

  7. Java 编码 字符集

    Java 编码 字符集 @author ixenos 1.   字符集 a)    字符集建立了两字节Unicode码元序列与使用本地字符编码方式的字节序列之间的映射. b)    为了兼容其它命名, ...

  8. hdu 2993 MAX Average Problem(斜率DP入门题)

    题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训 ...

  9. Computation expressions and wrapper types

    原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-wrapper-types/ 在上一篇中,我们介绍了“maybe ...

  10. ubuntu 14.04 cagl

    libboost-atomic1.-dev libboost-atomic1.-dev libboost-chrono1.-dev libboost-dev libboost-program-opti ...