/*

本题的题意:

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

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

*/

参考代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define maxn 100010
  4.  
  5. int pre[maxn], dis[maxn];
  6. //pre 数组代表当前骆驼的前一个骆驼, dis 数组代表当前骆驼到水井的距离
  7. int main()
  8. {
  9. int n;
  10. while(scanf("%d", &n) != EOF){
  11. int x, y, i, ans, mind;
  12. memset(dis, , sizeof(dis));
  13. //父节点初始化 并查集必备
  14. for(i = ; i < n; i++)
  15. pre[i] = i;
  16. for(i = ; i < n; i++){
  17. scanf("%d %d", &x, &y);
  18. pre[y] = x;
  19. dis[x] = ;
  20. }
  21. for(mind = n + , ans = i = ; i <= n; i++){//注意:mind的初始值要大于n
  22. //如果dis[i]为0 即该节点无子节点
  23. if(!dis[i]){
  24. x = i; //x 是一个临时变量
  25. //如果这个节点有父节点 递归求出距离
  26. while(pre[x] != x){
  27. x = pre[x];
  28. dis[i]++;
  29. }
  30. //找出最大距离所在的骆驼
  31. if(!x && dis[i] < mind){
  32. mind = dis[i];
  33. ans = i;
  34. }
  35. }
  36. }
  37. printf("%d\n", ans);
  38. }
  39. return ;
  40. }

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. Android编程获取手机的IMEI

    手机在生产时,每部手机均有一个唯一的标识(ID),国际上采用国际移动设备身份码(IMEI, International Mobile Equipment Identity).IMEI是由15位数字组成 ...

  2. vs2008编译FileZilla服务端源码

    vs2008编译FileZilla服务端源码 FileZilla服务端下载地址:https://download.filezilla-project.org/server/.FileZilla服务端源 ...

  3. TortoiseGit 错误信息Aborting commit due to empty commit message.解决

    错误信息: Aborting commit due to empty commit message. git不能完全退出(退出码 1) (47 ms @ 2016/2/19 14:03:24) 解决办 ...

  4. Jmeter:相应断言介绍

    Jmeter进行性能测试时,作为对上一个请求返回信息的校验,基本上断言是不可少的,今天主要介绍一下Jmeter的相应断言校验. 相应断言:即对服务器相应信息的校验判断,发送http请求后,对服务器返回 ...

  5. Linux系统编程初探系列之一:文件编程

    系统函数 int creat(const char* filename,mode_t mode) filename:需要创建的文件名(包含路径,缺省为当前路径) mode:创建模式 常见的创建模式有: ...

  6. Ubuntu下NFS,TFTP服务搭建

    环境:Ubuntu 一. 搭建NFS服务器 (1)安装: sudo apt-get install nfs-kernel-server   #安装NFS服务器端 sudo apt-get instal ...

  7. 遍历(一)jquery $().each和$.each()

    原文:http://www.frontopen.com/1394.html 在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法. $().each 在dom处理上 ...

  8. hdu_5286_wyh2000 and sequence(分块)

    题目链接:hdu_5286_wyh2000 and sequence 题意: 给一段长度为N的序列,每次询问l-r(l和r和上一次询问的答案有关)内 不同的数的 出现次数的次方 的和.强制在线 题解: ...

  9. 3、Data对象

    1.创建part1.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  10. vconfig 的使用

    http://man.cx/vconfig%288%29 vconfig 作用: (802.1q)VLAN配置程序 root@hbg:/# vconfig --helpBusyBox v1.22.1 ...