/*

本题的题意:

沙漠中有很多骆驼和一个池塘,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. QTP脚本汇总比较有价值

    1.Object Spy的Tips Hold the CTRL key to change the window focus or perform other mouse operations  2. ...

  2. 调试技术(/proc、/sys、/dev、strace)

    一.在/proc中实现文件 当某个进程读取我们的/proc文件时,内核会分配一个内存页,即PAGE_SIZE字节的内存块.驱动程序可以将数据通过这个内存页返回到用户空间. 第一种方法,不采用seq_f ...

  3. 数据结构之线性表的顺序存储结构的实现--C语言版

    #include <stdio.h> #include <stdlib.h> #include <time.h> #define INIT_SIZE 100 #de ...

  4. Linux下各类压缩文件命令小结

    .tar 解包:tar xvf FileName.tar    解包后原始文件仍存在 打包:tar cvf FileName.tar DirName1 Filename1 - 列出内容:tar tvf ...

  5. iOS 10权限崩溃问题

    手机升级到iOS10 Beta版本后,再次进行真机调试时会遇见权限崩溃问题,崩溃原因如下:This app has crashed because it attempted to access pri ...

  6. java复习-多线程

    和线程之间的关系: 进程:进程是程序的一次动态执行过程,他经理了代码加载,执行到执行完毕的一个完整过程,这个过程也是进程本身从产生,发展到最终消亡的过程. 线程:线程是实现并发机制的一种有效手段,进程 ...

  7. ACdream 1028 Path

    先思考一下序列上应该怎么做. 如果某段和为x,并且x为偶数,那么比x小的偶数,一定是这段的子段. 如果某段和为x,并且x为奇数,那么比x小的奇数,一定是这段的子段. 因此....只要寻找最大的连续的和 ...

  8. python3.4项目打包

    1.首先下载pyinstaller并且解压(就直接解压再桌面就可以,这样子比较方便) 2.然后就去下载pywin32(按照电脑和python的版本去下载) 我电脑是64位的,python是3.4版本的 ...

  9. <? extends T>和<? super T>

    转自:Java泛型中extends和super的区别? 另,问题来源:Java 泛型 <? super T> 中 super 怎么 理解?与 extends 有何不同? <? ext ...

  10. qml 中 使用 shader

    使用绘制工具如Photoshop .Flash已经可以创建许多效果非常绚丽的图像.动画等. Qt/QML 的努力其实是在这些工具发展的后面, 因此很多效果在Qt中无法实现. 不得不佩服Qt小组的才智, ...