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 <= ...
随机推荐
- 类加载class loader
Class装载验证流程: 加载:取得类的二进制流,转为方法区的数据结构,在java堆中生成对应的java.lang.class对象 链接:就是将已经读入到内存的类的二进制数据合并到虚拟机的运行时环境中 ...
- lr_abort()、exit(-1) 和 return-1之间的区别
int status; status = web_url("Login", "URL=https://secure.computing.com//login.asp?us ...
- blog地址
1 Java 设计模式 http://www.cnblogs.com/java-my-life/
- 2.安装中国版本的firefox
Linux刚安装好的时候,默认是火狐浏览器并且版本比较低,而且预装的是国际版 跟中国版无法同步,因为我windows上是中国版 首先去火狐主页,中文是http://www.firefox.com.cn ...
- 下载PhpStorm并进行激活
1.首先登陆PhpStorm官网http://www.jetbrains.com/phpstorm/ 点击附图中的download now 按钮 2.第二步根据os x \wind\ linux进行下 ...
- final使用
final修饰 基本数据类型时候 对应的 数据不能改变:::final修饰 对象类型 ,那么对应的引用地址不能改变(对象中的值可以改变): 如果final修改方法,那么该方法不能被子类重写 :: ...
- Apache环境服务器配置Let's Encrypt免费SSL证书及自动续期方法
如今越来越多的网站开始使用SSL证书,实现HTTPS网址形式,如果我们是英文网站更需要用到这样格式的HTTPS网址,因为根据谷歌搜索结果提示到如果用到SSL证书的在同等条件下排名结果是有靠前可能的.我 ...
- Maven 复制jar到指定目录
在完成模块开发后,需要发布jar到nexus上,与此同时,则要部署开发的模块,需要将编译打包的jar复制到指定的路径,再进行部署,而不是手动的去复制那些jar,因为当模块多的话,则会感到特别的烦,所以 ...
- 《Windows编程循序渐进》——基本控件
按钮控件的介绍 界面设计如下:
- 【定位:PDF文件定位关键字所在坐标和页码】
iText简介: iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件 ...