csu1010: Water Drinking
/*
本题的题意:
沙漠中有很多骆驼和一个池塘,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的更多相关文章
- CSUOJ Water Drinking
Description The Happy Desert is full of sands. There is only a kind of animal called camel living on ...
- Taking water into exams could boost grades 考试带瓶水可以提高成绩?
Takeing a bottle of water into the exam hall could help students boost their grades, researchers cla ...
- Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
#1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
随机推荐
- Android编程获取手机的IMEI
手机在生产时,每部手机均有一个唯一的标识(ID),国际上采用国际移动设备身份码(IMEI, International Mobile Equipment Identity).IMEI是由15位数字组成 ...
- vs2008编译FileZilla服务端源码
vs2008编译FileZilla服务端源码 FileZilla服务端下载地址:https://download.filezilla-project.org/server/.FileZilla服务端源 ...
- 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) 解决办 ...
- Jmeter:相应断言介绍
Jmeter进行性能测试时,作为对上一个请求返回信息的校验,基本上断言是不可少的,今天主要介绍一下Jmeter的相应断言校验. 相应断言:即对服务器相应信息的校验判断,发送http请求后,对服务器返回 ...
- Linux系统编程初探系列之一:文件编程
系统函数 int creat(const char* filename,mode_t mode) filename:需要创建的文件名(包含路径,缺省为当前路径) mode:创建模式 常见的创建模式有: ...
- Ubuntu下NFS,TFTP服务搭建
环境:Ubuntu 一. 搭建NFS服务器 (1)安装: sudo apt-get install nfs-kernel-server #安装NFS服务器端 sudo apt-get instal ...
- 遍历(一)jquery $().each和$.each()
原文:http://www.frontopen.com/1394.html 在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法. $().each 在dom处理上 ...
- hdu_5286_wyh2000 and sequence(分块)
题目链接:hdu_5286_wyh2000 and sequence 题意: 给一段长度为N的序列,每次询问l-r(l和r和上一次询问的答案有关)内 不同的数的 出现次数的次方 的和.强制在线 题解: ...
- 3、Data对象
1.创建part1.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- vconfig 的使用
http://man.cx/vconfig%288%29 vconfig 作用: (802.1q)VLAN配置程序 root@hbg:/# vconfig --helpBusyBox v1.22.1 ...