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 <= ...
随机推荐
- laravel利用subquery使左连接查询右表数据唯一查询
如:表a,连接表b,b中有多条符合查询的记录 1.建立需要的子查询 $sub = DB::table('b')->select(['aid'])->selectRaw('max(id) a ...
- ftp服务器端的安装及配置
搭建过程 安装 vsftp 服务(yum 安装即可) 配置/etc/vsftpd/vsftpd.conf anonymous_enable=NO #禁止匿名登录 local_enable=YES ...
- 第14天dbutils与案例
第14天dbutils与案例 第14天dbutils与案例 1 1. 1.dbutils介绍 2 2. 2.dbutils快速入门 2 3. 3.dbutils A ...
- 新项目引入gulp
1:安装npm:官网下载nodejs--https://nodejs.org/en/.进行安装npm;--http://jingyan.baidu.com/article/a17d528506d7f5 ...
- php:跨域
一个没那么难的历史难题,其实只要在被请求端,加一句: header('Access-Control-Allow-Origin: *'); 然后--然后没有了. //跨域访问的时候才会存在此字段 $or ...
- SQLSERVER异机备份
/* 作者:landv 功能:异机备份 开发时间:2016年7月2日 15:27:08 */ ) drop procedure [dbo].[p_backupdb] GO create proc p_ ...
- iOS开发Embedded dylibs/frameworks are only supported on iOS 8.0 and later for architecture armv7的解决方法
全局搜索IPHONEOS_DEPLOYMENT_TARGE改为更小的版本
- 更改自身web项目的图标(默认为tomcat的小喵咪)
在页面<head>标签中加入 <link rel="shortcut icon" href="img/11.png" type="i ...
- soj 2543 完全二叉树
1000. 完全二叉树 Total: 338 Accepted: 81 Time Limit: 1sec Memory Limit:256MB Descr ...
- 让 idea webstorm phpstorm 能够 识别 thinkphp 的方法(自动提示功能)
1.在/ThinkPHP/Library/Think 目录下 新建一个文件,名为: BaseController.class.php 2.BaseController.class.php 内容为 n ...